0

我有两个 div (div1,div2) 我想在 pdf 第 1 页中导出 div1 并在 pdf 第 2 页中导出 div2 我的问题我只能使用 domtoimage.toPng 从它们中捕获一个 div 如何捕获两个 div 我的代码:`

                               <script>
              $('#downloadPDF').click(function () {
              domtoimage.toPng(document.getElementById('div1'))
          //  domtoimage.toJpeg(document.getElementById('div2'))

    .then(function (blob) {
        var pdf = new jsPDF('p', 'pt', [$('#div1').width(), $('#div1').height()]);
        pdf.addImage(blob, 'PNG', 0, 0, $('#div2').width(), $('#div2').height());

  pdf.addPage();

        pdf.addImage(blob, 'PNG', 0, 0, $('#div1').width(), $('#div1').height());
        pdf.save("report.pdf");

        that.options.api.optionsChanged();
    });
    });
    </script>`
4

1 回答 1

0

您需要准备 2 个 blob 以将它们保存为 PDF。

$('#downloadPDF').click(function() {
  Promise.all([
      domtoimage.toPng(document.getElementById('div1')),
      domtoimage.toPng(document.getElementById('div2'))
    ])
    .then(function([blob1,  blob2]) {
      var pdf = new jsPDF('p', 'pt', [$('#div1').width(), $('#div1').height()]);
      pdf.addImage(blob1, 'PNG', 0, 0, $('#div1').width(), $('#div1').height());

      pdf.addPage();

      pdf.addImage(blob2, 'PNG', 0, 0, $('#div2').width(), $('#div2').height());
      pdf.save("report.pdf");

      that.options.api.optionsChanged();
    });
});

于 2021-03-27T12:35:17.247 回答