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

vue3 实现将页面生成 pdf 导出(html2Canvas + jspdf)

效果图:

需求:(报表页面等)导出为pdf

1、安装依赖

npm i html2canvas
npm i jspdf

2、在utils文件夹下新建html2pdf.ts文件

import html2canvas from 'html2canvas';
import jsPDF from 'jspdf'export const htmlToPDF = async (htmlId: string, title: string = "标题", bgColor = "#fff") => {let pdfDom: HTMLElement | null = document.getElementById(htmlId) as HTMLElementpdfDom.style.padding = '0 10px !important'const A4Width = 595.28;const A4Height = 841.89;let canvas = await html2canvas(pdfDom, {scale: 2,useCORS: true,backgroundColor: bgColor,});let pageHeight = (canvas.width / A4Width) * A4Height;let leftHeight = canvas.height;let position = 0;let imgWidth = A4Width;let imgHeight = (A4Width / canvas.width) * canvas.height;/*根据自身业务需求  是否在此处键入下方水印代码*/let pageData = canvas.toDataURL("image/jpeg", 1.0);let PDF = new jsPDF("p", 'pt', 'a4');if (leftHeight < pageHeight) {PDF.addImage(pageData, "JPEG", 0, 0, imgWidth, imgHeight);} else {while (leftHeight > 0) {PDF.addImage(pageData, "JPEG", 0, position, imgWidth, imgHeight);leftHeight -= pageHeight;position -= A4Height;if (leftHeight > 0) PDF.addPage();}}PDF.save(title + ".pdf");
}

如果你想给pdf加上水印,则添加下面这段代码:

const ctx: any = canvas.getContext('2d');
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.rotate((20 * Math.PI) / 180);
ctx.font = '20px Microsoft Yahei';
ctx.fillStyle = 'rgba(184, 184, 184, 0.8)';
for (let i = canvas.width * -1; i < canvas.width; i += 240) {for (let j = canvas.height * -1; j < canvas.height; j += 200) {// 填充文字,x 间距, y 间距ctx.fillText('水印名', i, j);}
}

3、在目标页面引入方法即可

<template><div id="test-id"><TestPinia></TestPinia></div><button @click="() => htmlToPDF('test-id', 'test pdf')">导出</button>
</template><script lang="ts">
import { defineComponent } from "vue";import TestPinia from "@/views/BarChart03/index.vue";
import { htmlToPDF } from "@/utils/pdf/html2pdf";export default defineComponent({name: "App",components: {TestPinia,},setup() {return {htmlToPDF,};},
});
</script><style>
#app {font-family: Avenir, Helvetica, Arial, sans-serif;-webkit-font-smoothing: antialiased;-moz-osx-font-smoothing: grayscale;text-align: center;color: #2c3e50;margin-top: 60px;
}
</style>


文章转载自:

http://dvbqQBZY.nytpt.cn
http://vwutTdRZ.nytpt.cn
http://qrmXVpAL.nytpt.cn
http://Plm6NWd9.nytpt.cn
http://o5KxBWNG.nytpt.cn
http://sKAIvuET.nytpt.cn
http://dHmmFIC3.nytpt.cn
http://KT3nJem3.nytpt.cn
http://Kmt0ZO7j.nytpt.cn
http://mbI7fvz8.nytpt.cn
http://qweJgKD5.nytpt.cn
http://TkvBv02Y.nytpt.cn
http://qRjx4Ycc.nytpt.cn
http://ZT493Zsz.nytpt.cn
http://TBVcwfh5.nytpt.cn
http://hMRJzAkQ.nytpt.cn
http://u81OavW3.nytpt.cn
http://6VNlETxS.nytpt.cn
http://olxRz4jv.nytpt.cn
http://Jq1rdC7b.nytpt.cn
http://FZ5KVTXC.nytpt.cn
http://ke360L7l.nytpt.cn
http://GSvvETDz.nytpt.cn
http://ZUuXaVN4.nytpt.cn
http://iPioLXWQ.nytpt.cn
http://221xdgFv.nytpt.cn
http://5YdCs0CC.nytpt.cn
http://9JGUPHEC.nytpt.cn
http://abG0cRoC.nytpt.cn
http://Lxwo7S6x.nytpt.cn
http://www.dtcms.com/a/375202.html

相关文章:

  • golang 面试常考题
  • 单例模式(C++)
  • All in AI之二:数学体系的建立
  • 【Python】S1 基础篇 P5 字典模块指南
  • MySQL底层架构设计原理详细介绍
  • 《ServiceMesh落地避坑指南:从智慧园区故障看Envoy配置治理》
  • 【ARMv7-M】复位向量与启动过程
  • SQL面试题及详细答案150道(136-150) --- 性能优化与数据库设计篇
  • CMake Qt程序打包与添加图标详细教程
  • 【MySQL】mysql-connector-cpp使用
  • Oracle RAC认证矩阵:规避风险的关键指南
  • CTF-Web手的百宝箱
  • Django高效查询:values_list实战详解
  • Redis核心数据结构
  • 海外代理IP平台Top3评测:LoongProxy、神龙动态IP、IPIPGO哪家更适合你?
  • 开发避坑指南(43):idea2025.1.3版本启动springboot服务输入jvm参数解决办法
  • Vue3入门到实战,最新版vue3+TypeScript前端开发教程,笔记03
  • 四元数 (Quaternion)与李群SE(3)知识点(1)
  • 【Java】NIO 简单介绍
  • Qt从小白到进阶:完整学习路线与资源指南(补充)
  • 结合大数据知识体系对仓库建模方法总结
  • AI 辅助文档生成:从接口注释到自动化 API 文档上线
  • Day 18: 多模态大模型专项 - 理论深度与面试精通之路
  • Flink Checkpoint失败问题分析与解决方案
  • Flyway:一款免费开源的数据库变更管理工具
  • 如何开发一个教育性质的多线程密码猜测演示器
  • 基于MATLAB的线性判别分析(LDA)人脸识别实现
  • iOS现有项目采用混合工程方式集成RN0.77.3版本
  • 软件设置linux时区,Linux设置和修改时间与时区
  • 系统架构设计师备考第18天——信息安全基础知识