我正在使用dom to image生成A5 PDF。pdf的每一页上都会出现额外的空间,但canvasImageWidth和pdfWidth是相同的,内容应该正好适合pdf的宽度。如何删除这些空间?
// const margin = 15; const margin = 0; const htmlWidth = 419.52755906 - (margin * 2); // a5 size in pt const ratio = document.getElementById('article-body').offsetWidth / htmlWidth; const htmlHeight = document.getElementById('article-body').offsetHeight / ratio; let pdfWidth = 419.52755906; // a5 size in pt let pdfHeight = 595.27559055; // a5 size in pt const totalPDFPages = Math.ceil(htmlHeight / pdfHeight) - 1; const data = this.document.getElementById('article-body'); const canvasImageWidth = htmlWidth; const canvasImageHeight = htmlHeight; console.log("canvasImageWidth : " + canvasImageWidth ); console.log("pdfWidth: " + pdfWidth); domtoimage.toJpeg(data, { quality: 0.95, bgcolor: "#ffffff" }).then (function (dataUrl) { let pdf = new jsPDF('p', 'pt', [pdfWidth, pdfHeight]); pdf.addImage(dataUrl, 'png', margin, margin, canvasImageWidth, canvasImageHeight); for (let i = 1; i <= totalPDFPages; i++) { pdf.addPage([pdfWidth, pdfHeight], 'p'); pdf.addImage(dataUrl, 'png', margin, - (pdfHeight * i) + margin, canvasImageWidth, canvasImageHeight); } pdf.save("<?php echo $this->item->alias; ?>" + '.pdf'); }) .catch(function (error) { console.error('oops, something went wrong!', error); });
在此输入图像描述在此输入图像说明