jsPDF - 多个页面 - 渲染 HTML 始终转到第 1 页

Alw*_*ing 4 html2canvas jspdf

我们使用 jsPDF 2.5.1 渲染多页 PDF。

我们使用 html 函数将各种 DOM 元素渲染到每个页面,这在 jsPDF 1.x 版本中有效

但是现在每次我们调用 .html() - 它都会将其放在第一页上,而不是新添加的页面上,这是代码

if (pdfPageIndex < numPdfPages) {
    if (pdfPageIndex > 0) {
        pdf.addPage();
    }

    pdf.html(
        document.getElementById('pdfPage_' + pdfPageIndex),             
        {
         html2canvas: {
             logging: true
             },
         callback: function(){ return pdfCallback($scope)}});
Run Code Online (Sandbox Code Playgroud)

Nag*_*Nag 13

请尝试使用以下基于画布高度和宽度的 addImage 方法,它将在多个页面中呈现

const data = document.getElementById('pdfPage_');
html2canvas(data).then((canvas:any) => {
  const imgWidth = 208;
  const pageHeight = 295;
  const imgHeight = (canvas.height * imgWidth) / canvas.width;
  let heightLeft = imgHeight;
  let position = 0;
  heightLeft -= pageHeight;
  const doc = new jspdf('p', 'mm');
  doc.addImage(canvas, 'PNG', 0, position, imgWidth, imgHeight, '', 'FAST');
  while (heightLeft >= 0) {
    position = heightLeft - imgHeight;
    doc.addPage();
    doc.addImage(canvas, 'PNG', 0, position, imgWidth, imgHeight, '', 'FAST');
    heightLeft -= pageHeight;
  }
  doc.save('Downld.pdf');
});
Run Code Online (Sandbox Code Playgroud)