jsPDF官网
html2Canvas官网
<div id="canvas_pdf">
我是一段话
</div>
import html2canvas from 'html2canvas';
import {
jsPDF } from 'jspdf';
const _this = this;
const canvas_pdf = document.getElementById('canvas_pdf');
canvas_pdf.style.background = '#FFFFFF';
const contentWidth = canvas_pdf.scrollWidth;
const contentHeight = canvas_pdf.scrollHeight;
const pageHeight = (contentWidth / 592.28) * 841.89;
let leftHeight = contentHeight;
let position = 0;
const imgWidth = 595.28;
const imgHeight = (592.28 / contentWidth) * contentHeight;
console.log(contentHeight)
html2canvas(canvas_pdf, {
width: contentWidth,
height: contentHeight,
scrollY: -canvas_pdf.scrollTop,
useCORS: _this.images.length ? true : true,
}).then(function (canvas) {
document.body.appendChild(canvas);
const pageData = canvas.toDataURL('image/jpeg', 1);
var pdf = new jsPDF('', 'pt', 'a4');
if (leftHeight < pageHeight) {
pdf.addImage(pageData, 'JPEG', 0, 0, imgWidth, imgHeight);
} else {
while (leftHeight > 0) {
pdf.addImage(pageData, 'JPEG', 0, position, imgWidth, imgHeight);
leftHeight -= pageHeight;
position -= 841.89;
if (leftHeight > 0) {
pdf.addPage();
}
}
}
pdf.save('content.pdf');
});