无法使用jsPDF获取div内容 [英] Can't get div content using jsPDF

查看:225
本文介绍了无法使用jsPDF获取div内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用jsPDF和html2canvas从HTML生成PDF.它适用于整页,但不适用于使用getElementById()的特定div.控制台显示

I was trying to generate PDF from HTML using jsPDF and html2canvas. It worked with full page, but not specific div using getElementById(). The console says

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>

有人可以帮我吗?

推荐答案

doc.addHTML()实际上是已弃用.新的支持向量的API doc.html()效果更好.亲自查看他们的示例.这是工作代码:

doc.addHTML() is actually deprecated now. The new vector-supporting API, doc.html(), works much better. See their sample for yourself. Here is the working code:

<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>

更新:如果您有空白页,请尝试不同版本的html2canvas

这篇关于无法使用jsPDF获取div内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