前端将网页转换为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);
    });
  });
}
相关推荐
xiangxiongfly91516 分钟前
CSS link标签
前端·css
快乐非自愿43 分钟前
常用设计模式:工厂方法模式
javascript·设计模式·工厂方法模式
岁月宁静1 小时前
AI 多模态全栈应用项目描述
前端·vue.js·node.js
十年磨一剑~1 小时前
html+js开发一个测试工具
javascript·css·html
nn_(nana)2 小时前
修改文件权限--- chmod ,vi/vim,查看文件内容,yum-软件包管理器,systemctl管理系统服务
前端
汪汪队立大功1232 小时前
JavaScript是怎么和html元素关联起来的?
开发语言·javascript·html
烛阴3 小时前
从零开始掌握C#核心:变量与数据类型
前端·c#
han_3 小时前
前端高频面试题之Vuex篇
前端·vue.js·面试
qq_415216253 小时前
vue3搭建项目yarn+vue3+webpack+less+element-plus
前端·webpack·less