当前位置: 首页 > 工具软件 > jsPDF > 使用案例 >

jspdf打印、pdf打印

李联
2023-12-01

先安装 然后在你的组件直接调用this.getPdf() 就可以打印
具体安装什么 请看代码 import就是要安装的 安装好之后千万别忘了再main.js也引入哦

import html2Canvas from 'html2canvas'
import JsPDF from 'jspdf'

const A4pageH = 841.89
const A4pageW = 595.28

export default {
  install (Vue, options) {
    Vue.prototype.getPdf = function () {
      var title = this.htmlTitle
      html2Canvas(document.querySelector('#pdfDom'), {allowTaint: true}).then(function (canvas) {
        let contentWidth = canvas.width
        let contentHeight = canvas.height
        let pageHeight = contentWidth / (A4pageW - 3) * A4pageH // A4纸尺寸
        let leftHeight = contentHeight
        let position = 0
        let imgWidth = A4pageW
        let imgHeight = (A4pageW - 3) / contentWidth * contentHeight
        let 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 -= pageHeight
            position -= 841.89
            if (leftHeight > 0) {
              PDF.addPage()
            }
          }
        }

        PDF.save(title + '.pdf')
      })
    }
  }
}
 类似资料: