首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >类型'jsPDF‘上不存在属性'fromHTML’

类型'jsPDF‘上不存在属性'fromHTML’
EN

Stack Overflow用户
提问于 2021-07-30 09:49:09
回答 2查看 3.2K关注 0票数 1

类型jsPDF上不存在属性'fromHTML‘我正尝试使用jsPDF将HTML代码转换为PDF。我已经看到最好的方法是使用jsPDF.fromHTML()函数,但是当我尝试的时候得到如下结果:属性'fromHTML‘在类型'jsPDF’上不存在,我使用的是Angular 10和jsPDF@2.3.1。我已经试过了

EN

回答 2

Stack Overflow用户

发布于 2021-08-03 23:22:45

嗯,这是因为文档中看到的.fromHTML()已经被弃用,取而代之的是.html()jsPDF documentation html()

票数 3
EN

Stack Overflow用户

发布于 2021-07-30 12:02:14

在我看来,这种方法是不存在的。我认为有两种方式:

如果您的html代码包含样式信息

代码语言:javascript
复制
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。

代码语言:javascript
复制
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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68588904

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档