接上篇文章,https://www.cnblogs.com/boytnt/p/11711439.html,继续探索如何给每页加页眉,其实思想很简单,比如页眉上想添加logo,那就在jspdf创建每页时多加个图片上去就好了。
先上效果图,借华为LOGO一用:
再放代码,主体还是上篇文章的分页导出算法:
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>测试PDF导出</title> 6 <meta http-equiv="X-UA-Compatible" content="IE=edge"> 7 <meta name="viewport" content="width=device-width, initial-scale=1"> 8 <script src="js/html2canvas.min.js"></script> 9 <script src="js/jspdf.min.js"></script> 10 <style> 11 html,body { margin:0; padding:0; } 12 #page ul { padding:0;list-style:none; } 13 #page li { line-height:110px;color:#fff;padding-left:10px;font-size:26px; } 14 #logo { display:none; } 15 .c0 { background-color:#ea644a; } 16 .c1 { background-color:#f1a325; } 17 .c2 { background-color:#38b03f; } 18 .c3 { background-color:#03b8cf; } 19 .c4 { background-color:#bd7b46; } 20 .c5 { background-color:#8666b8; } 21 </style> 22 <script> 23 function exportPdf() { 24 var element = document.getElementById("page"); 25 html2canvas(element, { 26 logging:false 27 }).then(function(canvas) { 28 var pdf = new jsPDF('p', 'mm', 'a4'); //A4纸,纵向 29 30 var ctx = canvas.getContext('2d'), 31 a4w = 190, a4h = 257, //A4大小,210mm x 297mm,左右保留10mm,上下保留20mm的边距,显示区域190x257 32 imgHeight = Math.floor(a4h * canvas.width / a4w), //按A4显示比例换算一页图像的像素高度 33 renderedHeight = 0; 34 35 var logo = document.getElementById("logo"); //放在页眉的图标 36 37 while(renderedHeight < canvas.height) { 38 var page = document.createElement("canvas"); 39 page.width = canvas.width; 40 page.height = Math.min(imgHeight, canvas.height - renderedHeight); //可能内容不足一页 41 42 //用getImageData剪裁指定区域,并画到前面创建的canvas对象中 43 page.getContext('2d').putImageData(ctx.getImageData(0, renderedHeight, canvas.width, Math.min(imgHeight, canvas.height - renderedHeight)), 0, 0); 44 45 //添加图像到页面,保留10mm/20mm边距 46 pdf.addImage(page.toDataURL('image/jpeg', 1.0), 'JPEG', 10, 20, a4w, Math.min(a4h, a4w * page.height / page.width)); 47 //添加页眉的logo 48 pdf.addImage(logo, 'PNG', 5, 3); 49 50 renderedHeight += imgHeight; 51 if(renderedHeight < canvas.height) 52 pdf.addPage(); //如果后面还有内容,添加一个空页 53 54 delete page; 55 } 56 pdf.save('content.pdf'); 57 }); 58 } 59 60 function generateData() { 61 var html = []; 62 html[html.length] = '<ul>'; 63 for(var i = 0;i < 20;++i) { 64 html[html.length] = '<li class="c'; 65 html[html.length] = i % 6; 66 html[html.length] = '">这是第'; 67 html[html.length] = i; 68 html[html.length] = '行</li>'; 69 } 70 html[html.length] = '</ul>'; 71 72 document.getElementById('page').innerHTML = html.join(''); 73 } 74 </script> 75 </head> 76 <body onload="generateData()"> 77 <button onclick="exportPdf()">导出pdf</button> 78 <div id="page"></div> 79 <img id="logo" src="huawei.png" /> 80 </body> 81 </html>
变化在于以下几点:
1、添加了一个隐藏的<img>标签,用于承载logo;
2、创建每一页时,多加了一行pdf.addImage(logo, 'PNG', 5, 3),用于在页眉位置放logo;
不使用<img>标签,在代码里直接创建Image对象然后指定src来加载图片也行,但要注意应在Image对象的onload事件里再处理后续代码。另外注意运行时不能直接双击打开,需要放到web服务器上,因为加载了图片资源,受浏览器安全性限制,toDataURL仅允许同源资源,否则会报错:Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported。