vue项目保存页面为pdf 、word
pdf下载
npm install html2canvas
npm install jspdf
import html2Canvas from "html2canvas";
import JsPDF from "jspdf";
export default {
install(Vue, options) {
Vue.prototype.getPdf = function (fileName, callBack) {
var title = fileName;
html2Canvas(document.querySelector("#pdfDom"), {
allowTaint: true,
useCORS: true,
}).then(function (canvas) {
let contentWidth = canvas.width;
let contentHeight = canvas.height;
let pageHeight = (contentWidth / 592.28) * 841.89;
let leftHeight = contentHeight;
let position = 0;
let imgWidth = 595.28;
let imgHeight = (592.28 / contentWidth) * contentHeight;
let pageData = canvas.toDataURL("image/jpeg", 1.0);
let PDF = new JsPDF("", "pt", "a4");
if (leftHeight < pageHeight) {
PDF.addImage(pageData, "JPEG", 0, 0, imgWidth, imgHeight);
} else {
while (leftHeight > 0) {
PDF.addImage(pageData, "JPEG", 0, position, imgWidth, imgHeight);
leftHeight -= pageHeight;
position -= 841.89;
if (leftHeight > 0) {
PDF.addPage();
}
}
}
console.log(1);
PDF.save(title + ".pdf");
console.log(2);
callBack();
});
};
},
};
this.getPdf("下载的文件名", this.callback);
word下载
- 下载依赖
npm install pizzip
npm install docxtemplater
npm install jszip-utils
npm install file-saver
docxtemplater-image-module-free
- 如何使用
需要先本地创建导出后的word 文档模版
这样导出后的格式是自己可以控制的 不会有样式丢失的问题
模版如下图
exportWord: function (callBack) {
let _this = this;
JSZipUtils.getBinaryContent("helpCenter.docx", function (error, content) {
if (error) {
throw error;
}
let zip = new PizZip(content);
let doc = new docxtemplater();
doc.loadZip(zip);
doc.setData({
dataList: _this.dataList,
});
try {
doc.render();
} catch (error) {
let e = {
message: error.message,
name: error.name,
stack: error.stack,
properties: error.properties,
};
throw error;
}
let out = doc.getZip().generate({
type: "blob",
mimeType:
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
});
saveAs(out, "文件名.docx");
callBack();
});
},
initChart(id, xData, yData, yData2, type) {
this.rateChart = echarts.init(document.getElementById(id));
this.rateChart.setOption(
Options.BarSeriesOption(xData, yData, yData2, type)
);
this.rateChart.resize();
this.loading = false;
let resBase64 = this.rateChart.getDataURL({
pixelRatio: 2,
backgroundColor: "#fff",
excludeComponents: ["toolbox"],
});
this.echartsList.map((item) => {
if (item.chartId === id) {
item.img = resBase64;
}
});
window.addEventListener("resize", () => {
this.rateChart.resize();
});
this.$forceUpdate();
},
base64DataURLToArrayBuffer(dataURL) {
const base64Regex = /^data:image\/(png|jpg|svg|svg\+xml);base64,/;
if (!base64Regex.test(dataURL)) {
return false;
}
const stringBase64 = dataURL.replace(base64Regex, "");
let binaryString;
if (typeof window !== "undefined") {
binaryString = window.atob(stringBase64);
} else {
binaryString = new Buffer(stringBase64, "base64").toString("binary");
}
const len = binaryString.length;
const bytes = new Uint8Array(len);
for (let i = 0; i < len; i++) {
const ascii = binaryString.charCodeAt(i);
bytes[i] = ascii;
}
return bytes.buffer;
},
exportWord: function () {
var ImageModule = require("docxtemplater-image-module-free");
let _this = this;
JSZipUtils.getBinaryContent(
"echartsDownload.docx",
function (error, content) {
if (error) {
throw error;
}
let opts = {};
opts = { centered: true };
opts.getImage = (chartId, tagName) => {
return _this.base64DataURLToArrayBuffer(chartId);
};
opts.getSize = function (img, tagValue, tagName) {
return [600, 200];
};
let zip = new PizZip(content);
let doc = new docxtemplater();
doc.attachModule(new ImageModule(opts));
doc.loadZip(zip);
doc.setData({
..._this.form,
imgList: _this.imgList,
});
try {
doc.render();
} catch (error) {
let e = {
message: error.message,
name: error.name,
stack: error.stack,
properties: error.properties,
};
throw error;
}
let out = doc.getZip().generate({
type: "blob",
mimeType:
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
});
saveAs(out, "文件名.docx");
}
);
},
- 图片模版