【问题标题】:export pdf for two divs inside pdf pages using domtoimage.toPng使用 domtoimage.toPng 为 pdf 页面内的两个 div 导出 pdf
【发布时间】:2021-06-24 03:11:33
【问题描述】:

我有两个 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>`

【问题讨论】:

    标签: javascript html pdf dom-to-image


    【解决方案1】:

    您需要准备 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();
        });
    });

    【讨论】:

    • 谢谢,但它给了我这个错误 dom-to-image.min.js:2 Uncaught (in promise) TypeError: Cannot read property 'cloneNode' of null at d (dom-to- image.min.js:2)
    • 看起来您在domtoimage.toPng() 之一中提供了一个空节点。请自行调试。
    猜你喜欢
    • 1970-01-01
    • 2016-12-31
    • 1970-01-01
    • 2014-03-15
    • 2014-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多