下载依赖:html2canvas、jspdf
npm install html2canvas jspdf --save
新建htmlToPdf.js文件
// 导出页面为PDF格式
import html2Canvas from 'html2canvas' // 导入依赖
import JsPDF from 'jspdf'
import {
Message, Loading // 导入UI组件
} from 'element-ui'
let loading;
export default{
install (Vue, options) {
Vue.prototype.getPdf = function () {
loading = Loading.service({ // 这里添加了loading效果
lock: true,
text: '正在下载pdf...',
background: 'rgba(0,0,0,.5)'
});
let title = this.htmlTitle // 文件名称
html2Canvas(document.querySelector('#pdfDom'), {
allowTaint: true
}).then(function (canvas) {
console.log(canvas)
let contentWidth = canvas.width;
let contentHeight = can