• vue3 如何将页面生成 pdf 导出


    前言

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

    步骤

    1.引入两个依赖

    1. npm i html2canvas
    2. npm i jspdf

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

    1. import html2canvas from 'html2canvas';
    2. import jsPDF from 'jspdf'
    3. export const htmlToPDF = async (htmlId: string, title: string = "标题", bgColor = "#fff") => {
    4. let pdfDom: HTMLElement | null = document.getElementById(htmlId) as HTMLElement
    5. pdfDom.style.padding = '0 10px !important'
    6. const A4Width = 595.28;
    7. const A4Height = 841.89;
    8. let canvas = await html2canvas(pdfDom, {
    9. scale: 2,
    10. useCORS: true,
    11. backgroundColor: bgColor,
    12. });
    13. let pageHeight = (canvas.width / A4Width) * A4Height;
    14. let leftHeight = canvas.height;
    15. let position = 0;
    16. let imgWidth = A4Width;
    17. let imgHeight = (A4Width / canvas.width) * canvas.height;
    18. /*
    19. 根据自身业务需求 是否在此处键入下方水印代码
    20. */
    21. let pageData = canvas.toDataURL("image/jpeg", 1.0);
    22. let PDF = new jsPDF("p", 'pt', 'a4');
    23. if (leftHeight < pageHeight) {
    24. PDF.addImage(pageData, "JPEG", 0, 0, imgWidth, imgHeight);
    25. } else {
    26. while (leftHeight > 0) {
    27. PDF.addImage(pageData, "JPEG", 0, position, imgWidth, imgHeight);
    28. leftHeight -= pageHeight;
    29. position -= A4Height;
    30. if (leftHeight > 0) PDF.addPage();
    31. }
    32. }
    33. PDF.save(title + ".pdf");
    34. }

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

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

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

    1. <script lang="ts">
    2. import { defineComponent, getCurrentInstance } from "vue";
    3. import TestPinia from "./components/TestPinia.vue";
    4. import { htmlToPDF } from '@/utils/html2pdf'
    5. export default defineComponent({
    6. name: "App",
    7. components: {
    8. TestPinia,
    9. },
    10. setup() {
    11. return {
    12. htmlToPDF,
    13. }
    14. },
    15. });
    16. script>
    17. <style>
    18. #app {
    19. font-family: Avenir, Helvetica, Arial, sans-serif;
    20. -webkit-font-smoothing: antialiased;
    21. -moz-osx-font-smoothing: grayscale;
    22. text-align: center;
    23. color: #2c3e50;
    24. margin-top: 60px;
    25. }
    26. style>

    页面

    image.png


    导出的效果图

    image.png

    若有收获,就点个赞吧

  • 相关阅读:
    Node.js 是怎么找到模块的?
    更直观地学习 Git 命令
    【AI设计模式】机器学习设计模式概述
    App出海起量难?传参安装打开获客增长新途径
    Flex布局和Float布局的实现
    Oracle 运维篇+应用容器数据库的install、upgrade、patch、uninstall
    ppt添加圆角矩形,并调整圆角弧度方法
    Vue项目中组件相互引用,组件不能正常注册,控制台警告处理
    Presto 聚合中groupBy分组的实现
    IDENTITY_INSERT 设置为 OFF 时,不能为表 ‘t_user‘ 中的标识列插入显式值
  • 原文地址:https://blog.csdn.net/weixin_42125732/article/details/133607838