马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
安装依赖:
npm install --save html2canvas //将页面html转换成图片
npm install jspdf --save //将图片天生pdf
yarn add html2canvas
yarn add jspdf
新建一个js文件 (pdf.js)
- import html2canvas from 'html2canvas'
- import jsPDF from 'jspdf'
- export const downloadPDF = (page) => {
- html2canvas(page).then(function (canvas) {
- canvas2PDF(canvas)
- })
- }
- const canvas2PDF = (canvas) => {
- let contentWidth = canvas.width
- let contentHeight = canvas.height
- //a4纸的尺寸[595.28,841.89],html页面生成的canvas在pdf中图片的宽高
- let imgWidth = 595.28
- let imgHeight = (592.28 / contentWidth) * contentHeight
- // 第一个参数: l:横向 p:纵向
- // 第二个参数:测量单位("pt","mm", "cm", "m", "in" or "px")
- let pdf = new jsPDF('p', 'pt')
- pdf.addImage(
- canvas.toDataURL('image/jpeg', 1.0),
- 'JPEG',
- 0,
- 0,
-
复制代码 |