import html2Canvas from 'html2canvas' import JsPDF from 'jspdf' import { Loading } from 'element-ui'; function footer(doc){ doc.text(150,285,'page ' + doc.page); //print number bottom right doc.page ++; }; export default{ install (Vue, options) { Vue.prototype.getPdf = function (id, title,sgin) { let loadingInstance = Loading.service({target:'page_main'}); html2Canvas(document.querySelector(`#${id}`), { // allowTaint: true useCORS: true, scale: 1.1, scrollY: 0, }).then(function (canvas) { let contentWidth = canvas.width; let contentHeight = canvas.height; let pageHeight = contentWidth / 592.28 * 841.89; let leftHeight = contentHeight; let position = 0; let imgWidth = 595.28; let imgHeight = 592.28 / contentWidth * contentHeight; let pageData = canvas.toDataURL('image/jpeg', 1.0); let PDF = new JsPDF('', 'pt', 'a4'); let logo =document.querySelector(`#logo`); let pageNumber=1; if (leftHeight < pageHeight) { PDF.addImage(pageData, 'JPEG', 0, 0, imgWidth, imgHeight); PDF.addImage(logo, 'PNG', 10, 6); } else { while (leftHeight > 0) { PDF.addImage(pageData, 'JPEG', 0, position, imgWidth, imgHeight); if(pageNumber==1){ pageNumber++ PDF.addImage(logo, 'PNG', 10, 6); } // PDF.setFont('msyh') // PDF.text(500,13,sgin); leftHeight -= pageHeight; position -= 841.89; if (leftHeight > 0) { PDF.addPage(); } } } PDF.save(title + '.pdf') loadingInstance.close(); } ) } } }