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

易思espcms企业网站管理系统/怎么做手工

易思espcms企业网站管理系统,怎么做手工,知名设计网站,青岛网站设计选哪家vue3将页面导出成PDF文件(完美解决图片、表格内容分割问题) 1、安装依赖2、在utils中创建htmlToPDF.js文件3、在vue中引入并使用 1、安装依赖 npm install --save html2canvas // 页面转图片 npm install jspdf --save // 图片转pdf2、在utils中创建h…

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

  • 1、安装依赖
  • 2、在utils中创建htmlToPDF.js文件
  • 3、在vue中引入并使用

1、安装依赖

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

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的边距,显示区域190x277var imgHeight = Math.floor(a4h * canvas.width / a4w) // 按A4显示比例换算一页图像的像素高度var renderedHeight = 0while (renderedHeight < canvas.height) {var page = document.createElement('canvas')page.width = canvas.widthpage.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 += imgHeightif (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') {  //判断元素是不是trnoTableHeight += 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}
}

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"  // 导出成PDFconst handleExport = (name) => {var fileName= '投资评审报告'const fileList = document.getElementsByClassName('pdfRef')   // 很重要htmlPdf(fileName, document.querySelector('#pdfRef'), fileList)}
</script>
http://www.rdtb.cn/news/394.html

相关文章:

  • 旅游公司网站开发与实现/网站推广方案策划书2000
  • 全国人大网站建设/百度关键词seo优化
  • 网站建设企业资质/chrome浏览器
  • 淘宝客网站推广工具/b2b电子商务平台网站
  • 做三级锅炉证模拟考试的网站/优化大师客服
  • asp.net网站开发文档/seo站内优化技巧
  • wordpress文章显示404/站长工具seo综合查询关键词
  • 毕业设计做b2c网站的意义/整合营销传播案例分析
  • 网站后台排版布局/中国公关公司前十名
  • 做数据权威的网站/重庆森林粤语
  • wordpress 采集小说/上海专业seo服务公司
  • 阿里云网站域名备案/推荐一个seo优化软件
  • wordpress存档显示文章所有内容/合肥seo管理
  • 开发什么网站好/zac seo博客
  • 临沭网站建设/深圳百度网站排名优化
  • 房地产推广方案和推广思路/谷歌seo需要做什么的
  • html5网站开发实战/西安网站建设方案优化
  • 代理公司注册网/seo托管公司
  • 秦皇岛海港区最新疫情/官网seo是什么意思
  • 凡科网站建设网站/优化师是一份怎样的工作
  • 郑州网站定制/汕头网站建设技术外包
  • 青岛知名网站建设多少钱/站点搜索
  • b2b网站推广方法/百家联盟推广部电话多少
  • 长春火车站附近美食/百度网页版链接
  • 网站建设分项报价表/花都网站建设公司
  • 安吉做网站/新网站秒收录技术
  • 天水建网站/电商网站有哪些
  • 美食网站建设的时间进度表/网络推广软文范文
  • 计算机网站建设的毕业论文/竞价代运营公司哪家好
  • 企业如何在工商网站上做公示/淘宝如何提升关键词排名