前端将网页转换为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);
    });
  });
}
相关推荐
差点GDP1 小时前
模拟请求测试 Fake Rest API Test
前端·网络·json
酒尘&2 小时前
Hook学习-上篇
前端·学习·react.js·前端框架·react
houyhea2 小时前
从香港竹脚手架到前端脚手架:那些"借来"的发展智慧与安全警示
前端
哈哈~haha3 小时前
Step 14: Custom CSS and Theme Colors 自定义CSS类
前端·css·ui5
Ndmzi3 小时前
Matlab编程技巧:自定义Simulink菜单(理解补充)
前端·javascript·python
勇气要爆发3 小时前
物种起源—JavaScript原型链详解
开发语言·javascript·原型模式
我命由我123453 小时前
VSCode - VSCode 修改文件树缩进
前端·ide·vscode·前端框架·编辑器·html·js
catchadmin3 小时前
Vite 8 Beta:Rolldown 驱动的新一代 Vite
vue
SoaringHeart4 小时前
Flutter组件封装:验证码倒计时按钮 TimerButton
前端·flutter
San30.4 小时前
深入理解 JavaScript OOP:从一个「就地编辑组件」看清封装、状态与原型链
开发语言·前端·javascript·ecmascript