javascript – 如何使用jsPDF和HTML2Canvas拥有多个pdf页面

前端之家收集整理的这篇文章主要介绍了javascript – 如何使用jsPDF和HTML2Canvas拥有多个pdf页面前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个脚本使用 HTML2Canvas在页面截取div的截图,然后使用jsPDF将其转换为pdf.

问题是生成的pdf只有一页,在某些情况下屏幕截图需要多个页面.例如,屏幕截图大于8.5×11.宽度很好,但我需要它来创建多个页面以适应整个屏幕截图.

这是我的脚本:

  1. var pdf = new jsPDF('portrait','pt','letter');
  2. $('.export').click(function() {
  3. pdf.addHTML($('.profile-expand')[0],function () {
  4. pdf.save('bfc-schedule.pdf');
  5. });
  6. });

有什么想法我可以修改它以允许多个页面

解决方法

pdf.addHtml doesnot work if there are svg images on the web page..
I copy the solution here: // suppose your picture is already in a canvas var imgData = canvas.toDataURL(‘image/png’); /* Here are the numbers (paper width and height) that I found to work. It still creates a little overlap part between the pages,but good enough for me. if you can find an official number from jsPDF,use them. */

  1. var imgWidth = 210;
  2. var pageHeight = 295;
  3. var imgHeight = canvas.height * imgWidth / canvas.width;
  4. var heightLeft = imgHeight;
  5. var doc = new jsPDF('p','mm');
  6. var position = 0;
  7.  
  8. doc.addImage(imgData,'PNG',position,imgWidth,imgHeight);
  9. heightLeft -= pageHeight;
  10.  
  11. while (heightLeft >= 0) {
  12. position = heightLeft - imgHeight;
  13. doc.addPage();
  14. doc.addImage(imgData,imgHeight);
  15. heightLeft -= pageHeight;
  16. }
  17. doc.save( 'file.pdf');`

猜你在找的JavaScript相关文章