亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時(shí)間:2019年08月12日 14:29:13   作者:乖摸摸頭  
這篇文章主要介紹了electron實(shí)現(xiàn)靜默打印的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

前言

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

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

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

效果圖


使用

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

實(shí)現(xiàn)

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

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

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

<template>
 <div id="app">
  <el-button type="primary" @click="showPrint">設(shè)置打印機(jī)</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 每次點(diǎn)擊打印按鈕后觸發(fā)組件的print方法并將數(shù)據(jù)傳過(guò)去 this.$refs.print.print(row.name)
printer.vue 查詢打印機(jī),然后調(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()
  },
  // 獲取打印機(jī)列表
  getPrintListHandle() {
   // 改用ipc異步方式獲取列表,解決打印列數(shù)量多的時(shí)候?qū)е驴ㄋ赖膯?wèn)題
   ipcRenderer.send('getPrinterList')
   ipcRenderer.once('getPrinterList', (event, data) => {
    // 過(guò)濾可用打印機(jī)
    this.printList = data.filter(element => element.status === 0)
    // 1.判斷是否有打印服務(wù)
    if (this.printList.length <= 0) {
     this.$message({
      message: '打印服務(wù)異常,請(qǐng)嘗試重啟電腦',
      type: 'error'
     })
     this.$emit('cancel')
    } else {
     this.checkPrinter()
    }
   })
  },
  // 2.判斷打印機(jī)狀態(tài)
  checkPrinter() {
   // 本地獲取打印機(jī)
   const printerName = this.$electronStore.get('printForm') || ''
   const printer = this.printList.find(device => device.name === printerName)
   // 有打印機(jī)設(shè)備并且狀態(tài)正常直接打印
   if (printer && printer.status === 0) {
    this.printDeviceName = printerName
    this.printRender()
   } else if (printerName === '') {
    this.$message({
     message: '請(qǐng)先設(shè)置其他打印機(jī)',
     type: 'error',
     duration: 1000,
     onClose: () => {
      this.dialogVisible = true
     }
    })
    this.$emit('cancel')
   } else {
    this.$message({
     message: '當(dāng)前打印機(jī)不可用,請(qǐ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: '打印中,請(qǐng)稍后',
    duration: 0
   })
   // 獲取<webview>節(jié)點(diǎn)
   const webview = this.$refs.printWebview
   // 發(fā)送信息到<webview>里的頁(yè)面
   webview.send('webview-print-render', {
    printName: this.printDeviceName,
    html: this.htmlData
   })
  }
 }
}
</script>
<style scoped>
.container {
 position: fixed;
 right: -500px;
}
</style>

public/print.html渲染webview頁(yè)面成功后發(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存取本地?cái)?shù)據(jù)

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

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

src/plugins/inject.js

注冊(cè)$electronStore

// eslint-disable-next-line
import { 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)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解vue 單頁(yè)應(yīng)用(spa)前端路由實(shí)現(xiàn)原理

    詳解vue 單頁(yè)應(yīng)用(spa)前端路由實(shí)現(xiàn)原理

    這篇文章主要介紹了詳解vue 單頁(yè)應(yīng)用(spa)前端路由實(shí)現(xiàn)原理,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • Vue Echarts實(shí)現(xiàn)可視化世界地圖代碼實(shí)例

    Vue Echarts實(shí)現(xiàn)可視化世界地圖代碼實(shí)例

    這篇文章主要介紹了Vue Echarts實(shí)現(xiàn)可視化世界地圖,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • vue?element-ui動(dòng)態(tài)橫向統(tǒng)計(jì)表格的實(shí)現(xiàn)

    vue?element-ui動(dòng)態(tài)橫向統(tǒng)計(jì)表格的實(shí)現(xiàn)

    這篇文章主要介紹了vue?element-ui動(dòng)態(tài)橫向統(tǒng)計(jì)表格的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • vue過(guò)渡和animate.css結(jié)合使用詳解

    vue過(guò)渡和animate.css結(jié)合使用詳解

    本篇文章主要介紹了vue過(guò)渡和animate.css結(jié)合使用詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-06-06
  • springboot?vue接口測(cè)試前端動(dòng)態(tài)增刪表單功能實(shí)現(xiàn)

    springboot?vue接口測(cè)試前端動(dòng)態(tài)增刪表單功能實(shí)現(xiàn)

    這篇文章主要為大家介紹了springboot?vue接口測(cè)試前端動(dòng)態(tài)增刪表單功能實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • vue-cli使用stimulsoft.reports.js的詳細(xì)教程

    vue-cli使用stimulsoft.reports.js的詳細(xì)教程

    Stimulsoft?Reports.JS是一個(gè)使用JavaScript和HTML5生成報(bào)表的平臺(tái)。它擁有所有擁來(lái)設(shè)計(jì),編輯和查看報(bào)表的必需組件。該報(bào)表工具根據(jù)開(kāi)發(fā)人員數(shù)量授權(quán)而不是根據(jù)應(yīng)用程序的用戶數(shù)量。接下來(lái)通過(guò)本文給大家介紹vue-cli使用stimulsoft.reports.js的方法,一起看看吧
    2021-12-12
  • vue同步父子組件和異步父子組件的生命周期順序問(wèn)題

    vue同步父子組件和異步父子組件的生命周期順序問(wèn)題

    這篇文章主要介紹了vue同步父子組件和異步父子組件的生命周期順序問(wèn)題,需要的朋友可以參考下
    2018-10-10
  • vue中map()快速使用方法小結(jié)

    vue中map()快速使用方法小結(jié)

    map()函數(shù)是在JS的數(shù)組中定義的,它返回一個(gè)新的數(shù)組,下面這篇文章主要給大家介紹了關(guān)于vue中map()快速使用的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-12-12
  • 從零實(shí)現(xiàn)一個(gè)vue文件解析器

    從零實(shí)現(xiàn)一個(gè)vue文件解析器

    本文就討論下怎么實(shí)現(xiàn)一個(gè)處理.vue文件的loader,以及用loader處理完.vue文件怎么把內(nèi)容渲染在瀏覽器上并實(shí)現(xiàn)簡(jiǎn)單的響應(yīng)式,對(duì)vue文件解析器相關(guān)知識(shí)感興趣的朋友一起看看吧
    2022-06-06
  • Vue 自定義標(biāo)簽的src屬性不能使用相對(duì)路徑的解決

    Vue 自定義標(biāo)簽的src屬性不能使用相對(duì)路徑的解決

    這篇文章主要介紹了Vue 自定義標(biāo)簽的src屬性不能使用相對(duì)路徑的解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09

最新評(píng)論