麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > JavaScript > 正文

electron實現(xiàn)靜默打印的示例代碼

2019-11-19 11:03:00
字體:
供稿:網(wǎng)友

前言

electron+vuecli3 實現(xiàn)設(shè)置打印機,靜默打印小票功能

網(wǎng)上相關(guān)的資料比較少,這里給大家分享一下,希望大家可以少踩一些坑
github地址

必須要強調(diào)一下的是electron的版本必須是3.0.0不能,我嘗試了4和5都沒有實現(xiàn)

效果圖


使用

git clone https://github.com/sunnie1992/electron-vue-print-demo.gitnpm installnpm run electron:serve

實現(xiàn)

操作思路
1.用戶點擊打印
2.查詢本地electron-store(用來向本地存儲,讀取數(shù)據(jù))是否存打印機名稱
3.已經(jīng)設(shè)置,直接打印
4.沒有設(shè)置,彈出設(shè)置打印機框
5.用戶設(shè)置好確認(rèn)后打印

首頁App.vue引入了兩個組件,一個是主動設(shè)置打印機的彈出printDialog

另外一個是打印組件,打印是通過webview將需要打印的內(nèi)容渲染到html頁面然后就能打印了

<template> <div id="app">  <el-button type="primary" @click="showPrint">設(shè)置打印機</el-button>  <printDialog :dialog-visible="dialogVisible" @cancel="handlePrintDialogCancel" />  <pinter ref="print" :html-data="HtmlData"></pinter>  <el-table :data="tableData" style="width: 100%">   <el-table-column prop="date" label="日期" width="180" column-key="date">   </el-table-column>   <el-table-column prop="name" label="姓名" width="180">   </el-table-column>   <el-table-column prop="address" label="地址">   </el-table-column>   <el-table-column label="操作">    <template slot-scope="scope">     <el-button type="primary" @click="doPrint(scope.row)">打印</el-button>    </template>   </el-table-column>  </el-table> </div></template><script>import { ipcRenderer } from 'electron'import printDialog from './components/PrintDialog.vue'import Pinter from './components/pinter.vue'export default { name: 'App', components: {  Pinter,  printDialog }, data() {  return {   dialogVisible: false,   HtmlData: '',   printList: [],   tableData: [{    date: '2016-05-02',    name: '我是小仙女',    address: '上海市浦東新區(qū)',    tag: '家'   }, {    date: '2016-05-04',    name: '我是小仙女1',    address: '上海市浦東新區(qū)',    tag: '公司'   }, {    date: '2016-05-01',    name: '我是小仙女2',    address: '上海市浦東新區(qū)',    tag: '家'   }, {    date: '2016-05-03',    name: '我是小仙女3',    address: '上海市浦東新區(qū)',    tag: '公司'   }]  } }, mounted() { }, methods: {  showPrint() {   this.dialogVisible = true  },  handlePrintDialogCancel() {   this.dialogVisible = false  },  doPrint(row) {   this.HtmlData = row.name   this.$refs.print.print(row.name)  } }}</script><style>#app { font-family: 'Avenir', Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #2c3e50; margin-top: 60px;}</style>

APP.VUE 每次點擊打印按鈕后觸發(fā)組件的print方法并將數(shù)據(jù)傳過去 this.$refs.print.print(row.name)
printer.vue 查詢打印機,然后調(diào)用打印方法printRender。

<template> <div class="container">  <webview id="printWebview" ref="printWebview" :src="fullPath" nodeintegration />  <printDialog :dialog-visible="dialogVisible" @cancel="handlePrintDialogCancel" @select-print="printSelectAfter" /> </div></template><script>import { ipcRenderer } from 'electron'import path from 'path'import printDialog from './PrintDialog.vue'export default { name: 'Pinter', components: {  printDialog }, props: {  // HtmlData: {  //  type: String,  //  default: '',  // }, }, data() {  return {   printList: [],   dialogVisible: false,   printDeviceName: '',   fullPath: path.join(__static, 'print.html'),   messageBox: null,   htmlData: ''  } }, mounted() {  const webview = this.$refs.printWebview  webview.addEventListener('ipc-message', (event) => {   if (event.channel === 'webview-print-do') {    console.log(this.printDeviceName)    webview.print(     {      silent: true,      printBackground: true,      deviceName: this.printDeviceName     },     (data) => {      this.messageBox.close()      if (data) {       this.$emit('complete')      } else {       this.$emit('cancel')      }     },    )   }  }) }, methods: {  print(val) {   this.htmlData = val   this.getPrintListHandle()  },  // 獲取打印機列表  getPrintListHandle() {   // 改用ipc異步方式獲取列表,解決打印列數(shù)量多的時候?qū)е驴ㄋ赖膯栴}   ipcRenderer.send('getPrinterList')   ipcRenderer.once('getPrinterList', (event, data) => {    // 過濾可用打印機    this.printList = data.filter(element => element.status === 0)    // 1.判斷是否有打印服務(wù)    if (this.printList.length <= 0) {     this.$message({      message: '打印服務(wù)異常,請嘗試重啟電腦',      type: 'error'     })     this.$emit('cancel')    } else {     this.checkPrinter()    }   })  },  // 2.判斷打印機狀態(tài)  checkPrinter() {   // 本地獲取打印機   const printerName = this.$electronStore.get('printForm') || ''   const printer = this.printList.find(device => device.name === printerName)   // 有打印機設(shè)備并且狀態(tài)正常直接打印   if (printer && printer.status === 0) {    this.printDeviceName = printerName    this.printRender()   } else if (printerName === '') {    this.$message({     message: '請先設(shè)置其他打印機',     type: 'error',     duration: 1000,     onClose: () => {      this.dialogVisible = true     }    })    this.$emit('cancel')   } else {    this.$message({     message: '當(dāng)前打印機不可用,請重新設(shè)置',     type: 'error',     duration: 1000,     onClose: () => {      this.dialogVisible = true     }    })   }  },  handlePrintDialogCancel() {   this.$emit('cancel')   this.dialogVisible = false  },  printSelectAfter(val) {   this.dialogVisible = false   this.$electronStore.set('printForm', val.name)   this.printDeviceName = val.name   this.printRender()  },  printRender(html) {   this.messageBox = this.$message({    message: '打印中,請稍后',    duration: 0   })   // 獲取<webview>節(jié)點   const webview = this.$refs.printWebview   // 發(fā)送信息到<webview>里的頁面   webview.send('webview-print-render', {    printName: this.printDeviceName,    html: this.htmlData   })  } }}</script><style scoped>.container { position: fixed; right: -500px;}</style>

public/print.html渲染webview頁面成功后發(fā)送打印指令

 <script>  const { ipcRenderer } = require('electron')  ipcRenderer.on('webview-print-render', (event, info) => {   // 執(zhí)行渲染   document.getElementById('bd').innerHTML = info.html   ipcRenderer.sendToHost('webview-print-do')  }) </script> 

這里用到了electron-store存取本地數(shù)據(jù)

background.js 引入 初始化掛載在global

import ElectronStore from 'electron-store'// ElectronStore 默認(rèn)數(shù)據(jù)import electronDefaultData from './config/electron-default-data'let electronStoreapp.on('ready', async() => { // 初始化配置文件 electronStore = new ElectronStore({  defaults: electronDefaultData,  cwd: app.getPath('userData') }) global.electronStore = electronStore})

src/plugins/inject.js

注冊$electronStore

// eslint-disable-next-lineimport { remote } from 'electron'export default { /* eslint no-param-reassign: "error" */ install(Vue) {  Vue.prototype.$electronStore = remote.getGlobal('electronStore')  }}

然后你就可以在vue文件里讀取了

this.$electronStore.get('printForm') 和 this.$electronStore.set('printForm', val.name)

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 久草最新在线 | 一区二区三区手机在线观看 | 爱爱视频天天干 | 亚洲第一页综合 | 黄色的视频在线观看 | 国产资源在线看 | 92看片淫黄大片一级 | 国产资源在线播放 | 特黄一区二区三区 | 精品国产一区二区三区天美传媒 | 免费观看三级毛片 | www.狠狠操.com | 高清视频91 | 久久国产精品99国产 | 精品国产91久久久久久久妲己 | 九九热在线视频观看 | 免费在线观看国产精品 | 经典三级av在线 | 国产亚洲激情 | 娇妻被各种姿势c到高潮小说 | 久久久久久久91 | 国产精品久久久久久久娇妻 | 中国的免费的视频 | 午夜a狂野欧美一区二区 | 一级毛片免费版 | 久久免费视频一区二区三区 | 91九色视频在线播放 | 亚洲第一页夜 | 92看片淫黄大片欧美看国产片 | 天天色人人爱 | 久久久www成人免费精品 | hdjapanesemassagehd日本 | 国产精品毛片无码 | 久久久久久久黄色片 | 高清国产午夜精品久久久久久 | 日本在线视 | 激情亚洲一区二区 | 精品国产一区二区三区久久久 | 欧美性成人 | 热re91久久精品国产99热 | 国产精品免费一区二区三区都可以 |