// 导出页面为PDF格式
import html2Canvas from "html2canvas";
import JsPDF from "jspdf";
export default {
install(Vue, options) {
Vue.prototype.getPdf = function(id, title,imgw,top) {
return new Promise((resolve, reject) => {
let get1id = id.replace('#','')
debugger
html2Canvas(document.querySelector(id), {
allowTaint: true,
backgroundColor: 'white',
useCORS: true, //支持图片跨域
scale: 1, //设置放大的倍数
height: document.getElementById(get1id).scrollHeight+200,
windowHeight: document.getElementById(get1id).scrollHeight+200
}).then(function(canvas) {
debugger
let contentWidth = canvas.width;
let contentHeight = canvas.height;
let pageHeight = (contentWidth / 592.28) * 841.89;
let leftHeight = contentHeight;
let position = top;
const imgWidth = imgw;
let imgHeight = (imgw / contentWidth) * contentHeight;
let pageData = canvas.toDataURL("image/jpeg", 1.0);
// let PDF = new JsPDF("", "pt");
let PDF = new JsPDF("", "pt", "a4");
// PDF.addImage(pageData, "JPEG", 0, 0, contentWidth, contentHeight);
if (leftHeight < pageHeight) {
PDF.addImage(pageData, "JPEG", 65, 10, imgWidth, imgHeight);
} else {
while (leftHeight > 0) {
PDF.addImage(pageData, "JPEG", 65, position, imgWidth, imgHeight);
leftHeight -= pageHeight;
position -= 841.89;
if (leftHeight > 0) {
PDF.addPage();
}
}
}
PDF.save(title + ".pdf");
resolve(11);
});
});
};
}
};