解决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();
                        }
       });
            },
posted @   天堂花草  阅读(4214)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示