导出pdf

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

js 复制代码
import exportPDFMixin from '@/mixins/exportPDFMixin';
mixins: [exportPDFMixin],
html 复制代码
 <span v-show="showEdit" class="export textR" @click="exportPDF('pdfDom', '导出的pdf名称')">导出</span>
js 复制代码
//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.width
        let contentHeight = canvas.height
        let pageHeight = contentWidth / 592.28 * 841.89
        let leftHeight = contentHeight
        let position = 0
        let imgWidth = 595.28
        let imgHeight = 592.28 / contentWidth * contentHeight
        let 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 -= pageHeight
            position -= 841.89
            if (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
    },
  }
}
相关推荐
如果超人不会飞2 分钟前
TinyRobot SuggestionPopover智能建议弹出框组件
前端·vue.js
LiuJun2Son17 分钟前
Angular 快速入门:从零搭建你的第一个应用
前端·javascript·angular.js
烬羽18 分钟前
从零理解树与二叉树:用 JS 带你手撕遍历和递归
javascript·数据结构
小徐_233325 分钟前
Wot UI 2.1.0 发布:ConfigProvider 全局配置能力升级
前端·uni-app
方白羽26 分钟前
Vibe Coding 四个核心阶段
android·前端·app
奶油话梅糖26 分钟前
浏览器解析 HTML 头部的底层逻辑:从字节流到资源调度
前端·html
YHL27 分钟前
🚀从零理解树与二叉树 —— 概念、实现与遍历
前端·javascript·数据结构
小时前端28 分钟前
微前端技术选型深度分析:从概念到实践
前端
十九画生29 分钟前
学 JavaScript 数据类型,真正要搞懂的是:变量里存的到底是什么?
javascript
ZengLiangYi31 分钟前
测试策略:单元测试 + 集成测试怎么写
javascript·typescript·node.js