import html2canvas from 'html2canvas'
import JsPDF from 'jspdf'
// id -> 放echarts图表的节点id
// fileName -> 想要生成的文件名
function exportData(id, fileName) {
const el = document.getElementById(id)
const elWidth = el.clientWidth
const elHeight = el.clientHeight
const max = Math.max(elWidth, elHeight) // 宽高都取最大值生成的pdf才会正常显示
html2canvas(el, {
scale: 1, // 如果生成文件不清晰,可以自己调整下
width: max,
height: max,
useCORS: true // 允许canvas画布内可以跨域请求外部链接图片, 允许跨域请求。
}).then(canvas => {
let pdf = new JsPDF('', 'pt', [canvas.width, canvas.height])
let pageData = canvas.toDataURL('image/jpeg', 1.0) // 转换图片为dataURL
pdf.addImage(pageData, 'JPEG', 0, 0, canvas.width, canvas.height) // 添加图像到页面
pdf.save(`${fileName}.pdf`)
})
}
echarts图表导出为pdf
于 2023-02-13 23:38:39 首次发布