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`);
});
}
前端PDF导出,使用html2Canvas和jsPDF插件
最新推荐文章于 2023-11-16 08:53:39 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)