主要通过html2canvas和jspdf两个插件实现
通过html2canvas将HTML页面转换成图片,然后再将图片转换成base64,通过jspdf生成pdf文件
1、安装插件
npm install html2canvas jspdf --save
2、写实现方法
import html2Canvas from 'html2canvas'
import JsPDF from 'jspdf'
export default (elementId, fileName) => {
if (!elementId) throw new Error("请传入元素id")
const container = document.querySelector(`#${
elementId}`)
const canvas = document.createElement("canvas")
const contentWidth = container.clientWidth
const contentHeight = c