我试图使用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>
有人可以帮我吗?
答案 0 :(得分: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>