前端导出页面PDF

server/2024/10/4 12:58:13/
import html2canvas from 'html2canvas'
import { jsPDF } from 'jspdf'
import { Loading } from 'element-ui'
let downloadLoadingInstance// 导出页面为PDF格式---使用插件html2canvas和jspdf插件
export function exportPDF(fileName, node) {downloadLoadingInstance = Loading.service({ text: 'データをダウンロード中です。しばらくお待ちください', spinner: 'el-icon-loading', background: 'rgba(0, 0, 0, 0.7)' })document.body.scrollTop = document.documentElement.scrollTop = 0var clientH = node.clientHeightnode.style.height = node.scrollHeight + 'px'// 把要导出的元素高度设置为滚动高度html2canvas(node, {allowTaint: true,useCORS: true,backgroundColor: '#142D42', // 如果指定的div没有设置背景色会默认成黑色,这里是个坑y: 0,ignoreElements: element => {if (element.tagName === 'BUTTON' ||typeof element.className !== 'string' ||(typeof element.className === 'string' && element.className.includes('sidebar-container')) ||(typeof element.className === 'string' && element.className.includes('navbar')) ||(typeof element.className === 'string' && element.className.includes('tags-view-container')) ||(typeof element.className === 'string' && element.className.includes('operation-bar')) ||element.tagName === 'svg') {return true}return false}}).then((canvas) => {// 未生成pdf的html页面高度var leftHeight = canvas.heightvar a4Width = 576var a4Height = 772.89 // A4大小,210mm x 297mm,四边各保留10mm的边距,显示区域190x277// 一页pdf显示html页面生成的canvas高度;var a4HeightRef = Math.floor((canvas.width / a4Width) * a4Height)// pdf页面偏移var position = 0var pageData = canvas.toDataURL('image/jpeg', 1.0)var pdf = new jsPDF('p', 'pt', 'a4') // A4纸,纵向// pdf.addFont('simkai-normal.ttf', 'simkai', 'normal')// pdf.setFont('simkai')// pdf.setFontSize(20)var canvas1 = document.createElement('canvas')var heightpdf.setDisplayMode('fullwidth', 'continuous', 'FullScreen')function createImpl(canvas) {if (leftHeight > 0) {var checkCount = 0if (leftHeight > a4HeightRef) {var i = position + a4HeightReffor (i = position + a4HeightRef; i >= position; i--) {var isWrite = truefor (var j = 0; j < canvas.width; j++) {var c = canvas.getContext('2d').getImageData(j, i, 1, 1).dataif (c[0] !== 0xff || c[1] !== 0xff || c[2] !== 0xff) {isWrite = falsebreak}}if (isWrite) {checkCount++if (checkCount >= 10) {break}} else {checkCount = 0}}height = Math.round(i - position) || Math.min(leftHeight, a4HeightRef)if (height <= 0) {height = a4HeightRef}} else {height = leftHeight}canvas1.width = canvas.widthcanvas1.height = heightvar ctx = canvas1.getContext('2d')ctx.drawImage(canvas,0,position,canvas.width,height,0,0,canvas.width,height)if (position !== 0) {pdf.addPage()}pdf.addImage(canvas1.toDataURL('image/jpeg', 1.0),'JPEG',10,40,a4Width,(a4Width / canvas1.width) * height)leftHeight -= heightposition += heightif (leftHeight > 0) {setTimeout(createImpl, 500, canvas)} else {pdf.save(fileName + '.pdf')// 下载完成后改变浏览器界面可视区域高度node.style.height = clientH + 'px'}}node.style.height = 'initial'downloadLoadingInstance.close()}// 当内容未超过pdf一页显示的范围,无需分页if (leftHeight < a4HeightRef) {pdf.addImage(pageData,'JPEG',10,40,a4Width,(a4Width / canvas.width) * leftHeight)pdf.save(fileName + '.pdf')// 下载完成后改变浏览器界面可视区域高度node.style.height = clientH + 'px'} else {try {pdf.deletePage(0)setTimeout(createImpl, 500, canvas)} catch (err) {console.log('err', err)}}node.style.height = 'initial'downloadLoadingInstance.close()})
}

