2020-09-02更新 版本比较低,请谨慎使用,我对electron研究比较少,小伙伴们可以加我微信,我有个专门的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页面然后就能打印了

设置打印机

打印

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)

}

}

}

#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;

}

APP.VUE 每次点击打印按钮后触发组件的print方法并将数据传过去 this.$refs.print.print(row.name)

printer.vue 查询打印机,然后调用打印方法printRender。

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

})

// 获取节点

const webview = this.$refs.printWebview

// 发送信息到里的页面

webview.send('webview-print-render', {

printName: this.printDeviceName,

html: this.htmlData

})

}

}

}

.container {

position: fixed;

right: -500px;

}

public/print.html渲染webview页面成功后发送打印指令

const { ipcRenderer } = require('electron')

ipcRenderer.on('webview-print-render', (event, info) => {

// 执行渲染

document.getElementById('bd').innerHTML = info.html

ipcRenderer.sendToHost('webview-print-do')

})

这里用到了electron-store存取本地数据

background.js 引入 初始化挂载在globalimport 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)

关于我

获取更多技术相关文章,关注公众号”前端女塾“,加我微信或加入”前端仙女群“。

如果对你有帮助送我一颗小星星(づ ̄3 ̄)づ╭❤~

html5静默打印_electron实现静默打印相关推荐

  1. java中使用pdfBox打印pdf;java web打印pdf;静默打印;jar程序打包成exe文件;exe4j的使用

    java编写打印控件 web打印一直是个棘手的问题,市面上的第三方打印插件也是贵的离谱,这里给出一个可行的解决方案. SpringBoot项目的web服务,实现的功能: 获取电脑可用的打印机列表 设置 ...

  2. 基于WEB的Office文档打印——浏览器中静默打印Word文档

    web应用开发中,如何集成Office文档打印功能,并不是一个容易实线的问题.现在有了打天下web打印插件,仅以几行JS代码就可以让你将Office打印功能集成到你的Web项目中. 引入PrintWo ...

  3. 使用java实现打印功能_java实现打印功能有没有比较好的方式?

    Java 实现打印文件 Java 原生打印: 有一下几种实现方式: 实现打印对象:实现Printable接口 使用工具箱自带的打印对象(Toolkit.getDefaultToolkit().getP ...

  4. vue-print-nb 实现页面打印(含分页打印)

    Web 实现页面打印 安装 官网地址:https://github.com/Power-kxLee/vue3-print-nb // 安装 打印组件 npm install vue-print-nb ...

  5. vue打印模版-自定义模版-局部打印/使用插件vue-print-nb打印,打印样式设置

    一.自定义模版打印功能: <template><div v-if="printVisible"><div id="printBox" ...

  6. 在C#里实现DATAGRID的打印预览和打印

    作者Blog:http://blog.csdn.net/qieyj/ 很多人都在论坛里问,如何实现DATAGRID的打印预览和打印,现在我就把这方面的源代码告诉大家.这段代码也花费了我1个晚上的时间, ...

  7. java web 打印控件_web打印,web打印控件,dotnet web打印控件,java web打印控件,webprint...

    webprint打印参数设置小插件,主要实现打印纸张,边距,打印机等的设置. 主要功能: 设置页眉页脚属性 设置打印页面左右上下边距 设置打印方向,打印份数,打印指定页 打印模版和指定纸型绑定 设置自 ...

  8. 打印容器_喷墨打印MnO?制备微型超级电容器

    转自微信公众号:柔性电子服务平台 作者:young 印刷电子作为柔性电子器件组装的一种新兴技术,引起人们的广泛地关注,包括有机晶体管.OLED和能量储存器件等.通过印刷工艺可是实现有机.无机纳米材料的 ...

  9. 【C 语言】数组 ( 验证二维数组内存是线性的 | 打印二维数组 | 以一维数组方式打印二维数组 | 打印二维数组值和地址 )

    文章目录 一.验证二维数组内存是线性的 1.打印二维数组 2.以一维数组方式打印二维数组 3.打印二维数组值和地址 二.完整代码示例 一.验证二维数组内存是线性的 验证二维数组内存是线性的 : 验证方 ...

最新文章

  1. 学软件测试的优势有哪些
  2. 2021年14项世界互联网领先科技成果发布
  3. MinHook - 最小化的 x86/x64 API 钩子库
  4. 08:Python数据分析之pandas学习
  5. 请教于国富律师——怎样把灰鸽子病毒和灰鸽子程序区分开
  6. 找找Amazon的A9算法更新和变化的蛛丝马迹!
  7. Cubieboard2裸机开发之(四)定时器操作
  8. DCMTK:dcmseg模块的辅助功能
  9. RISC-V应用创新大赛 | 一文详解RVB2601套件 助你快速上手赢比赛
  10. 一站式学习Redis 从入门到高可用分布式实践(慕课)第六章 Redis开发运维常见问题...
  11. 蓝桥杯 2011年第二届C语言初赛试题(2)
  12. python函数-装饰器
  13. linux服务器centos空间满的检查及解决方法
  14. python获取网站window全局对象或方法的返回值
  15. 四叶草社交平台——十天冲刺(7)
  16. HDU5620 KK's Steel【菲波拉契数列+水题】
  17. 【知识图谱系列】多关系异质知识图谱表示学习综述
  18. 进阶的阿牛哥之pandas透视表pivot_table的使用
  19. Python使用cairosvg将SVG转PNG设置dpi无效
  20. 百度网盘网页端的视频如何调节播放倍速?

热门文章

  1. 什么是局域网、广域网、城域网?
  2. 大咖齐聚CCIG论坛——文档图像智能分析的产业前沿
  3. 许嵩歌词里的值得深思的话
  4. 服务医学,基于目标检测模型实现细胞检测识别
  5. 这些css 动画效果你一定要收藏
  6. C++基础入门(上):命名空间、输入输出、缺省参数
  7. 2022-2028全球纸浆模塑机器行业调研及趋势分析报告
  8. 我的收藏中的十大开源论坛
  9. 【前端浏览器】浏览器缓存(http缓存) 浏览器本地存储(总结)
  10. 组合数学在计算机科学编码中的应用,组合数学的历史、方法及在生活中的应用...