废话不多数直接来代码:
1.安装包
npm install jspdf --save
npm install html2canvas --save
2.项目中引入
import jspdf from ‘jspdf‘;
import html2canvas from ‘html2canvas‘;
3.
var data = document.getElementById(‘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;
const contentDataURL = canvas.toDataURL(‘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
我是感觉有点失真。
如果有更好的代码或者更高的需要可以讨论、学习。