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

vue3实现导出pdf、png功能

准备做的系统中出现了 想导出当前页面的png或者pdf设计数据较多后端做可能比较麻烦 就自己研究了一下

1、安装html2canvas 、jspdf包

npm install --save html2canvas   // 可以将dom元素转为一张图片
npm install --save jspdf   // 导出为PDF格式

2、vue组件中引用,代码如下:

<template><div class="content"><a-button @click="exportPNG" size="small" type="primary">导出PNG</a-button><a-button @click="exportPDF" size="small" type="primary">导出PDF</a-button><div id="main-charts">需要截取的内容区域我想测试导出是否可行</div></div>
</template>

3、导出png

<script lang="ts" setup>// 引入插件import html2canvas from 'html2canvas';import jsPDF from 'jspdf';// 导出pngconst exportPNG = () => {const ele: HTMLElement | null = document.getElementById('main-charts');html2canvas(ele as HTMLElement).then((canvas: any) => {const contentWidth = canvas.width;const contentHeight = canvas.height;const ctx: any = canvas.getContext('2d');// 添加水印ctx.textAlign = 'center';ctx.textBaseline = 'middle';ctx.rotate((25 * Math.PI) / 180);ctx.font = '20px Microsoft Yahei';ctx.fillStyle = 'rgba(184, 184, 184, 0.8)';for (let i = contentWidth * -1; i < contentWidth; i += 240) {for (let j = contentHeight * -1; j < contentHeight; j += 100) {// 填充文字,x 间距, y 间距ctx.fillText('水印名', i, j);}}const imgUrl = canvas.toDataURL('image/png');const tempLink = document.createElement('a'); // 创建一个a标签tempLink.style.display = 'none';tempLink.href = imgUrl;tempLink.setAttribute('download', '文件名'); // 给a标签添加下载属性if (typeof tempLink.download === 'undefined') {tempLink.setAttribute('target', '_blank');}document.body.appendChild(tempLink); // 将a标签添加到body当中tempLink.click(); // 启动下载document.body.removeChild(tempLink); // 下载完毕删除a标签window.URL.revokeObjectURL(imgUrl);})}
</script>

4、导出pdf

<script lang="ts" setup>// 引入插件import html2canvas from 'html2canvas';import jsPDF from 'jspdf';const exportPDF = () => {const ele: HTMLElement | null = document.getElementById('main-charts');html2canvas(ele as HTMLElement, {dpi: 96, // 分辨率scale: 2, // 设置缩放useCORS: true, // 允许canvas画布内 可以跨域请求外部链接图片, 允许跨域请求。,// backgroundColor:'#ffffff',这样背景还是黑的bgcolor: '#ffffff', // 应该这样写logging: false, // 打印日志用的 可以不加默认为false}).then((canvas) => {const contentWidth = canvas.width;const contentHeight = canvas.height;// 一页pdf显示html页面生成的canvas高度;const pageHeight = (contentWidth / 592.28) * 841.89;// 未生成pdf的html页面高度let leftHeight = contentHeight;// 页面偏移let position = 0;// a4纸的尺寸[595.28,841.89],html页面生成的canvas在pdf中图片的宽高const imgWidth = 595.28;const imgHeight = (595.28 / contentWidth) * contentHeight;const ctx: any = canvas.getContext('2d');// 添加水印ctx.textAlign = 'center';ctx.textBaseline = 'middle';ctx.rotate((25 * Math.PI) / 180);ctx.font = '20px Microsoft Yahei';ctx.fillStyle = 'rgba(184, 184, 184, 0.8)';for (let i = contentWidth * -1; i < contentWidth; i += 240) {for (let j = contentHeight * -1; j < contentHeight; j += 100) {// 填充文字,x 间距, y 间距ctx.fillText('水印名', i, j);}}const pageData = canvas.toDataURL('image/jpeg', 1.0);const pdf = new jsPDF('', 'pt', 'a4');if (leftHeight < pageHeight) {// 在pdf.addImage(pageData, 'JPEG', 左,上,宽度,高度)设置在pdf中显示;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(`文件名.pdf`);});}
</script>

http://www.lryc.cn/news/335799.html

相关文章:

  • what is tty?
  • 在vite中限制node版本
  • 07 Php学习:运算符
  • 做了多年前端,有没有想在python,go,nodejs,.net,java,c++中学一门后端,推荐
  • JR-SMD201-P便携式网络解码器
  • 线程池阻塞队列的选择
  • linux内核驱动-在内核代码里添加设备结点
  • 【算法优选】 动态规划之简单多状态dp问题——贰
  • 【算法刷题 | 二叉树 06】4.10( 路径总和、路径总和 || )
  • 代码学习记录37----动态规划
  • Spring Boot:Web开发之三大组件的整合
  • 2024.3.15力扣每日一题——卖木头块
  • vue快速入门(七)内联语句
  • Docker实战教程 第2章 Docker基础
  • 【S32K3 MCAL配置】-3.2-CANFD配置-发送“经典CAN/CANFD标准帧“和“经典CAN/CANFD扩展帧“(基于MCAL+FreeRTOS)
  • 【airtest】自动化入门教程(四)Poco元素定位
  • Go语言中如何处理goroutine和循环变量
  • Pytest教程:一文了解如何使用 pytest_runtest_makereport 修改 Pytest 测试报告内容
  • 《高通量测序技术》分享,生物信息学生信流程的性能验证,以肿瘤NGS基因检测为例。
  • Django+Celery框架自动化定时任务开发
  • 解决element-plus table组件 fixed=“right“(left)浮动后横向滚动文字穿透的问题
  • 【opencv】示例-distrans.cpp 距离变换
  • LVGL V8 代码细读——极致的链表使用
  • 蓝桥杯第十二届c++大学B组详解
  • Tubi 十岁啦!
  • Qt C++ 实现文件监视源码
  • 蓝桥杯第十一届c++大学B组详解
  • 大模型日报2024-04-10
  • redis修改协议改了,有哪些替代品?
  • 《QT实用小工具·十六》IP地址输入框控件