行业知识
html2canvas+jsPDF实现前端导出pdf
2026-11-17 07:33:57
来源:互联网转载
html2canvas+jsPDF实现前端导出pdf
- 安装插件包
npm install jspdfnpm install html2canvas
- 引入插件
import html2canvas from 'html2canvas';import jsPDF from 'jspdf';
- 生成pdf
const perCanvas = document.createElement('canvas'); perCanvas.style.backgroundColor = '#fff' const context = perCanvas.getContext('2d'); html2canvas(this.$refs.pdfContent, { scale: 4, //放大防止模糊 dpi: 300 // 处理模糊问题 }).then((canvas) => { const canvasData = canvas.toDataURL('image/jpeg', 1.0); // pdf的尺寸 const pdfWidth = canvas.width; const pdfHeight = pdfWidth * 1.414; //切割后的canvas图片的宽高,就等于每页pdf的宽高 perCanvas.width = canvas.width; perCanvas.height = pdfHeight; // 每张图片的高度:适当减少100,上下各留50页边距 const perHeight = pdfHeight - 100; // 计算切割次数 let splitCount = Math.ceil(canvas.height / perHeight); if (splitCount * perHeight { let pdf = new jsPDF('p', 'pt', [pdfWidth, pdfHeight]); //切割canvas图片,贴到每一页pdf中 for (let i = 0; i ()()
上一篇:怎么进入自己买的服务器?
下一篇:小花仙游戏怎么换服务器?