类型jsPDF上不存在属性'fromHTML‘我正尝试使用jsPDF将HTML代码转换为PDF。我已经看到最好的方法是使用jsPDF.fromHTML()函数,但是当我尝试的时候得到如下结果:属性'fromHTML‘在类型'jsPDF’上不存在,我使用的是Angular 10和jsPDF@2.3.1。我已经试过了
发布于 2021-08-03 23:22:45
嗯,这是因为文档中看到的.fromHTML()已经被弃用,取而代之的是.html()。jsPDF documentation html()
发布于 2021-07-30 12:02:14
在我看来,这种方法是不存在的。我认为有两种方式:
如果您的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');
page.addImage(contentDataURL, 'PNG', 0, 0, imgWidth, imgHeight);
doc.save('test.pdf'); // save / download
doc.output('dataurlnewwindow'); // just open ithttps://stackoverflow.com/questions/68588904
复制相似问题