2 回答
TA贡献1803条经验 获得超6个赞
重新安装 html2canvas 后工作正常
import html2canvas from 'html2canvas';
import jspdf from 'jspdf';
sendToPdf(){
let data = document.getElementById("test");
// let data = document.getElementById("maindiv");
console.log(data);
html2canvas(data).then(canvas => {
const contentDataURL = canvas.toDataURL('image/jpeg', 1.0)
console.log(contentDataURL);
let pdf = new jspdf('l', 'cm', 'a4'); //Generates PDF in landscape mode
// let pdf = new jspdf('p', 'cm', 'a4'); //Generates PDF in portrait mode
pdf.addImage(contentDataURL, 'PNG', 0, 0, 29.7, 21.0);
pdf.save('Filename.pdf');
});
}
TA贡献1854条经验 获得超8个赞
要在 Angular13+ 中使用您的代码,您必须添加 2 行代码,否则会出现 Type 'null' is not allocateable to type 'HTMLElement' 错误:
您可以使用:
sendToPdf() {
let data = document.getElementById('test');
// let data = document.getElementById("maindiv");
console.log(data);
if (data != null) {
html2canvas(data).then((canvas) => {
const contentDataURL = canvas.toDataURL('image/jpeg', 1.0);
console.log(contentDataURL);
let pdf = new jsPDF('l', 'cm', 'a4'); //Generates PDF in landscape mode
// let pdf = new jspdf('p', 'cm', 'a4'); //Generates PDF in portrait mode
pdf.addImage(contentDataURL, 'PNG', 0, 0, 29.7, 21.0);
pdf.save('Filename.pdf');
});
}
}
添加回答
举报