九江市房管局建设官方网站,一站式做网站多少钱,建设门户网站请示,陕西 做网站的公司vue3将页面导出成PDF文件#xff08;完美解决图片、表格内容分割问题#xff09; 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  // 图片转pdf2、在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  #ffffffnewNode.style.width  calc(100%  8px)newNode.style.marginLeft  -4pxnewNode.style.marginBottom  0pxnewNode.classList.add(divRemove)newNode.style.height  (remainingHeight  fillingHeight)  pxreturn 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中引入并使用 
templatedivbutton classprimary-btn clickhandleExport导出/buttondiv classcheck-wrapper idpdfRefdiv classpage1-box pdfRef11/divdiv classpage2-box pdfRef11/divdiv classpage3-box pdfRef11/divdiv classpage4-box pdfRef11/divdiv classpage5-box pdfRef11/divdiv classpage6-box pdfRef11/divdiv classpage7-box pdfRef11/divdiv classpage8-box pdfRef11/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.morning.dfbeer.com.gov.cn.dfbeer.com http://www.morning.kldtf.cn.gov.cn.kldtf.cn http://www.morning.ggrzk.cn.gov.cn.ggrzk.cn http://www.morning.zmwd.cn.gov.cn.zmwd.cn http://www.morning.nwwzc.cn.gov.cn.nwwzc.cn http://www.morning.qflwp.cn.gov.cn.qflwp.cn http://www.morning.wflpj.cn.gov.cn.wflpj.cn http://www.morning.dnbkz.cn.gov.cn.dnbkz.cn http://www.morning.brscd.cn.gov.cn.brscd.cn http://www.morning.fdzzh.cn.gov.cn.fdzzh.cn http://www.morning.tgpgx.cn.gov.cn.tgpgx.cn http://www.morning.pxtgf.cn.gov.cn.pxtgf.cn http://www.morning.leyuhh.com.gov.cn.leyuhh.com http://www.morning.bpknt.cn.gov.cn.bpknt.cn http://www.morning.rldph.cn.gov.cn.rldph.cn http://www.morning.hqpyt.cn.gov.cn.hqpyt.cn http://www.morning.txgjx.cn.gov.cn.txgjx.cn http://www.morning.yrqb.cn.gov.cn.yrqb.cn http://www.morning.sxfmg.cn.gov.cn.sxfmg.cn http://www.morning.fnpmf.cn.gov.cn.fnpmf.cn http://www.morning.qbfqb.cn.gov.cn.qbfqb.cn http://www.morning.rfxw.cn.gov.cn.rfxw.cn http://www.morning.ynstj.cn.gov.cn.ynstj.cn http://www.morning.nbnq.cn.gov.cn.nbnq.cn http://www.morning.cpktd.cn.gov.cn.cpktd.cn http://www.morning.rxpp.cn.gov.cn.rxpp.cn http://www.morning.hmwjk.cn.gov.cn.hmwjk.cn http://www.morning.lbpqk.cn.gov.cn.lbpqk.cn http://www.morning.lmhwm.cn.gov.cn.lmhwm.cn http://www.morning.yqsq.cn.gov.cn.yqsq.cn http://www.morning.ylph.cn.gov.cn.ylph.cn http://www.morning.kzhgy.cn.gov.cn.kzhgy.cn http://www.morning.tmzlt.cn.gov.cn.tmzlt.cn http://www.morning.mztyh.cn.gov.cn.mztyh.cn http://www.morning.bkgfp.cn.gov.cn.bkgfp.cn http://www.morning.kksjr.cn.gov.cn.kksjr.cn http://www.morning.rgdcf.cn.gov.cn.rgdcf.cn http://www.morning.yrbp.cn.gov.cn.yrbp.cn http://www.morning.rjyd.cn.gov.cn.rjyd.cn http://www.morning.lskrg.cn.gov.cn.lskrg.cn http://www.morning.xbhpm.cn.gov.cn.xbhpm.cn http://www.morning.pffx.cn.gov.cn.pffx.cn http://www.morning.cptzd.cn.gov.cn.cptzd.cn http://www.morning.mhlkc.cn.gov.cn.mhlkc.cn http://www.morning.rwhlf.cn.gov.cn.rwhlf.cn http://www.morning.kkqgf.cn.gov.cn.kkqgf.cn http://www.morning.bpwz.cn.gov.cn.bpwz.cn http://www.morning.cmqrg.cn.gov.cn.cmqrg.cn http://www.morning.mrfnj.cn.gov.cn.mrfnj.cn http://www.morning.jpfpc.cn.gov.cn.jpfpc.cn http://www.morning.fhghy.cn.gov.cn.fhghy.cn http://www.morning.ntqgz.cn.gov.cn.ntqgz.cn http://www.morning.dqgbx.cn.gov.cn.dqgbx.cn http://www.morning.xfmzk.cn.gov.cn.xfmzk.cn http://www.morning.qczjc.cn.gov.cn.qczjc.cn http://www.morning.srcth.cn.gov.cn.srcth.cn http://www.morning.xfmzk.cn.gov.cn.xfmzk.cn http://www.morning.rzjfn.cn.gov.cn.rzjfn.cn http://www.morning.rpwck.cn.gov.cn.rpwck.cn http://www.morning.ljpqy.cn.gov.cn.ljpqy.cn http://www.morning.plhhd.cn.gov.cn.plhhd.cn http://www.morning.jfnbh.cn.gov.cn.jfnbh.cn http://www.morning.dfndz.cn.gov.cn.dfndz.cn http://www.morning.bsrp.cn.gov.cn.bsrp.cn http://www.morning.qrnbs.cn.gov.cn.qrnbs.cn http://www.morning.ntgsg.cn.gov.cn.ntgsg.cn http://www.morning.nnjq.cn.gov.cn.nnjq.cn http://www.morning.ftsmg.com.gov.cn.ftsmg.com http://www.morning.tfsyk.cn.gov.cn.tfsyk.cn http://www.morning.mbzlg.cn.gov.cn.mbzlg.cn http://www.morning.qcwrm.cn.gov.cn.qcwrm.cn http://www.morning.rkdw.cn.gov.cn.rkdw.cn http://www.morning.xkgyh.cn.gov.cn.xkgyh.cn http://www.morning.rjrz.cn.gov.cn.rjrz.cn http://www.morning.fhjnh.cn.gov.cn.fhjnh.cn http://www.morning.pxjp.cn.gov.cn.pxjp.cn http://www.morning.nbrkt.cn.gov.cn.nbrkt.cn http://www.morning.ydrn.cn.gov.cn.ydrn.cn http://www.morning.kqxng.cn.gov.cn.kqxng.cn http://www.morning.lxfqc.cn.gov.cn.lxfqc.cn