electron+vuecli3 實現設置打印機,靜默打印小票功能html
網上相關的資料比較少,這裏給你們分享一下,但願你們能夠少踩一些坑
github地址前端
必需要強調一下的是electron的版本必須是3.0.0不能,我嘗試了4和5都沒有實現vue
git clone https://github.com/sunnie1992/electron-vue-print-demo.git
npm install
npm run electron:serve
複製代碼
操做思路
1.用戶點擊打印
2.查詢本地electron-store(用來向本地存儲,讀取數據)是否存打印機名稱
3.已經設置,直接打印
4.沒有設置,彈出設置打印機框
5.用戶設置好確認後打印node
首頁App.vue引入了兩個組件,一個是主動設置打印機的彈出printDialoggit
另一個是打印組件,打印是經過webview將須要打印的內容渲染到html頁面而後就能打印了github
<template>
<div id="app">
<el-button type="primary" @click="showPrint">設置打印機</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: '上海市浦東新區',
tag: '家'
}, {
date: '2016-05-04',
name: '我是小仙女1',
address: '上海市浦東新區',
tag: '公司'
}, {
date: '2016-05-01',
name: '我是小仙女2',
address: '上海市浦東新區',
tag: '家'
}, {
date: '2016-05-03',
name: '我是小仙女3',
address: '上海市浦東新區',
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 每次點擊打印按鈕後觸發組件的print方法並將數據傳過去 this.$refs.print.print(row.name)
printer.vue 查詢打印機,而後調用打印方法printRender。web
<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異步方式獲取列表,解決打印列數量多的時候致使卡死的問題
ipcRenderer.send('getPrinterList')
ipcRenderer.once('getPrinterList', (event, data) => {
// 過濾可用打印機
this.printList = data.filter(element => element.status === 0)
// 1.判斷是否有打印服務
if (this.printList.length <= 0) {
this.$message({
message: '打印服務異常,請嘗試重啓電腦',
type: 'error'
})
this.$emit('cancel')
} else {
this.checkPrinter()
}
})
},
// 2.判斷打印機狀態
checkPrinter() {
// 本地獲取打印機
const printerName = this.$electronStore.get('printForm') || ''
const printer = this.printList.find(device => device.name === printerName)
// 有打印機設備而且狀態正常直接打印
if (printer && printer.status === 0) {
this.printDeviceName = printerName
this.printRender()
} else if (printerName === '') {
this.$message({
message: '請先設置其餘打印機',
type: 'error',
duration: 1000,
onClose: () => {
this.dialogVisible = true
}
})
this.$emit('cancel')
} else {
this.$message({
message: '當前打印機不可用,請從新設置',
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>節點
const webview = this.$refs.printWebview
// 發送信息到<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頁面成功後發送打印指令npm
<script>
const { ipcRenderer } = require('electron')
ipcRenderer.on('webview-print-render', (event, info) => {
// 執行渲染
document.getElementById('bd').innerHTML = info.html
ipcRenderer.sendToHost('webview-print-do')
})
</script>
複製代碼
這裏用到了electron-store存取本地數據
background.js 引入 初始化掛載在global微信
import ElectronStore from 'electron-store'
// ElectronStore 默認數據
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
註冊$electronStoremarkdown
// 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.set('printForm', val.name)
獲取更多技術相關文章,關注公衆號」前端女塾「。
回覆前端,便可加入」前端仙女羣「
您能夠掃描添加下方的微信並備註 Sol 加前端交流羣,交流學習。
若是對你有幫助送我一顆小星星(づ ̄3 ̄)づ╭❤~