生活随笔
收集整理的這篇文章主要介紹了
vue项目保存页面为pdf 、word
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
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
如何使用
需要先本地創(chuàng)建導出后的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");});},
總結
以上是生活随笔為你收集整理的vue项目保存页面为pdf 、word的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。