无法使用jsPDF获取div内容

问题描述 投票:1回答:1

我试图使用jsPDF和html2canvas从HTML生成PDF。它使用整页,但不是使用getElementById()的特定div。控制台说

IndexSizeError: Index or size is negative or greater than the allowed amount html2canvas.js:2859

这是我的剧本

 <script>
        let doc = new jsPDF();

        doc.addHTML(document.getElementById('content'), function() {
            doc.save('html.pdf');
        });
    </script>

谁能帮我这个?

javascript jspdf html2canvas
1个回答
1
投票

doc.addHTML()现在实际上是deprecated。新的矢量支持API doc.html()工作得更好。亲自看看他们的sample。这是工作代码:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.debug.js" 
        integrity="sha384-NaWTHo/8YCBYJ59830LTz/P4aQZK1sS0SneOgAvhsIl3zBu8r9RevNg5lHCHAuQ/"
        crossorigin="anonymous"></script>
<script src="https://html2canvas.hertzen.com/dist/html2canvas.min.js"></script>
<!-- html2canvas 1.0.0-alpha.11 or higher version is needed -->

<script>
    function download() {
        let pdf = new jsPDF('l', 'pt', 'a3');
        pdf.html(document.getElementById('content'), {
            callback: function (pdf) {
                pdf.save('test.pdf');
            }
        });
    }
</script>

更新:如果你有一个空白页面,try different versions of html2canvas

© www.soinside.com 2019 - 2024. All rights reserved.