其他分享
首页 > 其他分享> > 解决jsPDF导出html,黑屏,白屏,不全问题

解决jsPDF导出html,黑屏,白屏,不全问题

作者:互联网

1.项目中用到导出pdf,用到jsPDF,canvas,
2.网上很多方法,存在黑屏白屏,只能解决当前可视区域的问题
3.其中参考 https://blog.csdn.net/qq_36706878/article/details/111289963和https://blog.csdn.net/m0_37879526/article/details/103827235?utm_medium=distribute.pc_relevant_bbs_down.none-task--2~all~first_rank_v2~rank_v29-5.nonecase&depth_1-utm_source=distribute.pc_relevant_bbs_down.none-task--2~all~first_rank_v2~rank_v29-5.nonecase  ,这两位博友的博客,
使用方法:引入

<script src="https://cdn.bootcss.com/jspdf/1.5.3/jspdf.debug.js"></script> <script src="https://cdn.bootcss.com/html2canvas/0.5.0-beta4/html2canvas.min.js"></script> 方法名 downloadC() {//方法名绑定你的导出按钮                     var _this = this;                     var targetDom = $("#export_content");//你的目标元素id                     var copyDom = targetDom.clone();                     copyDom.width(targetDom.width() + "px");                     copyDom.height(targetDom.height() + "px");                     $('body').append(copyDom);                     console.log('copyDom',copyDom)                     html2canvas(copyDom, {                         onrendered:function(canvas) {                             var contentWidth = canvas.width;                             var contentHeight = canvas.height;                             var pageHeight = contentWidth / 592.28 * 841.89;                             var leftHeight = contentHeight;                             var position = 0;                             var f="p";                             // var imgWidth = 595.28;                             // var imgHeight = 592.28/contentWidth * contentHeight;
                            var pageHeight = contentWidth / 592.28 * 841.89;                             //未生成pdf的html页面高度                             var leftHeight = contentHeight;                             //页面偏移                             var position = 0;                             //a4纸的尺寸[595.28,841.89],html页面生成的canvas在pdf中图片的宽高                             var imgWidth = 595.28;                             var imgHeight = 592.28/contentWidth * contentHeight;                                              var pageData = canvas.toDataURL('image/jpeg', 1.0);                                              var pdf = new jsPDF('', 'pt', 'a4');                                                          //有两个高度需要区分,一个是html页面的实际高度,和生成pdf的页面高度(841.89)                             //当内容未超过pdf一页显示的范围,无需分页                             if (leftHeight < pageHeight) {                                 pdf.addImage(pageData, 'JPEG', 0, 0, imgWidth, imgHeight);                             } else {    // 分页                                 while(leftHeight > 0) {                                     pdf.addImage(pageData, 'JPEG', 0, position, imgWidth, imgHeight)                                     leftHeight -= pageHeight;                                     position -= 841.89;                                     // position += 841.89;                                     //避免添加空白页                                     if(leftHeight > 0) {                                         pdf.addPage();                                     }                                 }                             }                             // contentHeight > contentWidth ? f = 'p' : f = 'l';                             // var pageData = canvas.toDataURL('image/png', 1.0);                             // _this.aaa = canvas.toDataURL();                             // var pdf = new jsPDF(f, 'px', [imgWidth, imgHeight]);                             // pdf.addImage(pageData, 'png', 0, 0, imgWidth, imgHeight);                             pdf.save('content.pdf');                             copyDom.remove();                         }        });             },

标签:jsPDF,canvas,copyDom,html,contentWidth,var,pdf,contentHeight,黑屏
来源: https://www.cnblogs.com/eide/p/14234798.html