vue 中使用print.js導(dǎo)出pdf操作
1.print.js
// 打印類屬性、方法定義
/* eslint-disable */
const Print = function (dom, options) {
if (!(this instanceof Print)) return new Print(dom, options);
this.options = this.extend({
'noPrint': '.no-print'
}, options);
if ((typeof dom) === "string") {
this.dom = document.querySelector(dom);
} else {
this.isDOM(dom)
this.dom = this.isDOM(dom) ? dom : dom.$el;
}
this.init();
};
Print.prototype = {
init: function () {
var content = this.getStyle() + this.getHtml();
this.writeIframe(content);
},
extend: function (obj, obj2) {
for (var k in obj2) {
obj[k] = obj2[k];
}
return obj;
},
getStyle: function () {
var str = "",
styles = document.querySelectorAll('style,link');
for (var i = 0; i < styles.length; i++) {
str += styles[i].outerHTML;
}
str += "<style>" + (this.options.noPrint ? this.options.noPrint : '.no-print') + "{display:none;}</style>";
str += "<style>html,body,div,#preview-main,.report{width:100%!important;height: auto!important;font-size:14px}</style>";
return str;
},
getHtml: function () {
var inputs = document.querySelectorAll('input');
var textareas = document.querySelectorAll('textarea');
var selects = document.querySelectorAll('select');
var canvass = document.querySelectorAll('canvas');
for (var k = 0; k < inputs.length; k++) {
if (inputs[k].type == "checkbox" || inputs[k].type == "radio") {
if (inputs[k].checked == true) {
inputs[k].setAttribute('checked', "checked")
} else {
inputs[k].removeAttribute('checked')
}
} else if (inputs[k].type == "text") {
inputs[k].setAttribute('value', inputs[k].value)
} else {
inputs[k].setAttribute('value', inputs[k].value)
}
}
for (var k2 = 0; k2 < textareas.length; k2++) {
if (textareas[k2].type == 'textarea') {
textareas[k2].innerHTML = textareas[k2].value
}
}
for (var k3 = 0; k3 < selects.length; k3++) {
if (selects[k3].type == 'select-one') {
var child = selects[k3].children;
for (var i in child) {
if (child[i].tagName == 'OPTION') {
if (child[i].selected == true) {
child[i].setAttribute('selected', "selected")
} else {
child[i].removeAttribute('selected')
}
}
}
}
}
//canvass echars圖表轉(zhuǎn)為圖片
for (var k4 = 0; k4 < canvass.length; k4++) {
var imageURL = canvass[k4].toDataURL("image/png");
var img = document.createElement("img");
img.src = imageURL;
img.setAttribute('style', 'max-width: 100%;');
img.className = 'isNeedRemove'
// canvass[k4].style.display = 'none'
// canvass[k4].parentNode.style.width = '100%'
// canvass[k4].parentNode.style.textAlign = 'center'
canvass[k4].parentNode.insertBefore(img,canvass[k4].nextElementSibling);
}
return this.dom.outerHTML;
},
writeIframe: function (content) {
var w, doc, iframe = document.createElement('iframe'),
f = document.body.appendChild(iframe);
iframe.id = "myIframe";
//iframe.style = "position:absolute;width:0;height:0;top:-10px;left:-10px;";
iframe.setAttribute('style', 'position:absolute;width:0;height:0;top:-10px;left:-10px;');
w = f.contentWindow || f.contentDocument;
doc = f.contentDocument || f.contentWindow.document;
doc.open();
doc.write(content);
doc.close();
// 避免重復(fù)打印echarts
var removes = document.querySelectorAll('.isNeedRemove');
for (var k = 0; k < removes.length; k++) {
removes[k].parentNode.removeChild(removes[k]);
}
var _this = this
iframe.onload = function(){
_this.toPrint(w);
setTimeout(function () {
document.body.removeChild(iframe)
}, 100)
}
},
toPrint: function (frameWindow) {
try {
setTimeout(function () {
frameWindow.focus();
try {
if (!frameWindow.document.execCommand('print', false, null)) {
frameWindow.print();
}
} catch (e) {
frameWindow.print();
}
frameWindow.close();
}, 10);
} catch (err) {
console.log('err', err);
}
},
isDOM: (typeof HTMLElement === 'object') ?
function (obj) {
return obj instanceof HTMLElement;
} :
function (obj) {
return obj && typeof obj === 'object' && obj.nodeType === 1 && typeof obj.nodeName === 'string';
}
};
const MyPlugin = {}
MyPlugin.install = function (Vue, options) {
// 4. 添加實(shí)例方法
Vue.prototype.$print = Print
}
export default MyPlugin
自己頁面的樣式添加
str += "<style>html,body,div,#preview-main,.report{width:100%!important;height: auto!important;font-size:14px}</style>";
解決echarts圖表多點(diǎn)擊一次 就會(huì)多一張bug
img.className = 'isNeedRemove'
canvass[k4].parentNode.insertBefore(img,canvass[k4].nextElementSibling);
var removes = document.querySelectorAll('.isNeedRemove');
2.用法
//調(diào)用系統(tǒng)打印機(jī)
print () {
this.$print(this.$refs.print) // 使用
},
題外話
1.在自己需要的頁面引用打印頁面 這邊我打印頁面時(shí)preview.vue頁面
<!-- 預(yù)覽 --> <Modal v-model="previewModal" fullscreen :mask-closable="false" :closable="false"> <preview ref="print" :previewEditOldForm="previewEditForm"></preview> <div slot="footer" class="btn"> <Button type="text" @click="closePreview">關(guān)閉</Button> <Button type="primary" @click="print">打印</Button> </div> </Modal>
import Preview from '../operation_report/Preview'
export default {
components: {
Preview
}
}
獲取別的頁面的表單內(nèi)容 初始化
export default {
data() {
return{
previewEditForm: {},
}
}
}
// 父組件向子組件傳值
on: {
click: () => {
// 父組件向子組件傳值
this.previewEditForm = params.row;
setTimeout(() => {
this.$refs.print.queryById();
this.$refs.print.eventOverviews();
this.previewModal=true;
}, 2000)
}
}
隱藏打印頁面不需要的按鈕
<style scoped>
@media print {
.btn{
display: none;
}
}
</style>
4.打印內(nèi)容有echarts時(shí) 內(nèi)容不自適應(yīng) 調(diào)整width
width: 210mm;
最終如下