使用的时候直接引入

import { exportPDF } from './exportPDF'

exportPdf() {if (this.drChange) {exportPDF(`[${this.wakuGroup.grpWkMei}]_[${this.nen}年${this.textSeason}]${this.$moment().format('YYYYMMDDHHmmss')}`, this.$refs.collapse.$el)} else {exportPDF(`[${this.wakuGroup.grpWkMei}]_[${this.nen}年${this.textSeason}]_${this.$moment().format('YYYYMMDDHHmmss')}`, this.$refs.collapse.$el)}},


http://www.ppmy.cn/server/126944.html

相关文章

ARM嵌入式学习--第一天

-ARM核介绍 -CPU核 CPU又叫中央处理器&#xff0c;其主要功能是进行算数运算和逻辑运算&#xff0c;内部结构大概可以分为控制单元&#xff0c;算术逻辑单元和储存单元等几个部分 -ARM核 工作模式&#xff1a; user mode:用户模式是用户程序的工作模式&#xff0c;他运行在操作…

北京数字孪生工业互联网可视化技术,赋能新型工业化智能制造工厂

随着北京数字孪生工业互联网可视化技术的深入应用&#xff0c;新型工业化智能制造工厂正逐步迈向智能化、高效化的全新阶段。这项技术不仅实现了物理工厂与数字世界的精准映射&#xff0c;更通过大数据分析、人工智能算法等先进手段&#xff0c;为生产流程优化、资源配置合理化…

Tomcat监控与调优:比Tomcat Manager更加强大的Psi-Probe

这是一款 Tomcat 管理和监控工具&#xff0c;前身是 Lambda Probe。由于 Lambda Probe 2006不再更新&#xff0c;所以 PSI Probe 算是对其的一个 Fork 版本并一直更新至今。 Probe psi-probe是在相同的开源许可证(GPLV2)下分发的社区驱动的 Lambda Probe &#xff0c;psi-pro…

深入理解网络通信: 长连接、短连接与WebSocket

在现代网络应用开发中,选择合适的通信方式对于应用的性能、效率和用户体验至关重要。本文将深入探讨三种常见的网络通信方式:长连接、短连接和WebSocket,分析它们的特点、区别以及适用场景。 1. 短连接 © ivwdcwso (ID: u012172506) 1.1 定义 短连接是指客户端和服务器…

基于Hive和Hadoop的用电量分析系统

本项目是一个基于大数据技术的用电量分析系统&#xff0c;旨在为用户提供全面的电力消耗信息和深入的用电量分析。系统采用 Hadoop 平台进行大规模数据存储和处理&#xff0c;利用 MapReduce 进行数据分析和处理&#xff0c;通过 Sqoop 实现数据的导入导出&#xff0c;以 Spark…

Unity3D播放GIF图片 插件播放

系列文章目录 unity工具 文章目录 系列文章目录👉前言👉一、效果图👉二、使用方法1.代码如下👉三、壁纸分享👉总结👉前言 unity播放gif图片,本身是不支持的,但是可以使用其他方法来实现,有一种使用System.Drawing来实现播放的,一种是让UI把图片导出成帧动画图片,…

推荐 uniapp 相对好用的海报生成插件

插件地址&#xff1a;自定义canvas样式海报 - DCloud 插件市场 兼容性也是不错的&#xff1a;

端侧Agent系列 | 端侧AI Agent任务拆解大师如何助力AI手机?(详解版)

引言 简介 Octo-planner 规划和执行Agent框架 规划数据集 基准设计 实验设计 结果 全量微调与LoRA 多LoRA训练与合并 不同基础模型的全量微调 不同数据集大小的全量微调 总结 实战 英文 中文示例1&#xff1a; 中文示例2&#xff1a; 0. 引言 人生到处知何似&a…