当前位置: 首页 > news >正文

导出pdf

该方法导出的pdf大小是A4纸的尺寸,如果大于1页需要根据元素高度进行截断的话,页面元素需要加 class ergodic-dom,方法里面会获取ergodic-dom元素,对元素高度和A4高度做比较,如果大于A4高度,会塞一个空白元素,确保每一个元素在换页的时候不会被分割。

import exportPDFMixin from '@/mixins/exportPDFMixin';
mixins: [exportPDFMixin],
 <span v-show="showEdit" class="export textR" @click="exportPDF('pdfDom', '导出的pdf名称')">导出</span>
//exportPDFMixin.js
import html2Canvas from 'html2canvas';
import JsPDF from 'jspdf';
export default {data() {return {showEdit: true}},methods: {exportPDF(elId, title = "pdf") {this.showEdit = false;this.$nextTick(() => {this.downloadPDF(elId, title);})},downloadPDF(elId, title) {html2Canvas(document.querySelector(`#${elId}`), {allowTaint: true,useCORS: true,onclone: (documentclone) => {this.formatNode(documentclone);}}).then((canvas) => {let contentWidth = canvas.widthlet contentHeight = canvas.heightlet pageHeight = contentWidth / 592.28 * 841.89let leftHeight = contentHeightlet position = 0let imgWidth = 595.28let imgHeight = 592.28 / contentWidth * contentHeightlet pageData = canvas.toDataURL('image/jpeg', 1.0)let PDF = new JsPDF('', 'pt', 'a4')if (leftHeight < pageHeight) {PDF.addImage(pageData, 'JPEG', 0, 0, imgWidth, imgHeight)} else {while (leftHeight > 0) {PDF.addImage(pageData, 'JPEG', 0, position, imgWidth, imgHeight)leftHeight -= pageHeightposition -= 841.89if (leftHeight > 0) {PDF.addPage()}}}PDF.save(title + '.pdf');this.showEdit = true;})},formatNode(documentclone) {let pageHeight = documentclone.querySelector("#pdfDom").scrollWidth / 592.28 * 841.89;let lableListID = documentclone.getElementsByClassName('ergodic-dom');for (let i = 0; i < lableListID.length; i++) {let multiple = Math.ceil((lableListID[i].offsetTop + lableListID[i].offsetHeight) / pageHeight)if (this.isSplit(lableListID, i, multiple * pageHeight)) {let divParent = lableListID[i].parentNode // 获取该div的父节点let _H = multiple * pageHeight - (lableListID[i].offsetTop + lableListID[i].offsetHeight)let newNode = this.getFooterElement(_H)let next = lableListID[i].nextSibling // 获取div的下一个兄弟节点// 判断兄弟节点是否存在if (next) {// 存在则将新节点插入到div的下一个兄弟节点之前,即div之后divParent.insertBefore(newNode, next)} else {// 不存在则直接添加到最后,appendChild默认添加到divParent的最后divParent.appendChild(newNode)}}}},getFooterElement (remainingHeight, fillingHeight = 85) {let newNode = document.createElement('div')newNode.style.background = '#fff'newNode.style.width = 'calc(100% + 8px)'newNode.style.marginLeft = '-4px'newNode.style.marginBottom = '0px'newNode.style.height = (remainingHeight + fillingHeight) + 'px' // pdf截断需要一个空白位置return newNode},isSplit (nodes, index, pageHeight) {return nodes[index].offsetTop + nodes[index].offsetHeight < pageHeight && nodes[index + 1] && nodes[index + 1].offsetTop + nodes[index + 1].offsetHeight > pageHeight},}
}
http://www.lryc.cn/news/135499.html

相关文章:

  • 【考研数学】线形代数第三章——向量 | 基本概念、向量组的相关性与线性表示
  • 温故知新之:接口和抽象类有什么区别?
  • 回归预测 | MATLAB实现SSA-RF麻雀搜索优化算法优化随机森林算法多输入单输出回归预测(多指标,多图)
  • 文旅景区vr体验馆游乐场vr项目是什么
  • Vue Element upload组件和Iview upload 组件上传文件
  • 无涯教程-PHP - File 函数
  • elasticsearch 常用查询 7.4 版本
  • ChatGpt 从入门到精通
  • vscode远程调试
  • Vue3 数据响应式原理
  • 2023.08.20 学习周报
  • 软件测试技术之单元测试—工程师 Style 的测试方法(2)
  • 项目中超图 for openlayer和超图for cesium同时引入的问题
  • 3D与沉浸式技术,如何助力企业数字化转型?
  • excel vba 将多张数据表的内容合并到一张数据表
  • 接口和抽象类的区别?解析接口和抽象类的特点和用法
  • vscode-vue项目格式化
  • SAP MM学习笔记26- SAP中 振替转记(转移过账)和 在库转送(库存转储)1- 移动Type间振替转记
  • SAP SPL(Special Ledger)之注释行项目-Noted Items
  • 学习平台助力职场发展与提升
  • 有没有免费格式转换工具推荐?PDF转化为PPT的方法
  • 【LeetCode-经典面试150题-day12】
  • TCP机制-延迟应答,捎带应答
  • 【Redis从头学-8】Redis中的ZSet数据类型实战场景之用户积分榜
  • Springboot内嵌SQLite配置使用
  • 【微服务学习笔记】认识微服务
  • 基于Android R快速编译recovery-ramdisk.img
  • Redis分布式缓存
  • 最大公约数和最小公倍数
  • 数据结构——二叉搜索树(附带C++实现版本)