先安装 然后在你的组件直接调用this.getPdf() 就可以打印
具体安装什么 请看代码 import就是要安装的 安装好之后千万别忘了再main.js也引入哦
import html2Canvas from 'html2canvas'
import JsPDF from 'jspdf'const A4pageH = 841.89
const A4pageW = 595.28export default {install (Vue, options) {Vue.prototype.getPdf = function () {var title = this.htmlTitlehtml2Canvas(document.querySelector('#pdfDom'), {allowTaint: true}).then(function (canvas) {let contentWidth = canvas.widthlet contentHeight = canvas.heightlet pageHeight = contentWidth / (A4pageW - 3) * A4pageH // A4纸尺寸let leftHeight = contentHeightlet position = 0let imgWidth = A4pageWlet imgHeight = (A4pageW - 3) / contentWidth * contentHeightlet pageData = canvas.toDataURL('image/jpeg', 1.0)let PDF = new JsPDF('', 'pt', 'a4')if (leftHeight < pageHeight) { // 单页足够打印完毕PDF.addImage(pageData, 'JPEG', 0, 0, imgWidth, imgHeight)} else { // 多页打印while (leftHeight > 0) {PDF.addImage(pageData, 'JPEG', 0, position, imgWidth, imgHeight)leftHeight -= pageHeightposition -= 841.89if (leftHeight > 0) {PDF.addPage()}}}PDF.save(title + '.pdf')})}}
}