Vue3利用vue-plugin-hiprint插件實現(xiàn)無預(yù)覽打印功能
前言
在MES管理系統(tǒng)中需要實現(xiàn)條碼數(shù)據(jù)從接口返回后,直接調(diào)用打印機進行打印,跳過瀏覽器的預(yù)覽確定那一步。在嘗試很多JS的方式和插件后,都無法實現(xiàn)該功能,因為基本上最后都是使用了Window.pring()方法進行打印,所以需要本地啟動服務(wù)調(diào)用打印機,才能實現(xiàn)該功能。
附上該插件的官方文檔 http://hiprint.io/,本文中未提及的功能基本上都能在官網(wǎng)中找到。Gitee鏈接
一、Vue3項目配置
1、安裝vue-plugin-hiprint插件
npm install vue-plugin-hiprint
2、文件引入
//在項目的入口文件中引入所需的CDN <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8"> <title>hinnn-hiprint</title> <!-- hiprint --> <link href="hiprint/css/hiprint.css" rel="external nofollow" rel="stylesheet"> <link href="hiprint/css/print-lock.css" rel="external nofollow" rel="external nofollow" rel="stylesheet"> <link href="hiprint/css/print-lock.css" rel="external nofollow" rel="external nofollow" media="print" rel="stylesheet"> </head> <body> <h1>hiprint 是一款用于web打印的js組件</h1> <!-- 加載 hiprint 的所有 JavaScript 插件。你也可以根據(jù)需要只加載單個插件。 --> <!-- polyfill.min.js解決瀏覽器兼容性問題v6.26.0--> <script src="hiprint/polyfill.min.js"></script> <!-- hiprint 核心js--> <script src="hiprint/hiprint.bundle.js"></script> <!-- 條形碼生成組件--> <script src="hiprint/plugins/JsBarcode.all.min.js"></script> <!-- 二維碼生成組件--> <script src="hiprint/plugins/qrcode.js"></script> <!-- 調(diào)用瀏覽器打印窗口helper類,可自行替換--> <script src="hiprint/plugins/jquery.hiwprint.js"></script> </body> </html>
3、項目中使用
在使用無預(yù)覽打印時如果我們需要對打印內(nèi)容設(shè)置樣式,那我們需要把樣式寫成行內(nèi)樣式不然不會生效.
// 全局引入 // main.ts import { hiPrintPlugin } from 'vue-plugin-hiprint' app.use(hiPrintPlugin, '$pluginName') // 局部引入 import { hiPrintPlugin } from 'vue-plugin-hiprint' // 頁面中使用 <template> <div ref="printRef" > //打印內(nèi)容 </div> </template> const printRef = ref(); // 組件 hiprint.init({ host: "https://printjs.cn:17521", // 打印客戶端的地址 token: "vue-plugin-hiprint", // 與打印客戶端相同的 token }) //初始化 const hiprintTemplate = new hiprint.PrintTemplate() // printByHtml為預(yù)覽打印,直接通過打印某個元素區(qū)域最好用(最好上手)所以使用的是打印html的方式 hiprintTemplate.printByHtml(printRef.value,{}); //printByHtml2方法為直接打印,需要安裝客戶端 下面會提到客戶端的安裝 hiprintTemplate.printByHtml2(printRef.value,{ printer: printerName, // 打印機名稱,不填則默認打印機 title: '任務(wù)隊列中名稱' });
二、安裝客戶端
如果要實現(xiàn)無預(yù)覽打印(靜默打印)就需要安裝打印客戶端, 無預(yù)覽打印的原理是使用 hiprint 的打印客戶端,它是一個基于 electron 的桌面應(yīng)用,可以接收來自瀏覽器的打印請求,然后直接調(diào)用本地打印機進行打印。
安裝完成后打開軟件,如果你的前端運行著,就會顯示有本地連接,不然就是沒有連上,需要重新啟動服務(wù)。界面如下:
以上就是Vue3利用vue-plugin-hiprint插件實現(xiàn)無預(yù)覽打印功能的詳細內(nèi)容,更多關(guān)于Vue3 vue-plugin-hiprint無預(yù)覽打印的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue v-for 點擊當前行,獲取當前行數(shù)據(jù)及event當前事件對象的操作
這篇文章主要介紹了vue v-for 點擊當前行,獲取當前行數(shù)據(jù)及event當前事件對象的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09解決vue+router路由跳轉(zhuǎn)不起作用的一項原因
這篇文章主要介紹了解決vue+router路由跳轉(zhuǎn)不起作用的一項原因,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07玩轉(zhuǎn)vue的slot內(nèi)容分發(fā)
這篇文章主要介紹了玩轉(zhuǎn)vue的slot內(nèi)容分發(fā),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-09-09