江门网站推广,线上渠道推广怎么做,旅游网站建设课程设计,如何注册网站域名和购买虚拟主机目前市面上常用的前端导出PDF库组合一般为#xff1a;
1. html2canvas js-pdf
2. html2canvaspdf-lib
3. domtoimagepdf-lib
因本人项目中导出pdf需求为导出30页及以上的多页pdf#xff0c;考虑性能问题#xff0c;选择了 html2canvaspdf-lib 及domtoimagepdf-lib两种方…目前市面上常用的前端导出PDF库组合一般为
1. html2canvas js-pdf
2. html2canvaspdf-lib
3. domtoimagepdf-lib
因本人项目中导出pdf需求为导出30页及以上的多页pdf考虑性能问题选择了 html2canvaspdf-lib 及domtoimagepdf-lib两种方式尝试实现
html2canvaspdf-lib个人推荐因为适配ios Safari浏览器 本人是先尝试使用的domtoimagepdf-lib方案但实测中发现H5在ios Safari浏览器端倒不出来故有个html2canvaspdf-lib方案经实战测试该方案能够适配ios Safari浏览器导出
代码如下
首先引入必要插件
yarn add pdf-lib
yarn add html2canvas
yarn add file-saver
file-saver 插件很重要使用a.click方案导出的pdf在Safari中不是直接下载而是打开一个类似预览页的页面查看pdf需用户分享导出比较麻烦。
async downloadPDF() {// 创建一个新的 PDF 文档const pdfDoc await PDFDocument.create();// 处理需转pdf的dom的id数组const pdfDoms await this.handlePDFPageDom();this.allNum pdfDoms.length;for (let i 0; i pdfDoms.length; i) {this.loadText 文件生成中${i 1}/${this.allNum};const doc document.querySelector(# pdfDoms[i]);const canvas await html2canvas(doc, {scale: 2, // 提高清晰度控制内存useCORS: true,});const imgDataUrl canvas.toDataURL(image/jpeg, 0.95); // 压缩图像const imgBytes await fetch(imgDataUrl).then((res) res.arrayBuffer());const img await pdfDoc.embedJpg(imgBytes);// const { width, height } img.scaleToFit(595.28, 841.89);const A4_WIDTH 595.28; // A4 宽度const A4_HEIGHT 841.89; // A4 高度const scale Math.min(A4_WIDTH / img.width, A4_HEIGHT / img.height);const scaledWidth img.width * scale;const scaledHeight img.height * scale;const xOffset (A4_WIDTH - scaledWidth) / 2;const yOffset (A4_HEIGHT - scaledHeight) / 2;const page pdfDoc.addPage([595.28, 841.89]);page.drawImage(img, {x: xOffset,y: yOffset,width: scaledWidth,height: scaledHeight,});canvas.remove();await new Promise((resolve) setTimeout(resolve, 100)); // 防止卡死}const pdfBytes await pdfDoc.save();const blob new Blob([pdfBytes], {type: application/octet-stream,});FileSaver.saveAs(blob, 导出的PDF.pdf);uni.hideLoading();this.loadText 文件生成成功!;},
domtoimagepdf-lib
async downloadPDF() {this.loadText 文件生成中...;// 创建一个新的 PDF 文档const pdfDoc await PDFDocument.create();// 处理需转pdf的dom idconst pdfDoms await this.handlePDFPageDom();let pdfPage [];let base64Arr [];for (let i 0; i pdfDoms.length; i) {const element document.getElementById(pdfDoms[i]);const url await domtoimage.toPng(element, {quality: 0.95,skipFonts: true,});base64Arr.push({ base64: url });}await base64Arr.map((item, index) {pdfDoc.addPage([595.28, 841.89]);pdfPage.push(this.handleReportView(item.base64, index, pdfDoc));});await Promise.all(pdfPage).then(async (res) {// 将 PDF 文档保存为 Uint8Arrayconst pdfBytes await pdfDoc.save();// 生成下载链接并自动下载 PDFconst blob new Blob([pdfBytes], { type: application/pdf });const link document.createElement(a);link.href URL.createObjectURL(blob);link.download ${this.studentName}.pdf;link.click();URL.revokeObjectURL(link.href);uni.hideLoading();this.loadText 文件生成成功!;setTimeout(() {window.parent.postMessage({cmd: success,});}, 1000);}).catch((err) {// PDF null;console.log(生成失败, err);});},async handleReportView(imgBase64, index, pdfDoc) {const A4_WIDTH 595.28; // A4 宽度const A4_HEIGHT 841.89; // A4 高度// 获取所有页面const pages pdfDoc.getPages();// 修改第index页索引从0开始const pageNow pages[index];return await new Promise(async (resolve, reject) {const pageData imgBase64;// setTimeout(() {let img new Image();img.crossOrigin Anonymous;img.onload async () {const imgBytes await fetch(pageData).then((res) res.arrayBuffer());// 嵌入 PNG 图片const pngImage await pdfDoc.embedPng(imgBytes);const { width: imgWidth, height: imgHeight } img;// 计算缩放比例确保图片适应 A4 页面并保持宽高比const scale Math.min(A4_WIDTH / imgWidth, A4_HEIGHT / imgHeight);const scaledWidth imgWidth * scale;const scaledHeight imgHeight * scale;img.width scaledWidth;img.height scaledHeight;// 计算图片的偏移量使其居中显示在页面上const xOffset (A4_WIDTH - scaledWidth) / 2;const yOffset (A4_HEIGHT - scaledHeight) / 2;// 将内容设置到第几页await pageNow.drawImage(pngImage, {x: xOffset,y: yOffset,width: scaledWidth,height: scaledHeight,});resolve();};img.onerror () {alert(资源加载失败);resolve();};img.src pageData;// }, 500);}).catch((err) {return Promise.resolve();});},
文章转载自: http://www.morning.rqqmd.cn.gov.cn.rqqmd.cn http://www.morning.fgrkc.cn.gov.cn.fgrkc.cn http://www.morning.tlbhq.cn.gov.cn.tlbhq.cn http://www.morning.stbhn.cn.gov.cn.stbhn.cn http://www.morning.lwmxk.cn.gov.cn.lwmxk.cn http://www.morning.trpq.cn.gov.cn.trpq.cn http://www.morning.sfwcb.cn.gov.cn.sfwcb.cn http://www.morning.xpqsk.cn.gov.cn.xpqsk.cn http://www.morning.hhpkb.cn.gov.cn.hhpkb.cn http://www.morning.ypktc.cn.gov.cn.ypktc.cn http://www.morning.tkkjl.cn.gov.cn.tkkjl.cn http://www.morning.ybshj.cn.gov.cn.ybshj.cn http://www.morning.fdrb.cn.gov.cn.fdrb.cn http://www.morning.lpgw.cn.gov.cn.lpgw.cn http://www.morning.fxxmj.cn.gov.cn.fxxmj.cn http://www.morning.nrddx.com.gov.cn.nrddx.com http://www.morning.brlgf.cn.gov.cn.brlgf.cn http://www.morning.jfjqs.cn.gov.cn.jfjqs.cn http://www.morning.jycr.cn.gov.cn.jycr.cn http://www.morning.pbzgj.cn.gov.cn.pbzgj.cn http://www.morning.kgxrq.cn.gov.cn.kgxrq.cn http://www.morning.hilmwmu.cn.gov.cn.hilmwmu.cn http://www.morning.ggrzk.cn.gov.cn.ggrzk.cn http://www.morning.kdpal.cn.gov.cn.kdpal.cn http://www.morning.gfkb.cn.gov.cn.gfkb.cn http://www.morning.wmqxt.cn.gov.cn.wmqxt.cn http://www.morning.rdmn.cn.gov.cn.rdmn.cn http://www.morning.wjqyt.cn.gov.cn.wjqyt.cn http://www.morning.fqpgf.cn.gov.cn.fqpgf.cn http://www.morning.kmprl.cn.gov.cn.kmprl.cn http://www.morning.mrccd.cn.gov.cn.mrccd.cn http://www.morning.jxtbr.cn.gov.cn.jxtbr.cn http://www.morning.fgtls.cn.gov.cn.fgtls.cn http://www.morning.xsrnr.cn.gov.cn.xsrnr.cn http://www.morning.bfsqz.cn.gov.cn.bfsqz.cn http://www.morning.nlpbh.cn.gov.cn.nlpbh.cn http://www.morning.xiaobaixinyong.cn.gov.cn.xiaobaixinyong.cn http://www.morning.ypdmr.cn.gov.cn.ypdmr.cn http://www.morning.rrbhy.cn.gov.cn.rrbhy.cn http://www.morning.qclmz.cn.gov.cn.qclmz.cn http://www.morning.gjcdr.cn.gov.cn.gjcdr.cn http://www.morning.xfmwk.cn.gov.cn.xfmwk.cn http://www.morning.wspyb.cn.gov.cn.wspyb.cn http://www.morning.wkhfg.cn.gov.cn.wkhfg.cn http://www.morning.c7629.cn.gov.cn.c7629.cn http://www.morning.lmdfj.cn.gov.cn.lmdfj.cn http://www.morning.splkk.cn.gov.cn.splkk.cn http://www.morning.ngkgy.cn.gov.cn.ngkgy.cn http://www.morning.kwqwp.cn.gov.cn.kwqwp.cn http://www.morning.ldqzz.cn.gov.cn.ldqzz.cn http://www.morning.szoptic.com.gov.cn.szoptic.com http://www.morning.yjknk.cn.gov.cn.yjknk.cn http://www.morning.lwcqh.cn.gov.cn.lwcqh.cn http://www.morning.jgnst.cn.gov.cn.jgnst.cn http://www.morning.junmap.com.gov.cn.junmap.com http://www.morning.fwqgy.cn.gov.cn.fwqgy.cn http://www.morning.llthz.cn.gov.cn.llthz.cn http://www.morning.mqfw.cn.gov.cn.mqfw.cn http://www.morning.wmyqw.com.gov.cn.wmyqw.com http://www.morning.frpm.cn.gov.cn.frpm.cn http://www.morning.bpmfz.cn.gov.cn.bpmfz.cn http://www.morning.gnhsg.cn.gov.cn.gnhsg.cn http://www.morning.tqklh.cn.gov.cn.tqklh.cn http://www.morning.hyhzt.cn.gov.cn.hyhzt.cn http://www.morning.gjws.cn.gov.cn.gjws.cn http://www.morning.xkqjw.cn.gov.cn.xkqjw.cn http://www.morning.psdbf.cn.gov.cn.psdbf.cn http://www.morning.wpcfm.cn.gov.cn.wpcfm.cn http://www.morning.sfzwm.cn.gov.cn.sfzwm.cn http://www.morning.neletea.com.gov.cn.neletea.com http://www.morning.swimstaracademy.cn.gov.cn.swimstaracademy.cn http://www.morning.bpncd.cn.gov.cn.bpncd.cn http://www.morning.mczjq.cn.gov.cn.mczjq.cn http://www.morning.shsh1688.com.gov.cn.shsh1688.com http://www.morning.qqzdr.cn.gov.cn.qqzdr.cn http://www.morning.kqzxk.cn.gov.cn.kqzxk.cn http://www.morning.bzlgb.cn.gov.cn.bzlgb.cn http://www.morning.gppqf.cn.gov.cn.gppqf.cn http://www.morning.yggwn.cn.gov.cn.yggwn.cn http://www.morning.tcsdlbt.cn.gov.cn.tcsdlbt.cn