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

React网页转换为pdf并下载|使用jspdf html2canvas

checkout 分支后突然报错,提示:

Can't resolve 'jspdf' in ...

Can't resolve 'html2canvas' in ...

解决方法很简单,重新 yarn install 就好了,至于为什么,我暂时也不知道,总之解决了。

思路来源:

先随便记一下写的js,将组件转换为pdf,添加水印,并且下载:

import html2canvas from "html2canvas";
import jsPDF from "jspdf";/*** 导出PDF* @param {导出后的文件名} filename* @param {要导出的dom节点} dom* @param {导出的文件水印:用户邮箱} email*/export const exportPDF = (filename, dom, email) => {const scale = 0.8;// 滚动到顶部,避免打印不全document.documentElement.scrollTop = 0;html2canvas(dom, {allowTaint: true, // Whether to allow cross-origin images to taint the canvasscale, // The scale to use for rendering. Defaults to the browsers device pixel ratio.}).then((canvas) => {const contentWidth = canvas.width / scale;const contentHeight = canvas.height / scale;console.log("height",contentHeight,canvas.height,contentWidth,canvas.width,dom.offsetWidth,dom.offsetHeight,);const pdf = new jsPDF("", "pt", [contentWidth, contentHeight]);const pageData = canvas.toDataURL("image/jpeg", 1.0);pdf.addImage(pageData,"JPEG",(contentWidth - contentWidth / 2.6) / 2, // x偏移20, // y偏移contentWidth / 2.6,contentHeight > 14400 ? 14380 : contentHeight,"","FAST");// 添加水印for (let i = 1; i < contentHeight / 240 - 1; i++) {pdf.setTextColor(150);pdf.setFontSize(35);pdf.setFont("courier");pdf.text(contentWidth / 2, 450 * i, email, 45);}pdf.save(`${filename}.pdf`);});
};

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

相关文章:

  • EASYEXCEL导出表格(有标题、单元格合并)
  • pytest 断言异常
  • 听GPT 讲Rust源代码--src/tools(22)
  • OD Linux发行版本
  • 华为端口隔离简单使用方法同vlan下控制个别电脑不给互通
  • DaVinci各版本安装指南
  • 【黑马甄选离线数仓day10_会员主题域开发_DWS和ADS层】
  • OD 完美走位
  • SpringSecurity6 | 失败后的跳转
  • MySQL数据库增删改查
  • Altium Designer(AD24)新工程复用设计文件图文教程及视频演示
  • Python遥感影像深度学习指南(1)-使用卷积神经网络(CNN、U-Net)和 FastAI进行简单云层检测
  • Hive-DML详解(超详细)
  • PHP实现可示化代码
  • useState语法讲解
  • 堆与二叉树(下)
  • 讲诉JVM
  • 8、SpringCloud高频面试题-版本1
  • PHP案例代码:PHP如何提供下载功能?
  • The Cherno C++笔记 03
  • 蓝牙物联网与嵌入式开发如何结合?
  • 前端面试——JavaScript面经(持续更新)
  • 微前端——无界wujie
  • 连锁便利店管理系统有什么用
  • Vue 的两种实现:VSCode 中配置 vue 模板快捷方式的过程
  • electron 切换至esm
  • 【新版】软考 - 系统架构设计师(总结笔记)
  • Spring MVC 方法中添加参数、HttpServletRequest 和 HttpServletResponse 对象
  • 单片机的RTC获取网络时间
  • Android 13 内置可卸载的搜狗输入法