'jsPDF'类型上不存在属性'fromHTML'。 我试图使用jsPDF将HTML代码转换为PDF。我看到最好的方法是使用jsPDF.fromHTML()函数,但是当我尝试时,会出现以下问题:Property 'fromHTML' does not exist on type 'jsPDF'。 我正在使用Angular 10和jsPDF@2.3.1。我已经尝试过
从我的角度来看,这种方法不存在。我看到两种方法:
如果您的HTML代码包含样式信息
const doc = new jsPDF('p', 'pt', 'a4');
const div = ...your code to get the html
await doc.html(div);
doc.save('test.pdf'); // save / download
doc.output('dataurlnewwindow'); // just open it
如果样式在其他地方定义(通常在 Angular 应用程序中),你可以使用 html2canvas 从画布中提取图像并添加到 PDF 中。
const doc = new jsPDF('p', 'pt', 'a4');
const div = ...your code to get the html
await html2canvas(... your element ...).then(canvas => {
// Few necessary setting options
const imgWidth = 208; // your own stuff to calc the format you want
const imgHeight = canvas.height * imgWidth / canvas.width; // your own stuff to calc the format you want
const contentDataURL = canvas.toDataURL('image/png');
doc.addImage(contentDataURL, 'PNG', 0, 0, imgWidth, imgHeight);
doc.save('test.pdf'); // save / download
doc.output('dataurlnewwindow'); // just open it
page
应该改为 doc
。我经常使用 page = doc
和 page = doc.addPage()
来更好地理解,即我正在向文档内的当前页面添加图像或内容。感谢您注意到了这一点。 - Thomas Renger