
转载将CSDN博客文章导出为PDF原文链接将CSDN文章导出为PDF方法进入浏览器开发者工具进入控制台可能需要在控制台中输入或配置“允许粘贴”输入如下代码按回车(function(){use strict;try{// 保存原始页面内容以便后续恢复varoriginalContentdocument.body.innerHTML;// 获取文章标题和内容根据CSDN页面结构varpageTitledocument.getElementsByClassName(article-title-box)[0].innerHTML;varmainContentdocument.getElementsByClassName(article_content)[0].innerHTML;// 移除文章内容容器上的内联样式确保内容完整显示varcontentBoxdocument.getElementsByClassName(article_content)[0];contentBox.removeAttribute(style);// 准备打印时的页眉和页脚内容varheaderContentdiv classprint-header styletext-align:center; font-size:14px; color:#666;导出自CSDN - newDate().toLocaleDateString()/div;varfooterContentdiv classprint-footer styletext-align:center; font-size:14px; color:#666;© newDate().getFullYear() 个人版权所有/div;// 创建打印专用样式优化PDF显示效果varstyledocument.createElement(style);style.innerHTMLmedia print { body { font-family: Microsoft YaHei, sans-serif; } .print-header { position: fixed; top: 0; width: 100%; } .print-footer { position: fixed; bottom: 0; width: 100%; } img { max-width: 100% !important; height: auto !important; } /* 确保图片适应页面 */ pre { white-space: pre-wrap; word-wrap: break-word; } /* 代码块自动换行 */ a { text-decoration: none; color: inherit; } /* 链接不显示下划线和颜色 */ };document.head.appendChild(style);// 临时替换页面内容只保留标题、文章主体和页眉页脚document.body.innerHTMLheaderContentpageTitlemainContentfooterContent;// 调整页面宽度和缩放比例优化打印布局document.body.style.width100%;document.body.style.zoom0.8;// 调用浏览器的打印功能用户可以选择保存为PDFwindow.print();// 打印完成后恢复原始页面内容document.body.innerHTMLoriginalContent;returnfalse;}catch(e){console.error(导出PDF出错:,e);// 发生错误时确保页面恢复document.body.innerHTMLoriginalContent;returnfalse;}})();