electron实现静默打印的示例代码
作者:乖摸摸头 发布时间:2024-05-09 15:25:14
标签:electron,静默打印
前言
electron+vuecli3 实现设置打印机,静默打印小票功能
网上相关的资料比较少,这里给大家分享一下,希望大家可以少踩一些坑
github地址
必须要强调一下的是electron的版本必须是3.0.0不能,我尝试了4和5都没有实现
效果图
使用
git clone https://github.com/sunnie1992/electron-vue-print-demo.git
npm install
npm run electron:serve
实现
操作思路
1.用户点击打印
2.查询本地electron-store(用来向本地存储,读取数据)是否存打印机名称
3.已经设置,直接打印
4.没有设置,弹出设置打印机框
5.用户设置好确认后打印
首页App.vue引入了两个组件,一个是主动设置打印机的弹出printDialog
另外一个是打印组件,打印是通过webview将需要打印的内容渲染到html页面然后就能打印了
<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。
<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页面成功后发送打印指令
<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
注册$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)
来源:https://segmentfault.com/a/1190000020041317


猜你喜欢
- 本文实例讲述了Python常见MongoDB数据库操作。分享给大家供大家参考,具体如下:MongoDB 是一个基于分布式文件存储的数据库。由
- 前言我们在写sql语句的时候,总是无法避免使用到连接关键词,比如内连接、外连接。种类是很多的,我在这里贴上一张在别处找到的图:这张图我认为是
- 如何在线创建新表?下面我们以建立一个数码相机库用表为例,看看在ASP程序代码中使用 [CREATE TABLE 相机 (品牌 TEXT(10
- 当系统出现故障时,只要存在数据日志那么就可以利用它来恢复数据解决数据库故障。作为SQL Server数据库管理员,了解数据日志文件的作用,以
- 之前在实现表单中file类型input选择多图片的时候找到一种方式 也许不是最好的但亲测可行且支持ie7以上以及chrome浏览器在表单中使
- 本文介绍了vue下history模式刷新后404错误解决方法,分享给大家,具体如下:官方说明文档:https://router.vuejs.
- 有别于JS跨域、IFRAME跨域等的常用处理办法,还可以利用P3P来实现跨域。P3P是什么P3P(Platform for Privacy
- I. 简介MoviePy 是什么?MoviePy 是一个使用 Python 编写的开源库,用于在视频编辑中创建、编辑和操作视频文件。它是一款
- 多线程多线程是个提高程序运行效率的好办法,本来要顺序执行的程序现在可以并行执行,可想而知效率要提高很多。但是多线程也不是能提高所有程序的效率
- 这个可应用于所有浏览器中.<SCRIPT language=javascript>var leave=true; functio
- 起步在 《分布式任务队列Celery使用说明》 中介绍了在 Python 中使用 Celery 来实验异步任务和定时任务功能。本文介绍如何在
- 本文实例为大家分享了Python OpenCV实现视频追踪的具体代码,供大家参考,具体内容如下1. MeanShift假设有一堆点集和一个圆
- 使用Pycharm的时候需要导入解释器然后安装一些第三方库,讲道理都是project Interpreter里面直接install的。但是打
- 静态文件配置概述:静态文件交由Web服务器处理,Django本身不处理静态文件。简单的处理逻辑如下(以nginx为例):URI请求 --&g
- 现在很多CMS系统因为安全原因会把后台编辑器里的上传功能给去除,但这样一来对实际使用过程造成了很多麻烦,今天我们以ASPCMS系统的FCKe
- 目录批量修改文件名(保留后缀)批量修改文件名(全改)读取文件下的所有文件名总结批量修改文件名(保留后缀)这种方法,保留了文件原本的后缀。这里
- 1、词表映射无论是深度学习还是传统的统计机器学习方法处理自然语言,都需要先将输入的语言符号(通常为标记Token),映射为大于等于0、小于词
- 在mysql网站时拿到的rpm包只能用root安装,不支持relocate用源码安装后./configure --prefix=/home/
- 在许多人看来,HTML应该是WEB制作所有语言中最简单的语言,因为它不需要编译、封闭等,甚至只需要一个记事本就可以让其在浏览器中呈现出来。所
- 在用python画散点图的时候想标记出特定的点,比如在某些点的外围加个空心圆,一样可以通过plt.scatter实现import matpl