jsPDF 类型上不存在属性“fromHTML” 我正在尝试使用 jsPDF 将 HTML 代码转换为 PDF。我发现最好的方法是使用 jsPDF.fromHTML() 函数,但是当我尝试时,我得到以下信息: Property 'fromHTML' does not exit 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
Run Code Online (Sandbox Code Playgroud)
或者如果样式是在其他地方定义的(通常在角度应用程序中)。你 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
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
13046 次 |
最近记录: |