解决安装jspdf和html2canvas报错问题,和实现HTML转pdf全过程

1.报错问题

报错export 'default' (imported as '_defineProperty') was not found in '@babel/runtime/helpers/defineProperty' (module has no exports)

版本问题,我用的vue2,安装以下版本

 成功解决报错问题

2.使用

npm i jspdf@2.0.0
npm i html2canvas@1.0.0

1.在util下创建一个js文件

// 导出页面为PDF格式
import html2Canvas from 'html2canvas'
import JsPDF from 'jspdf'
export default{
  install (Vue) {
    Vue.prototype.getPdf = function (options) {
      html2Canvas(options, {
        allowTaint: false,
        taintTest: true,
        dpi: '300',
        scale: 3
      }).then(function (canvas) {
        const PDFFile = {
            A4:[592.28, 841.89],
        };
        let canvasWidth = canvas.width  //页面生成canvas宽度
        let canvasHeight = canvas.height 
        let imgWidth = PDFFile.A4[0] - 60 ; //减去边距宽度
        let imgHeight = (canvasHeight/canvasWidth)*(PDFFile.A4[0] - 60) - 60; //减去边距高度
        let pageData = canvas.toDataURL('image/jpeg', 1.0)
        let PDF = new JsPDF('p', 'pt', 'a4')
         PDF.addImage(pageData, 'JPEG', 30, 30, imgWidth, imgHeight)
         PDF.output('dataurlnewwindow')  //预览
      })
    }
  }
}

2.在main.js中引入

import htmlToPdf from '@/utils/pdf'
Vue.use(htmlToPdf);

3.使用

var pdfDom = document.querySelector('#printPage')
this.getPdf(pdfDom)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值