javascript
import html2Canvas from "html2canvas";
import jsPDF from "jspdf";
export function downloadPDF(dom, filename) {
const scale = 2;
window.pageYOffset = 0;
// 滚动到顶部,避免打印不全
document.documentElement.scrollTop = 0;
document.body.scrollTop = 0;
html2Canvas(dom, {
allowTaint: true,
scale,
}).then((canvas) => {
console.log("canvas", canvas);
const contentWidth = canvas.width / scale;
const contentHeight = canvas.height / scale;
const pdf = new jsPDF("", "pt", [contentWidth, contentHeight]);
const pageData = canvas.toDataURL("image/jpeg", 1.0);
pdf.addImage(pageData, "JPEG", 0, 0, contentWidth, contentHeight);
pdf.save(`${filename}.pdf`);
});
}