前端将网页转换为pdf并支持下载与上传

1.pdf下载

javascript 复制代码
handleExport() {
    const fixedH = document.getElementById("fixed-h");
    const pageOne = document.getElementById("mix-print-box-one");
    const pageTwo = document.getElementById("mix-print-box-two");
    fixedH.style.height = '30vh';
    pageOne.style.display = 'block';
    pageTwo.style.display = 'block';
    // 禁止滚动
    document.body.addEventListener('touchmove', this.preventDefaultScroll, { passive: false });
    document.body.addEventListener('wheel', this.preventDefaultScroll, { passive: false });
    html2canvas(pageOne).then((canvasOne) => {
        const imgOne = canvasOne.toDataURL("image/png");
        const pdf = new jsPDF();
        const propsOne = pdf.getImageProperties(imgOne);
        const widthOne = pdf.internal.pageSize.getWidth();
        const heightOne = (propsOne.height * widthOne) / propsOne.width;
        pdf.addImage(imgOne, "PNG", 0, 0, widthOne, heightOne);
        html2canvas(pageTwo).then(canvasTwo => {
            const imgTwo = canvasTwo.toDataURL("image/png");
            const propsTwo = pdf.getImageProperties(imgTwo);
            const widthTwo = pdf.internal.pageSize.getWidth();
            const heightTwo = (propsTwo.height * widthTwo) / propsTwo.width;
            pdf.addPage(); // 分页
            pdf.addImage(imgTwo, "PNG", 0, 0, widthTwo, heightTwo);
            const title = `${this.formOne.application}测试的.pdf`;
            pdf.save(title);
            fixedH.style.height = 'auto';
            pageOne.style.display = 'none';
            pageTwo.style.display = 'none';
            // 恢复滚动
            document.body.removeEventListener('touchmove', this.preventDefaultScroll);
            document.body.removeEventListener('wheel', this.preventDefaultScroll);
        });
    });
}

2.pdf上传

javascript 复制代码
handleUploadPdf(id) {
  const element = document.getElementById("myElementId");
  html2canvas(element).then((canvas) => {
    const imgData = canvas.toDataURL("image/png");
    const pdf = new jsPDF();
    const imgProps = pdf.getImageProperties(imgData);
    const pdfWidth = pdf.internal.pageSize.getWidth();
    const pdfHeight = (imgProps.height * pdfWidth) / imgProps.width;
    pdf.addImage(imgData, "PNG", 0, 0, pdfWidth, pdfHeight);
    // 将 PDF 转换为 Blob 对象
    const blob = pdf.output("blob");
    // 上传文件
    const fileName = "测试的.pdf";
    const formData = new FormData();
    formData.append("file", blob, fileName);
    uploadPdf(formData).then((res) => {
      // 将pdf地址传给后端
      uploadPdfUrl({
        id,
        fileName,
        path: res.fileName,
      });
      console.log("上传成功", res.url);
    });
  });
}
相关推荐
大怪v3 小时前
AI抢饭?前端佬:我要验牌!
前端·人工智能·程序员
新酱爱学习3 小时前
字节外包一年,我的技术成长之路
前端·程序员·年终总结
小兵张健4 小时前
开源 playwright-pool 会话池来了
前端·javascript·github
IT_陈寒6 小时前
Python开发者必知的5大性能陷阱:90%的人都踩过的坑!
前端·人工智能·后端
codingWhat7 小时前
介绍一个手势识别库——AlloyFinger
前端·javascript·vue.js
Lee川7 小时前
深度拆解:基于面向对象思维的“就地编辑”组件全模块解析
javascript·架构
代码老中医7 小时前
2026年CSS彻底疯了:这6个新特性让我删掉了三分之一JS代码
前端
进击的尘埃7 小时前
Web Worker 与 OffscreenCanvas:把主线程从重活里解放出来
javascript
不会敲代码17 小时前
Zustand:轻量级状态管理,从入门到实践
前端·typescript
踩着两条虫7 小时前
VTJ.PRO 双向代码转换原理揭秘
前端·vue.js·人工智能