码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 网页vue3导出pdf


    需求:统计页面导出pdf;
    利用插件:html2canvas和jspdf库;
    原理就是:我们把需要转换的元素【例如一个div元素】使用html2canvas库将它转换为一个canvas对象,使用jspdf库创建PDF文档,用addImage方法将图像添加到PDF文件中 最后使用save方法将PDF文件保存到用户的设备上。

    jsPDF
    html2canvas
    废话不多说 上代码
    在utils下新建文件 htmlToPdf.ts

    /* eslint-disable spellcheck/spell-checker */
    // 页面导出为pdf格式
    import html2Canvas from 'html2canvas';
    import jsPDF from 'jspdf';
    
    const htmlToPdf = {
      getPdf(title: string, id: any) {
        html2Canvas(document.querySelector(id), {
          allowTaint: false,
          logging: false,
          useCORS: true,
          scale: 4, //按比例增加分辨率// 提升画面质量,但是会增加文件大小
          // 需要注意,高度 宽度一定要在这里定义一下,不然会存在只下载了当前你能看到的页面!!!
          // height: document.getElementById('pdfDom').scrollHeight,
          // windowHeight: document.getElementById('pdfDom').scrollHeight,
          height: 2700,
          windowHeight: 2700
        }).then(canvas => {
          const pdf = new jsPDF('p', 'mm', 'a4'); //A4纸,纵向
          const ctx = canvas.getContext('2d'),
            a4w = 210,
            a4h = 297, //A4大小,210mm x 297mm
            imgHeight = Math.floor((a4h * canvas.width) / a4w); //按A4显示比例换算一页图像的像素高度
          let renderedHeight = 0;
    
          while (renderedHeight < canvas.height) {
            const page = document.createElement('canvas');
            page.width = canvas.width;
            page.height = Math.min(imgHeight, canvas.height - renderedHeight); //可能内容不足一页
    
            //用getImageData剪裁指定区域,并画到前面创建的canvas对象中
            (page.getContext('2d') as any).putImageData(
              ctx?.getImageData(0, renderedHeight, canvas.width, Math.min(imgHeight, canvas.height - renderedHeight)),
              0,
              0
            );
            pdf.addImage(
              page.toDataURL('image/jpeg', 1.0),
              'JPEG',
              0,
              0,
              a4w,
              Math.min(a4h, (a4w * page.height) / page.width)
            ); //添加图像到页面,保留10mm边距
    
            renderedHeight += imgHeight;
            if (renderedHeight < canvas.height) {
              pdf.addPage(); //如果后面还有内容,添加一个空页
            }
            // delete page;
          }
          pdf.save(title + '.pdf');
        });
      }
    };
    
    export default htmlToPdf;
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    在使用的地方

    import htmlToPdf from '@/utils/htmlToPdf';
    
    
    const exportPDF = () => {
      // eslint-disable-next-line spellcheck/spell-checker
      htmlToPdf.getPdf('离缝报告', '#pdfDom');
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    参考文档

  • 相关阅读:
    自动化使用 ChatGPT 生成 PPT 大纲 - 基于 Python 和 PyAutoGUI
    并行前缀和计算——MPI SCAN算法的C语言实现
    JVM性能调优篇01-从JDK源码级别彻底剖析JVM类加载机制
    十四天算法快速入门第九天之「广度优先搜索 / 深度优先搜索」
    谷歌浏览器翻译插件,扩展程序,页面右键翻译
    怎样查看kafka写数据送到topic是否成功
    版本管理 | 如何解决SVN的合并冲突与分支问题?
    MySQL数据库用户管理操作
    2022年09月 C/C++(八级)真题解析#中国电子学会#全国青少年软件编程等级考试
    linux-文件查找、压缩和软件安装
  • 原文地址:https://blog.csdn.net/weixin_43957384/article/details/137915528
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号