補(bǔ)充知識(shí):使用vue-cli腳手架 與 axios 請(qǐng)求數(shù)據(jù) 配置代理進(jìn)行跨域訪問數(shù)據(jù)問題
首先利用腳手架vue-cli 把項(xiàng)目搭建好 然后在 利用控制臺(tái) 在項(xiàng)目中安裝http-proxy-middleware中間件作為代理
控制臺(tái)安裝中間代理命令 如下:
npm install -S http-proxy-middleware
然后按照下圖 一步步配置
配置結(jié)束 是不是 還是不行?
在這里 要關(guān)閉下項(xiàng)目 重新在控制臺(tái) npm run dev 重啟項(xiàng)
目 就大功告成了
一點(diǎn)心得 體會(huì) 寫得不好 還請(qǐng)大家多多包涵




以上這篇vue 中使用print.js導(dǎo)出pdf操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue2.0實(shí)現(xiàn)倒計(jì)時(shí)的插件(時(shí)間戳 刷新 跳轉(zhuǎn) 都不影響)
我發(fā)現(xiàn)好多倒計(jì)時(shí)的插件,刷新都會(huì)變成從頭再來,于是自己用vue2.0寫了一個(gè),感覺還不錯(cuò),特此分享到腳本之家平臺(tái)供大家參考下2017-03-03
Vue3+Vite使用雙token實(shí)現(xiàn)無感刷新
本文主要介紹了Vue3+Vite使用雙token實(shí)現(xiàn)無感刷新,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
Element的穿梭框數(shù)據(jù)量大時(shí)點(diǎn)擊全選卡頓的解決方案
本文主要介紹了Element的穿梭框數(shù)據(jù)量大時(shí)點(diǎn)擊全選卡頓的解決方案,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
vue+animation實(shí)現(xiàn)翻頁動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了vue+animation實(shí)現(xiàn)翻頁動(dòng)畫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06
Vue單頁式應(yīng)用(Hash模式下)實(shí)現(xiàn)微信分享的實(shí)例
本篇文章介紹了Vue單頁式應(yīng)用(Hash模式下)實(shí)現(xiàn)微信分享的實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
基于vue 實(shí)現(xiàn)token驗(yàn)證的實(shí)例代碼
這篇文章主要介紹了基于vue 實(shí)現(xiàn)token驗(yàn)證的實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-12-12
如何在Vue中實(shí)現(xiàn)登錄驗(yàn)證功能(代碼示例)
Vue是一種流行的JavaScript框架,可以幫助開發(fā)者建立高效的Web應(yīng)用程序,本文將為您介紹如何在Vue中實(shí)現(xiàn)登錄驗(yàn)證功能,并為您提供具體的代碼示例,感興趣的朋友一起看看吧2023-11-11
vue3中使用props和emits并指定其類型與默認(rèn)值
props是Vue3中的一個(gè)重要概念,它允許我們將數(shù)據(jù)從父組件傳遞到子組件,下面這篇文章主要給大家介紹了關(guān)于vue3中使用props和emits并指定其類型與默認(rèn)值的相關(guān)資料,需要的朋友可以參考下2023-04-04

