vue3 如何将页面生成 pdf 导出

前言

最近工作中有需要将一些前端页面(如报表页面等)导出为pdf的需求,博主采用的是html2Canvas + jspdf。

步骤

1.引入两个依赖

javascript 复制代码
npm i html2canvas
npm i jspdf

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

javascript 复制代码
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 HTMLElement
  pdfDom.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加上水印,则添加下面这段代码:

javascript 复制代码
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.在目标页面引入方法即可

javascript 复制代码
<template>
  <div id="test-id">
  <TestPinia></TestPinia>
  </div>
  <button @click="()=>htmlToPDF('test-id','test pdf')">导出</button>
  </template>

  <script lang="ts">
  import { defineComponent, getCurrentInstance } from "vue";

import TestPinia from "./components/TestPinia.vue";
import { htmlToPDF } from '@/utils/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>

页面

导出的效果图

若有收获,就点个赞吧

相关推荐
非凡ghost15 小时前
Xournal++(PDF文档注释工具) 中文绿色版
学习·pdf·生活·软件需求
MonkeyKing_sunyuhua15 小时前
使用ARQ做PDF OCR和 图片OCR的任务的方案
pdf·ocr
2501_9307077817 小时前
如何使用C#代码在 PDF 中添加或删除附件
pdf
m5655bj17 小时前
如何使用 Python 调整 PDF 页面顺序?
python·pdf
YuanYWRS18 小时前
办公基础:实现PDF中表单不改变格式的情况下转成excel
pdf·excel
E_ICEBLUE2 天前
PDF vs PDF/A:区别、场景与常用转换方法(2025 全面解读)
python·pdf
TextIn智能文档云平台2 天前
从散乱资料到智能知识库:基于TextIn与Coze的RAG实战
人工智能·pdf·知识库·rag·coze·文档解析
m0_564914922 天前
图片怎么快速转为PDF?PDF怎么快速转为图片?
pdf
2501_930707782 天前
如何使用C#代码将多张图片整合为一个PDF文档
开发语言·pdf·c#
拓端研究室2 天前
赢战2025电商新趋势下的增长策略报告:平台格局、跨境趋势、大促消费|附600+份报告PDF、数据、可视化模板汇总下载
大数据·pdf