• vue3将页面导出成PDF文件(完美解决图片、表格内容分割问题)


    vue3将页面导出成PDF文件(完美解决图片、表格内容分割问题)

    1、安装依赖

    npm install --save html2canvas  // 页面转图片
    npm install jspdf --save  // 图片转pdf
    
    • 1
    • 2

    2、在utils中创建htmlToPDF.js文件

    // 页面导出为pdf格式 //title表示为下载的标题,html表示document.querySelector('#myPrintHtml')
    import html2Canvas from 'html2canvas'
    import JsPDF from 'jspdf'
    var noTableHeight = 0 //table外的元素高度
    
    export const htmlPdf = (title, html, fileList, type) => {// type传有效值pdf则为横版
      if (fileList) {
        const pageHeight = Math.floor(277 * html.scrollWidth / 190) + 20 //计算pdf高度
        for (let i = 0; i < fileList.length; i++) { //循环获取的元素
          const multiple = Math.ceil((fileList[i].offsetTop + fileList[i].offsetHeight) / pageHeight) //元素的高度
          if (isSplit(fileList, i, multiple * pageHeight)) { //计算是否超出一页
            var _H = '' //向pdf插入空白块的内容高度
            if (fileList[i].localName !== 'tr') { //判断是不是表格里的内容
              _H = multiple * pageHeight - (fileList[i].offsetTop + fileList[i].offsetHeight)
            } else {
              _H = multiple * pageHeight - (fileList[i].offsetTop + fileList[i].offsetHeight + noTableHeight) + 20
            }
            var newNode = getFooterElement(_H)  //向pdf插入空白块的内容
            const divParent = fileList[i].parentNode // 获取该div的父节点
            const next = fileList[i].nextSibling // 获取div的下一个兄弟节点
            // 判断兄弟节点是否存在
            if (next) {
              // 存在则将新节点插入到div的下一个兄弟节点之前,即div之后
              divParent.insertBefore(newNode, next)
            } else {
              // 否则向节点添加最后一个子节点
              divParent.appendChild(newNode)
            }
          }
        }
      }
      html2Canvas(html, {
        allowTaint: false,
        taintTest: false,
        logging: false,
        useCORS: true,
        dpi: window.devicePixelRatio * 1,
        scale: 1 // 按比例增加分辨率
      }).then(canvas => {
        var pdf = new JsPDF('p', 'mm', 'a4') // A4纸,纵向
        var ctx = canvas.getContext('2d')
        var a4w = type ? 277 : 190; var a4h = type ? 190 : 277 // A4大小,210mm x 297mm,四边各保留10mm的边距,显示区域190x277
        var imgHeight = Math.floor(a4h * canvas.width / a4w) // 按A4显示比例换算一页图像的像素高度
        var renderedHeight = 0
        while (renderedHeight < canvas.height) {
          var page = document.createElement('canvas')
          page.width = canvas.width
          page.height = Math.min(imgHeight, canvas.height - renderedHeight)// 可能内容不足一页
    
          // 用getImageData剪裁指定区域,并画到前面创建的canvas对象中
          page.getContext('2d').putImageData(ctx.getImageData(0, renderedHeight, canvas.width, Math.min(imgHeight, canvas.height - renderedHeight)), 0, 0)
          pdf.addImage(page.toDataURL('image/jpeg', 1.0), 'JPEG', 10, 10, a4w, Math.min(a4h, a4w * page.height / page.width)) // 添加图像到页面,保留10mm边距
    
          renderedHeight += imgHeight
          if (renderedHeight < canvas.height) {
            pdf.addPage()// 如果后面还有内容,添加一个空页
          }
          // delete page;
        }
        // 保存文件
        pdf.save(title + '.pdf')
      })
    }
    // pdf截断需要一个空白位置来补充
    const getFooterElement = (remainingHeight, fillingHeight = 0) => {
      const newNode = document.createElement('div')
      newNode.style.background = '#ffffff'
      newNode.style.width = 'calc(100% + 8px)'
      newNode.style.marginLeft = '-4px'
      newNode.style.marginBottom = '0px'
      newNode.classList.add('divRemove')
      newNode.style.height = (remainingHeight + fillingHeight) + 'px'
      return newNode
    }
    const isSplit = (nodes, index, pageHeight) => {
      // 判断是不是tr 如果不是高度存起来
      // 表格里的内容要特殊处理
      // tr.offsetTop 是tr到table表格的高度
      // 所以计算高速时候要把表格外的高度加起来
      // 生成的pdf没有表格了这里可以不做处理 直接计算就行
      if (nodes[index].localName !== 'tr') {  //判断元素是不是tr
        noTableHeight += nodes[index].clientHeight
      }
    
      if (nodes[index].localName !== 'tr') {
        return nodes[index].offsetTop + nodes[index].offsetHeight < pageHeight && nodes[index + 1] && nodes[index + 1].offsetTop + nodes[index + 1].offsetHeight > pageHeight
      } else {
        return nodes[index].offsetTop + nodes[index].offsetHeight + noTableHeight < pageHeight && nodes[index + 1] && nodes[index + 1].offsetTop + nodes[index + 1].offsetHeight + noTableHeight > pageHeight
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90

    3、在vue中引入并使用

    <template>
    	<div>
        	<button class="primary-btn" @click="handleExport">导出</button>
            <div class="check-wrapper" id="pdfRef">
          		<div class="page1-box pdfRef">11</div>
          		<div class="page2-box pdfRef">11</div>
          		<div class="page3-box pdfRef">11</div>
          		<div class="page4-box pdfRef">11</div>
          		<div class="page5-box pdfRef">11</div>
          		<div class="page6-box pdfRef">11</div>
          		<div class="page7-box pdfRef">11</div>
          		<div class="page8-box pdfRef">11</div>
          	</div>
    	</div>
    </template>
    <script setup>
    	// 引入方法
    	import { htmlPdf } from "@/utils/htmlToPDF.js"  
    	// 导出成PDF
    	const handleExport = (name) => {
    	  var fileName= '投资评审报告'
    	  const fileList = document.getElementsByClassName('pdfRef')   // 很重要
    	  htmlPdf(fileName, document.querySelector('#pdfRef'), fileList)
    	}
    </script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
  • 相关阅读:
    Greenplum报错could not fork new process for connection: Cannot allocate memory
    C++11 基础知识
    【毕业设计】基于单片机的智能鱼缸系统设计与实现 - 嵌入式 物联网 stm32 c51
    python基础06——控制流语句:顺序、分支、循环
    【GUI】-- 09 JComboBox & JList、JTextField & JPasswordField & JTextArea
    FastDFS-01-单机和集群搭建
    this is incompatible with sql_mode=only_full_group_by
    (免费领源码)Java&springboo&Mysql儿童疫苗预约系统 12222-计算机毕业设计
    从Core Dump中提取CUDA的报错信息
    Go常用命令
  • 原文地址:https://blog.csdn.net/DZQ1223/article/details/132852160