jsPDF:使用新的.html()方法时未加载html2canvas [英] jsPDF: html2canvas not loaded while using new .html() method
问题描述
我想使用 jsPDF.html 将html页面转换为pdf,并且我正在使用以下代码:
I want to use jsPDF.html to convert html page to pdf, and I'm using this code:
savePdf () {
var pdf = new jsPDF({unit: 'mm', format: 'a4', orientation: 'portrait' })
pdf.html(document.getElementById('printable-cv'), {
callback: function (pdf) {
pdf.save('cv-a4.pdf');
}
})
}
但出现错误html2canvas not loaded
:是我忘记了什么吗?我确实有html2canvas
but I get error html2canvas not loaded
: is it something I forgot? I do have html2canvas
"html2canvas":"^ 1.0.0-alpha.12"
"html2canvas": "^1.0.0-alpha.12"
我正在将vuejs与webpack一起使用.
I'm using vuejs with webpack.
在同一页面上,我当前正在使用 html2pdf 并使用以下代码:
In the same page I'm currently using alternatively html2pdf with the following code:
savePdf0 () {
let opt = {
filename: 'cv.pdf',
enableLinks: true,
image: { type: 'jpeg', quality: 0.98 },
html2canvas: {
scale: 8,
useCORS: true,
width: 310,
letterRendering: true,
},
jsPDF: { unit: 'mm', format: 'a4', orientation: 'portrait' },
}
html2pdf().set(opt).from(document.getElementById('printable-cv')).save()
}
正确找到html2canvas的
.
that correcly finds html2canvas.
html2canvas not loaded
的真正含义是什么?我该怎么做才能加载它?
What does html2canvas not loaded
really mean? what can I do to load it?
推荐答案
jsPDF需要在全局范围内声明html2canvas才能工作,因此您必须编写
jsPDF needs html2canvas to be declared in the global scope to work, so you have to write
window.html2canvas = html2canvas;
在致电html()
之前的某个地方.
somewhere before you call html()
.
也就是说,我也无法使其正常工作,所以我求助于一个包装器可以通过手动调用html2canvas()
然后将生成的画布提供给jsPDF来解决该问题.
That said, I wasn't able to make it work either, so I resorted to a wrapper that works around the issue by calling manually html2canvas()
then giving the resulting canvas to jsPDF.
这篇关于jsPDF:使用新的.html()方法时未加载html2canvas的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!