使用pdfmake导出pdf文件

时间:2024-11-16 08:27:02
import * as pdfMake from 'pdfmake/build/pdfmake'; import html2canvas from 'html2canvas'; exportPdf() { const widthScale = 2; const pdfWidth = 600 * widthScale; // pdf宽度 const cntElem = document.getElementById('exportPdf');// 组件的id const width = cntElem.offsetWidth; const height = cntElem.scrollHeight; const scale = 3; // 数字越大,导出的pdf越清晰 const opts = { scale: scale, width: width, height: height, useCORS: true, }; html2canvas(cntElem, opts).then(canvas => { const contents = []; contents.push({ image: canvas.toDataURL('image/jpeg'), width: pdfWidth, }); pdfMake .createPdf({ pageSize: { width: pdfWidth, height: pdfWidth / (width / height), // 按照实际高度比,自定义pdf高度 }, pageMargins: [0, 0], content: contents, }) .download('11111');//1111是pdf的名称 }); }