vue純前端實(shí)現(xiàn)將頁(yè)面導(dǎo)出為pdf和word文件
適用業(yè)務(wù)需求:將當(dāng)前頁(yè)面顯示內(nèi)容導(dǎo)出pdf或者word文件
實(shí)現(xiàn)思路:先將顯示內(nèi)容轉(zhuǎn)成圖片base64地址,再生成相應(yīng)文件
注意:顯示內(nèi)容直接轉(zhuǎn)圖片慎用::before、::after這些css,svg圖標(biāo),不然可能出現(xiàn)生成的圖片樣式丟失問(wèn)題,如果確實(shí)需要顯示svg圖標(biāo)的話(huà)目前做法是轉(zhuǎn)成png顯示,如有更好方法,歡迎補(bǔ)充
導(dǎo)出pdf方式:
1.安裝相應(yīng)插件
npm i html2canvas jspdf -S
2.相應(yīng)頁(yè)面引入插件
import html2Canvas from 'html2canvas' import JsPDF from 'jspdf'
3.相應(yīng)頁(yè)面下載代碼實(shí)現(xiàn)
html2Canvas(document.querySelector('#pdfDom'), {
allowTaint: 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('p', '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()
}
}
}
PDF.save('導(dǎo)出文件名.pdf')
})
導(dǎo)出word方式:
(1) 安裝相應(yīng)插件
npm i html2canvas docxtemplater pizzip jszip-utils jszip file-saver angular-expressions image-size docxtemplater-image-module-free -S
(2) 新建exportFile.js,存儲(chǔ)位置自定義,本例放在utils/exportFile.js,代碼如下
import PizZip from 'pizzip'
import Docxtemplater from 'docxtemplater'
import JSZipUtils from 'jszip-utils'
import { saveAs } from 'file-saver'
/**
* 將base64格式的數(shù)據(jù)轉(zhuǎn)為ArrayBuffer
* @param {Object} dataURL base64格式的數(shù)據(jù)
*/
function base64DataURLToArrayBuffer(dataURL) {
const base64Regex = /^data:image/(png|jpg|jpeg|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
}
/**
* 導(dǎo)出word,支持圖片
* @param {Object} tempDocxPath 模板文件路徑
* @param {Object} wordData 導(dǎo)出數(shù)據(jù)
* @param {Object} fileName 導(dǎo)出文件名
* @param {Object} imgSize 自定義圖片尺寸
*/
export const exportWord = (tempDocxPath, wordData, fileName, imgSize) => {
// 這里要引入處理圖片的插件
var ImageModule = require('docxtemplater-image-module-free')
const expressions = require('angular-expressions')
// 讀取并獲得模板文件的二進(jìn)制內(nèi)容
JSZipUtils.getBinaryContent(tempDocxPath, function(error, content) {
if (error) {
throw error
}
expressions.filters.size = function(input, width, height) {
return {
data: input,
size: [width, height]
}
}
// function angularParser (tag) {
// const expr = expressions.compile(tag.replace(/'/g, "'"));
// return {
// get (scope) {
// return expr(scope);
// },
// };
// }
// 圖片處理
let opts = {}
opts = {
// 圖像是否居中
centered: false
}
opts.getImage = (chartId) => {
// console.log(chartId);//base64數(shù)據(jù)
// 將base64的數(shù)據(jù)轉(zhuǎn)為ArrayBuffer
return base64DataURLToArrayBuffer(chartId)
}
opts.getSize = function(img, tagValue, tagName) {
// console.log(img);//ArrayBuffer數(shù)據(jù)
// console.log(tagValue);//base64數(shù)據(jù)
// console.log(tagName);//wordData對(duì)象的圖像屬性名
// 自定義指定圖像大小
if (Object.prototype.hasOwnProperty.call(imgSize, tagName)) {
return imgSize[tagName]
} else {
return [600, 350]
}
}
// 創(chuàng)建一個(gè)PizZip實(shí)例,內(nèi)容為模板的內(nèi)容
const zip = new PizZip(content)
// 創(chuàng)建并加載docxtemplater實(shí)例對(duì)象
const doc = new Docxtemplater()
doc.attachModule(new ImageModule(opts))
doc.loadZip(zip)
doc.setData(wordData)
try {
// 用模板變量的值替換所有模板變量
doc.render()
} catch (error) {
// 拋出異常
const e = {
message: error.message,
name: error.name,
stack: error.stack,
properties: error.properties
}
console.log(JSON.stringify({
error: e
}))
throw error
}
// 生成一個(gè)代表docxtemplater對(duì)象的zip文件(不是一個(gè)真實(shí)的文件,而是在內(nèi)存中的表示)
const out = doc.getZip().generate({
type: 'blob',
mimeType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
})
// 將目標(biāo)文件對(duì)象保存為目標(biāo)類(lèi)型的文件,并命名
saveAs(out, fileName)
})
}
(3) 在相應(yīng)頁(yè)面引入插件
import html2Canvas from 'html2canvas'
import { exportWord } from '@/utils/exportFile'
(4) 在相應(yīng)頁(yè)面下載代碼,首先要先在本地存儲(chǔ)一份模版doc文件,vue-cli2存在static文件夾下面,vue-cli3存在public文件夾下面,doc模版類(lèi)似這樣聲明

實(shí)現(xiàn)代碼如下:
const fileUrl = window.location.href.split('#')[0].replace('index.html', '') + 'data/doc/report-model.docx' // 存在public下的模版文件
html2Canvas(document.querySelector('#abnormalChartExp' + p.value), {
allowTaint: true
}).then(function(canvas) {
const contentWidth = canvas.width
const contentHeight = canvas.height
const imgWidth = 595.28
const imgHeight = 592.28 / contentWidth * contentHeight
const pageData = canvas.toDataURL('image/jpeg', 1.0)
// console.log(pageData)
const imgSize = {
imgUrl: [imgWidth, imgHeight] // 控制導(dǎo)出的word圖片大小,寬度鋪滿(mǎn)
}
exportWord(fileUrl, { imgUrl: pageData }, `demo.docx`, imgSize)
})
word導(dǎo)出參考文章:vue實(shí)現(xiàn)導(dǎo)出word文檔
到此這篇關(guān)于vue純前端實(shí)現(xiàn)將頁(yè)面導(dǎo)出為pdf和word文件的文章就介紹到這了,更多相關(guān)vue頁(yè)面導(dǎo)出為pdf和word內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
FastAPI和Vue實(shí)現(xiàn)文件分片上傳+秒傳+斷點(diǎn)續(xù)傳的完整思路
本文介紹了使用FastAPI和Vue實(shí)現(xiàn)大文件分片上傳的方法,包括文件切片、哈希校驗(yàn)、秒傳和斷點(diǎn)續(xù)傳等核心概念,并提供了后端FastAPI和前端Vue的實(shí)現(xiàn)思路及關(guān)鍵代碼片段,感興趣的朋友跟隨小編一起看看吧2026-04-04
詳解Vuejs2.0 如何利用proxyTable實(shí)現(xiàn)跨域請(qǐng)求
本篇文章主要介紹了Vuejs2.0 如何利用proxyTable實(shí)現(xiàn)跨域請(qǐng)求,具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08
Vue使用screenfull實(shí)現(xiàn)全屏效果
這篇文章主要為大家詳細(xì)介紹了Vue使用screenfull實(shí)現(xiàn)全屏,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09
使用Vue-ECharts實(shí)現(xiàn)數(shù)據(jù)可視化圖表功能
在前端開(kāi)發(fā)中,經(jīng)常會(huì)遇到需要展示數(shù)據(jù)可視化的需求,比如柱狀圖、折線(xiàn)圖、餅圖等,這類(lèi)需求不僅要求我們準(zhǔn)確地將數(shù)據(jù)呈現(xiàn)出來(lái),還需要兼顧美觀(guān)與交互體驗(yàn),所以今天我們就來(lái)聊聊——Vue-ECharts,給大家介紹一下如何使用Vue-ECharts實(shí)現(xiàn)數(shù)據(jù)可視化圖表功能2025-05-05
antd+vue實(shí)現(xiàn)動(dòng)態(tài)驗(yàn)證循環(huán)屬性表單的思路
今天通過(guò)本文給大家分享antd+vue實(shí)現(xiàn)動(dòng)態(tài)驗(yàn)證循環(huán)屬性表單的思路,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-09-09
Vue @click.stop阻止事件向祖先元素傳遞方式(事件冒泡)
在Vue中,使用@click.stop修飾符可以阻止事件向父級(jí)元素傳遞,從而實(shí)現(xiàn)單擊父級(jí)元素執(zhí)行函數(shù),而單擊子元素不執(zhí)行函數(shù)的需求2025-02-02
vue實(shí)現(xiàn)手機(jī)號(hào)碼抽獎(jiǎng)上下滾動(dòng)動(dòng)畫(huà)示例
本篇文章主要介紹了vue實(shí)現(xiàn)手機(jī)號(hào)碼抽獎(jiǎng)上下滾動(dòng)動(dòng)畫(huà)示例。具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
Vue3搭建組件庫(kù)開(kāi)發(fā)環(huán)境的示例詳解
這篇文章給大家分享Vue3搭建組件庫(kù)開(kāi)發(fā)環(huán)境,給大家講解依次搭建組件庫(kù)、example、文檔、cli,本文內(nèi)容是搭建組件庫(kù)的開(kāi)發(fā)環(huán)境的過(guò)程,感興趣的朋友跟隨小編一起看看吧2022-11-11

