将页面导出为PDF

环境及插件配置:(理论上vue2应该也可以使用,没有试验过)

javascript 复制代码
"vue": "^3.2.36",
"webpack": "^5.94.0",
"webpack-cli": "^5.1.4",
"html2canvas": "^1.4.1",
"jspdf": "^2.5.2",

必须安装 html2canvas 与 jspdf

该方法中用到了一个判断数据类型的小方法

javascript 复制代码
 * 判断数据类型
 * @param {any} value 
 * @returns {String}
 * 返回数据为:
 * "[object Number]","[object String]","[object Boolean]","[object Date]","[object RegExp]"
 * "[object Object]","[object Array]","[object Function]","[object Null]","[object Undefined]"
 */

function getDataType(value) {
  return Object.prototype.toString.call(value)
}

将html页面导出为PDF

javascript 复制代码
import jsPDF from 'jspdf';
import html2canvas from 'html2canvas';

function getDataType(value) {
  return Object.prototype.toString.call(value)
}

/**
 * 将页面导出为PDF
 * @param {Object} 参数为{},具体属性参数参照详细解释
 * @param {html} htmlDom 要导出的DOM片段
 * @param {String} exportTitle 导出文件的文件名
 * @param {String} watermark 水印 需要水印时传入即可 非必填
 * @param {String} color 水印颜色 - 默认 opacity 为0.3
 * @param {String} bgColor 导出的PDF背景色 - 默认为白色
 */

function exportToPDF({ htmlDom, exportTitle = "导出文件", watermark, color = '184,184,184, 0.3', bgColor = "#fff" }) {
  const A4Width = 197;
  const A4Height = 297;
  html2canvas(htmlDom,
    {
      scale: 2,
      useCORS: true,
      dpi: 300,
      backgroundColor: bgColor
    }
  ).then(canvas => {
    const pdf = new jsPDF('p', 'mm', 'a4')
    const ctx = canvas.getContext('2d')
    // 水印
    if (watermark) {
      const contentWidth = canvas.width
      const contentHeight = canvas.height
      console.log(watermark)
      ctx.textAlign = 'center';
      ctx.textBaseline = 'middle';
      ctx.rotate((20 * Math.PI) / 180);
      ctx.font = '20px Microsoft Yahei';
      ctx.fillStyle = color;
      for (let i = contentWidth * -1; i < contentWidth; i += 400) {
        for (let j = contentHeight * -1; j < contentHeight; j += 400) {
          // 填充文字,x 间距, y 间距
          ctx.fillText(watermark, i, j);
        }
      }
    }
    const imgHeight = Math.floor(A4Height * canvas.width / A4Width)
    let rendereHeight = 0
    while (rendereHeight < canvas.height) {
      const page = document.createElement('canvas')
      page.width = canvas.width
      page.height = Math.min(imgHeight, canvas.height - rendereHeight)
      page.getContext('2d').putImageData(ctx.getImageData(0, rendereHeight, canvas.width, Math.min(imgHeight, canvas.height - rendereHeight)), 0, 0)
      pdf.addImage(page.toDataURL('image/jpeg', 1.0), 'PNG', 10, 10, A4Width, Math.min(A4Height, A4Width * page.height / page.width))
      rendereHeight += imgHeight
      if (rendereHeight < canvas.height) {
        pdf.addPage()
      }
    }
    pdf.save(exportTitle + '.pdf')
  })
}

export {
  exportToPDF
}
相关推荐
qq_1777673720 分钟前
React Native鸿蒙跨平台数据使用监控应用技术,通过setInterval每5秒更新一次数据使用情况和套餐使用情况,模拟了真实应用中的数据监控场景
开发语言·前端·javascript·react native·react.js·ecmascript·harmonyos
烬头882123 分钟前
React Native鸿蒙跨平台应用实现了onCategoryPress等核心函数,用于处理用户交互和状态更新,通过计算已支出和剩余预算
前端·javascript·react native·react.js·ecmascript·交互·harmonyos
小咖自动剪辑2 小时前
Base64与图片互转工具增强版:一键编码/解码,支持多格式
人工智能·pdf·word·媒体
程序员清洒2 小时前
Flutter for OpenHarmony:Text — 文本显示与样式控制
开发语言·javascript·flutter
雨季6663 小时前
Flutter 三端应用实战:OpenHarmony 简易“动态内边距调节器”交互模式深度解析
javascript·flutter·ui·交互·dart
天人合一peng3 小时前
Unity中button 和toggle监听事件函数有无参数
前端·unity·游戏引擎
会飞的战斗鸡3 小时前
JS中的链表(含leetcode例题)
javascript·leetcode·链表
方也_arkling4 小时前
别名路径联想提示。@/统一文件路径的配置
前端·javascript
毕设源码-朱学姐4 小时前
【开题答辩全过程】以 基于web教师继续教育系统的设计与实现为例,包含答辩的问题和答案
前端
qq_177767374 小时前
React Native鸿蒙跨平台剧集管理应用实现,包含主应用组件、剧集列表、分类筛选、搜索排序等功能模块
javascript·react native·react.js·交互·harmonyos