angular将HTML转化为pdf⽂件导出废话不多数直接来代码:
1.安装包
npm install jspdf --save
npm install html2canvas --save
2.项⽬中引⼊
import jspdf from 'jspdf';
import html2canvas from 'html2canvas';
3.
var data = ElementById('table');
html2canvas(data).then(canvas => { // Few necessary setting options
let imgWidth = 208;
let pageHeight = 295;
let imgHeight = canvas.height * imgWidth / canvas.width;
let heightLeft = imgHeight;
pdf转html
const contentDataURL = DataURL('image/png')
let pdf = new jspdf('p', 'mm', 'a4'); // A4 size page of PDF
let position = 0;
pdf.addImage(contentDataURL, 'PNG', 0, position, imgWidth, imgHeight)
pdf.save('[' + this.systomCode + ']' + this.systomName + '.pdf'); // Generated PDF
如果有更好的代码或者更⾼的需要可以讨论学习