jsPDF -多页-呈现HTML总是到page1



我们使用jsPDF 2.5.1来呈现多页PDF。

我们使用html函数向每个页面呈现各种DOM元素,这在版本1中是可行的。jsPDF的x

但是现在每次我们调用。html() -它把它放在第一页,而不是新添加的页面,这里是代码

if (pdfPageIndex < numPdfPages) {
if (pdfPageIndex > 0) {
pdf.addPage();
}
pdf.html(
document.getElementById('pdfPage_' + pdfPageIndex),             
{
html2canvas: {
logging: true
},
callback: function(){ return pdfCallback($scope)}});

请尝试下面的基于画布高度和宽度的addmage方法,它将在多个页面中呈现

const data = document.getElementById('pdfPage_');
html2canvas(data).then((canvas:any) => {
const imgWidth = 208;
const pageHeight = 295;
const imgHeight = (canvas.height * imgWidth) / canvas.width;
let heightLeft = imgHeight;
let position = 0;
heightLeft -= pageHeight;
const doc = new jspdf('p', 'mm');
doc.addImage(canvas, 'PNG', 0, position, imgWidth, imgHeight, '', 'FAST');
while (heightLeft >= 0) {
position = heightLeft - imgHeight;
doc.addPage();
doc.addImage(canvas, 'PNG', 0, position, imgWidth, imgHeight, '', 'FAST');
heightLeft -= pageHeight;
}
doc.save('Downld.pdf');
});

最新更新