类型“jsPDF"上不存在属性“fromHTML" [英] Property 'fromHTML' does not exist on type 'jsPDF
问题描述
jsPDF 类型上不存在属性fromHTML"我正在尝试使用 jsPDF 将 HTML 代码转换为 PDF.我已经看到最好的方法是使用 jsPDF.fromHTML() 函数,但是当我尝试时,我得到以下信息:jsPDF"类型上不存在属性fromHTML"我使用的是 Angular 10 和 jsPDF@2.3.1.我已经试过了
Property 'fromHTML' does not exist on type jsPDF I am trying to use jsPDF to convert an HTML code to PDF. I've seen that the best way to do this is to use the jsPDF.fromHTML() function but when I try I get the following: Property 'fromHTML' does not exist on type 'jsPDF' I am using Angular 10 and jsPDF@2.3.1. I already tried
推荐答案
在我看来这种方法不存在.我看到两种方式:
From my point of view this method does not exist. I see two ways:
如果您的 html 代码包含样式信息
if your html code contains the style information
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.
or if the style is defined somewhere else (as usually in angular application). you html2canvas and extract an image from canvas to add to 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');
page.addImage(contentDataURL, 'PNG', 0, 0, imgWidth, imgHeight);
doc.save('test.pdf'); // save / download
doc.output('dataurlnewwindow'); // just open it
这篇关于类型“jsPDF"上不存在属性“fromHTML"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!