vue導(dǎo)出html、word和pdf的實(shí)現(xiàn)代碼
導(dǎo)出的頁(yè)面組件如下:
<template> <div id="resumeId"> <resumeHtml ref="resume" @on-download="download"/> </div> </template>
1、導(dǎo)出html
方法:
1)獲取要導(dǎo)出的組件頁(yè)面的css把它設(shè)置成js變量一文本并通過(guò)export導(dǎo)出
2)獲取要導(dǎo)出組件頁(yè)面的html的dom標(biāo)簽代碼,通過(guò)this.$refs.resume.$el.innerHTML
獲取,也可以通過(guò)document.getElementById('resumeId')
獲得
3)構(gòu)造html頁(yè)面,并使用createObjectURL構(gòu)造一個(gè)文件流并下載,如下:
var a = document.createElement('a'); var url = window.URL.createObjectURL(new Blob([content], { type: (option.type || "text/plain") + ";charset=" + (option.encoding || 'utf-8') })); a.href = url; a.download = fileName || 'file'; a.click(); window.URL.revokeObjectURL(url);
具體代碼如下:
import axios from 'axios' import resumeHtml from './resume-html' import writer from 'file-writer'; import {resumecss} from '@/assets/style/download/resume.css.js' ... downloadHtml(name){ let html = this.getHtmlContent(); let s = writer(`${name}的簡(jiǎn)歷.html`, html, 'utf-8'); console.log('s stream',s); }, getHtmlContent(){ const template = this.$refs.resume.$el.innerHTML; let html = `<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <title>X-Find迅聘選才</title> <link rel="stylesheet" /> <style> ${resumecss} </style> </head> <body> <div class="resume_preview_page" style="margin:0 auto;width:1200px"> ${template} </div> </body> </html>`; return html; }
導(dǎo)出的樣式j(luò)s文件:
export const resumecss =` html, body { position: relative; height: 100%; } .page_layout { position: relative; height: 100%; display: flex; & .layout_content { flex-grow: 1; display: flex; flex-direction: column; } } ...
2、導(dǎo)出Word
方法:
1)使用上面構(gòu)造好的html文本,以文件流的形式發(fā)送到后臺(tái),后臺(tái)通過(guò)轉(zhuǎn)換得到word流傳給前端并下載
let url = `${this.$url}/uploadFile/uploadResume`; let html = this.getHtmlContent(); // 構(gòu)造blob文件流 let html_ = new Blob([html],{ "type" : "text/html;charset=utf-8" }) let formdata = new FormData(); formdata.append('file', html_, `sdf.html`);//sdf.html是設(shè)置文件名 axios({ method: 'post', url: url, data:formdata, responseType:'blob',//這里如果不設(shè)置,下載會(huì)打不開(kāi)文件 }) .then(res=>{ console.log('download res',res); //通過(guò)后臺(tái)返回 的word文件流設(shè)置文件名并下載 var blob = new Blob([res.data], { type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=utf-8'}); //application/vnd.openxmlformats-officedocument.wordprocessingml.document這里表示doc類型 var downloadElement = document.createElement('a'); var href = window.URL.createObjectURL(blob); //創(chuàng)建下載的鏈接 downloadElement.href = href; downloadElement.download ='s.doc'; //下載后文件名 document.body.appendChild(downloadElement); downloadElement.click(); //點(diǎn)擊下載 document.body.removeChild(downloadElement); //下載完成移除元素 window.URL.revokeObjectURL(href); //釋放掉blob對(duì)象 })
3、導(dǎo)出PDF
方法:
1)創(chuàng)建一個(gè)htmlToPdf.js文件,如下代碼
// 下面兩個(gè)package要單獨(dú)安裝 import html2Canvas from 'html2canvas' import JsPDF from 'jspdf' export default{ install (Vue, options) { Vue.prototype.getPdf = function (id,title) { html2Canvas(document.querySelector(`#${id}`), { // 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() } } } PDF.save(title + '.pdf') } ) } } }
2)main.js文件中添加如下代碼:
import htmlToPdf from '@/utils/htmlToPdf' Vue.use(htmlToPdf)
3)然后就可以在要導(dǎo)出pdf文件組件里面添加 如下 代碼即可導(dǎo)出
this.getPdf('resumeId',name)
總結(jié):
1、雖然完成了三種文件的導(dǎo)出但是我對(duì)word和html導(dǎo)出還是不滿意,不是最佳解決方法,如果 有人有更好的方法,歡迎留言
2、導(dǎo)出的word沒(méi)有了樣式,所以這塊還是有問(wèn)題
引用 :
1、https://stackoverflow.com/questions/43537121/how-to-get-html-content-of-component-in-vue-js
3、nodejs(officegen)+vue(axios)在客戶端導(dǎo)出word文檔
5、vue實(shí)現(xiàn)word,pdf文件的導(dǎo)出
以上所述是小編給大家介紹的vue導(dǎo)出html、word和pdf的實(shí)現(xiàn)代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- vue實(shí)現(xiàn)word,pdf文件的導(dǎo)出功能
- Vue將頁(yè)面導(dǎo)出為圖片或者PDF
- Vue如何將頁(yè)面導(dǎo)出成PDF文件
- Vue導(dǎo)出頁(yè)面為PDF格式的實(shí)現(xiàn)思路
- vue實(shí)現(xiàn)pdf導(dǎo)出解決生成canvas模糊等問(wèn)題(推薦)
- 基于Vue實(shí)現(xiàn)HTML轉(zhuǎn)PDF并導(dǎo)出
- vue 中使用print.js導(dǎo)出pdf操作
- vue3將頁(yè)面生成pdf導(dǎo)出的操作指南
- vue導(dǎo)出少量pdf文件實(shí)現(xiàn)示例詳解
- Vue3導(dǎo)出pdf文件詳細(xì)方案
相關(guān)文章
ant-design-vue Table pagination分頁(yè)實(shí)現(xiàn)全過(guò)程
這篇文章主要介紹了ant-design-vue Table pagination分頁(yè)實(shí)現(xiàn)全過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04vant遇到van-sidebar數(shù)據(jù)超出不能滑動(dòng)的問(wèn)題
這篇文章主要介紹了vant遇到van-sidebar數(shù)據(jù)超出不能滑動(dòng)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04前端vue中實(shí)現(xiàn)文件下載的幾種方法總結(jié)
這篇文章主要介紹了前端vue中實(shí)現(xiàn)文件下載的幾種方法總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04vue中的echarts實(shí)現(xiàn)寬度自適應(yīng)的解決方案
這篇文章主要介紹了vue中的echarts實(shí)現(xiàn)寬度自適應(yīng),本文給大家分享實(shí)現(xiàn)方案,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09vue實(shí)現(xiàn)純前端表格滾動(dòng)分頁(yè)加載
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)純前端表格滾動(dòng)分頁(yè)加載,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04vue?el-table實(shí)現(xiàn)動(dòng)態(tài)添加行和列具體代碼
最近遇到一個(gè)動(dòng)態(tài)增加行和列的需求,所以這里給大家總結(jié)下,這篇文章主要給大家介紹了關(guān)于vue?el-table實(shí)現(xiàn)動(dòng)態(tài)添加行和列的相關(guān)資料,需要的朋友可以參考下2023-09-09