[英]How can I use jsPDF with angular 7.2 to export component html of dynamic data into a PDF file?
I have jsPDF, html2canvas installed and imported those libraries ina component. 我安装了jsPDF,html2canvas并将这些库导入到组件中。
On clicking a button, it triggers to a function which is supposed to export a html file to a PDF file. 单击按钮时,它会触发一个函数,该函数应该将html文件导出为PDF文件。 But it is not working.
但它没有用。
Also, how can I use jsPDF to load an html file with dynamic data that I get from an API and download it into a PDF file? 另外,如何使用jsPDF加载带有动态数据的html文件,我从API获取并将其下载到PDF文件中?
Here are the codes: - jspdf.component.html 以下是代码: - jspdf.component.html
<div id="content" #content>
<div class="alert alert-info">
<strong>Html To PDF Conversion - Angular 7.2</strong>
</div>
<div>
<input type="button" value="CPTURE" (click)="captureScreen()"/>
</div>
</div>
<div class="text-danger" id="contentToConvert">hi everyone</div>
jspdf.component.ts jspdf.component.ts
import * as jspdf from 'jspdf'; import html2canvas from 'html2canvas'; public captureScreen() { var data = document.getElementById('contentToConvert'); html2canvas(data).then(canvas => { // Few necessary setting options var imgWidth = 208; var pageHeight = 295; var imgHeight = canvas.height * imgWidth / canvas.width; var heightLeft = imgHeight; const contentDataURL = canvas.toDataURL('image/png') let pdf = new jspdf('p', 'mm', 'a4'); // A4 size page of PDF var position = 0; pdf.addImage(contentDataURL, 'PNG', 0, position, imgWidth, imgHeight) pdf.save('MYPdf.pdf'); // Generated PDF }); }
On clicking Capture Screen button, the console shows the error core.js:15724 ERROR Error: Uncaught (in promise): TypeError: Cannot assign to read only property 'className' of object '[object SVGSVGElement]' TypeError: Cannot assign to read only property 'className' of object '[object SVGSVGElement]'
单击“捕获屏幕”按钮时,控制台显示错误
core.js:15724 ERROR Error: Uncaught (in promise): TypeError: Cannot assign to read only property 'className' of object '[object SVGSVGElement]' TypeError: Cannot assign to read only property 'className' of object '[object SVGSVGElement]'
Try like this: 试试这样:
captureScreen() {
var doc = new jsPDF();
var content = document.getElementById('contentToConvert')
html2canvas(content).then(canvas => {
doc.addImage(canvas.toDataURL("image/png"), 'PNG', 15, 40, 180, 110);
doc.save(`MYPdf.pdf`);
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.