vue form表单天生PDF

[复制链接]
发表于 2026-2-14 07:09:20 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
安装依赖:
npm install --save html2canvas //将页面html转换成图片
npm install jspdf --save  //将图片天生pdf
  
  yarn add html2canvas
  yarn add jspdf
  新建一个js文件 (pdf.js)
  1. import html2canvas from 'html2canvas'
  2. import jsPDF from 'jspdf'
  3. export const downloadPDF = (page) => {
  4.   html2canvas(page).then(function (canvas) {
  5.     canvas2PDF(canvas)
  6.   })
  7. }
  8. const canvas2PDF = (canvas) => {
  9.   let contentWidth = canvas.width
  10.   let contentHeight = canvas.height
  11.   //a4纸的尺寸[595.28,841.89],html页面生成的canvas在pdf中图片的宽高
  12.   let imgWidth = 595.28
  13.   let imgHeight = (592.28 / contentWidth) * contentHeight
  14.   // 第一个参数: l:横向  p:纵向
  15.   // 第二个参数:测量单位("pt","mm", "cm", "m", "in" or "px")
  16.   let pdf = new jsPDF('p', 'pt')
  17.   pdf.addImage(
  18.     canvas.toDataURL('image/jpeg', 1.0),
  19.     'JPEG',
  20.     0,
  21.     0,
  22.    
复制代码
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表