1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| import jsPDF from 'jspdf' import html2canvas from 'html2canvas'
const html2pdf = function(dom, domWidth, domHeight, windowWidth, windowHeight, fileName){
const pageNum = Math.ceil(domHeight / windowHeight);
const a4Width = 592.28; const a4Height = 841.89; const imgWidth = domWidth / windowWidth * a4Width; const imgHeight = domHeight / windowHeight * a4Height;
let pdf = new jsPDF('', 'pt', 'a4');
for ( let i= 0; i < pageNum; i++) { dom.style.top = (- i * windowHeight) + 'px'; html2canvas(dom, { onrendered: (canvas) => { let pageData = canvas.toDataURL('image/jpeg', 1.0);
pdf.addImage(pageData, 'JPEG', 0, 0, imgWidth, imgHeight );
if (i == pageNum -1) { pdf.save(fileName) } else { pdf.addPage(); } } }) } dom.style.top = 0; }
export default html2pdf;
|