npm install JsPDF --save
npm install html2Canvas --save
//在项目的util文件夹下新建htmlToPdf.js
// 导出页面为PDF格式
import html2Canvas from "html2canvas";
import JsPDF from "jspdf";
import { Loading } from "element-ui"
let loading;
export default {
install(Vue, options) {
Vue.prototype.getPdf = function(pdfTitle) {
loading = Loading.service({
lock: true,
text: "正在下载pdf...",
background: "rgba(144,147,153,.5)"
});
html2Canvas(document.querySelector("#pdfDom"), {
allowTaint: true,
scale: 2,
useCORS: true
}).then(function(canvas) {
loading.close();
let contentWidth = canvas.width;
let contentHeight = canvas.height;
let pageHeight = (contentWidth / 592.28) * 841.89;
l