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)