vue打印瀏覽器頁面功能的兩種實(shí)現(xiàn)方法
推薦使用方法二
方法一:通過npm 安裝插件
1,安裝 npm install vue-print-nb --save
2,引入 安裝好以后在main.js文件中引入
import Print from 'vue-print-nb'
Vue.use(Print); //注冊
3,現(xiàn)在就可以使用了
div id="printTest" > <p>明月照于山間</p> <p>清風(fēng)來于江上 </p> </div> <button v-print="'#printTest'">打印</button>
4.如需通過鏈接地址打?。簑indow.location.href = airway_bill; airway_bill為鏈接地址。
5.如果內(nèi)容打印不全,在打印操作時(shí)點(diǎn)擊更多設(shè)置,然后設(shè)置縮放。
方法一使用時(shí)可能會遇到內(nèi)容只有一頁,但是點(diǎn)擊打印會打印2張的情況。解決辦法:查看定義的元素高度是否有被設(shè)置為100%,或html高度被設(shè)置成100%,如果有去掉即可。
方法二:手動下載插件到本地
插件地址:https://github.com/xyl66/vuePlugs_printjs
1.在src下新建文件夾plugs,將下載好的print.js放入plugs文件夾下
2.在main.js中引入
print.js 里的代碼
//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.dom = dom; } 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>"; return str; }, getHtml: function () { var inputs = document.querySelectorAll('input'); var textareas = document.querySelectorAll('textarea'); var selects = document.querySelectorAll('select'); for (var k in inputs) { 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) } } for (var k2 in textareas) { if (textareas[k2].type == 'textarea') { textareas[k2].innerHTML = textareas[k2].value } } for (var k3 in selects) { 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') } } } } } 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;"; w = f.contentWindow || f.contentDocument; doc = f.contentDocument || f.contentWindow.document; doc.open(); doc.write(content); doc.close(); 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); } } }; const MyPlugin = {} MyPlugin.install = function (Vue, options) { Vue.prototype.$print = Print } export default MyPlugin
main.js里引入
import Print from './plugs/print' Vue.use(Print)
使用
<template> <div> <!-- 點(diǎn)擊按鈕打印 --> <el-button type="primary" @click="printDemo">點(diǎn)擊打印</el-button> <!-- <div ref="print"> <h1>這里是打印內(nèi)容</h1> </div>--> <img class="printsrcclass" ref='print' :src="printsrc"/> </div> </template> <script> export default { data(){ return {} }, methods: { // 點(diǎn)擊打印 printDemo(){ setTimeout(() => { this.$print(this.$refs.print) }, 100); } }, mounted() { } }
4.注意事項(xiàng) 需使用ref獲取dom節(jié)點(diǎn),若直接通過id或class獲取則webpack打包部署后打印內(nèi)容為空
5.指定不打印區(qū)域
方法1. 添加no-print樣式類
不要打印我
方法2. 自定義類名
不要打印我
this. print (this . print(this. print(this.refs.print,{‘no-print':‘.do-not-print-me-xxx'}) // 使用
如果圖片出不來 打印出不來 等情況
參考下面代碼
const res2 = await fnApi(orderId); let myBlob = new Blob([res2.data], { type: 'image/jpeg'}); var href = URL.createObjectURL(myBlob); // 創(chuàng)建對象超鏈接 // 此時(shí)拿到圖片地址 href,后面直接使用該地址即可 let img = new Image(); img.src = href; img.onload = () => { this.printsrc = href; this.$nextTick(() => { this.mypirntFN(); }) }
接口別忘了加類型
Ps:
一,可能遇到的問題及解決方案
①圖片占位置 ---------讓它脫離文檔流( position: absolute; 不要用fixed 這樣內(nèi)容多的時(shí)候只打印第一頁)
②頁面不想展示打印內(nèi)容 只打?。?------ 給它 z-index:-1 (display:none 的話打印內(nèi)容也沒有)
目前解決不了 跳過打印預(yù)覽直接打印功能
總結(jié)
到此這篇關(guān)于vue打印瀏覽器頁面功能的兩種實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)vue打印瀏覽器頁面功能內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
VUEJS實(shí)戰(zhàn)之修復(fù)錯(cuò)誤并且美化時(shí)間(2)
這篇文章主要為大家詳細(xì)介紹了VUEJS實(shí)戰(zhàn)之修復(fù)錯(cuò)誤并且美化時(shí)間,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06Monaco?Editor開發(fā)SQL代碼提示編輯器實(shí)例詳解
這篇文章主要為大家介紹了Monaco?Editor開發(fā)SQL編輯器實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08VUE配置proxy代理的開發(fā)測試及生產(chǎn)環(huán)境
這篇文章主要為大家介紹了VUE配置proxy代理的開發(fā)環(huán)境、測試環(huán)境、生產(chǎn)環(huán)境詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08vue?openlayers實(shí)現(xiàn)臺風(fēng)軌跡示例詳解
這篇文章主要為大家介紹了vue?openlayers實(shí)現(xiàn)臺風(fēng)軌跡示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11vue3點(diǎn)擊按鈕下載文件功能的代碼實(shí)現(xiàn)
在寫vue項(xiàng)目時(shí),有個(gè)需求是點(diǎn)擊表格中某一行的下載按鈕,然后開始下載這一行對應(yīng)的文件,所以本文小編給大家介紹了使用vue3實(shí)現(xiàn)點(diǎn)擊按鈕下載文件功能,文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下2024-01-01詳解基于vue-cli3快速發(fā)布一個(gè)fullpage組件
這篇文章主要介紹了詳解基于vue-cli3快速發(fā)布一個(gè)fullpage組件,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-03-03Vue中this.$router和this.$route的區(qū)別及push()方法
這篇文章主要給大家介紹了關(guān)于Vue中this.$router和this.$route的區(qū)別及push()方法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05