类型“jsPDF”上不存在属性“fromHTML”。

3

'jsPDF'类型上不存在属性'fromHTML'。 我试图使用jsPDF将HTML代码转换为PDF。我看到最好的方法是使用jsPDF.fromHTML()函数,但是当我尝试时,会出现以下问题:Property 'fromHTML' does not exist on type 'jsPDF'。 我正在使用Angular 10和jsPDF@2.3.1。我已经尝试过


1
你能提供一段代码示例和更多上下文吗?如果没有看到您如何使用它的更多信息,很难解决问题。 - Dan Obregon
2个回答

6

这是因为文档中已经说明不再使用.fromHTML(),推荐使用.html()。详见jsPDF文档之html()


5

从我的角度来看,这种方法不存在。我看到两种方法:

如果您的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

页面变量未定义。 - Kevingy
@Kevingy 你说得完全正确。在这个例子中,page 应该改为 doc。我经常使用 page = docpage = doc.addPage() 来更好地理解,即我正在向文档内的当前页面添加图像或内容。感谢您注意到了这一点。 - Thomas Renger
至少这种方法曾经存在:https://codepen.io/amkid/pen/qKYwXo - Brice C.
至少曾经存在这种方法:https://codepen.io/amkid/pen/qKYwXo - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接