Mrcdh技术博客 Mrcdh技术博客
首页
  • Html5
  • Javascript
  • Nodejs
  • electron
  • Android
  • 微信公众号
  • 框架
  • 其他
  • Mysql
  • PHP
  • Python
  • java
  • Gulp
  • 其它
  • 网站
  • 资源
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Mrcdh

全栈开发小学生
首页
  • Html5
  • Javascript
  • Nodejs
  • electron
  • Android
  • 微信公众号
  • 框架
  • 其他
  • Mysql
  • PHP
  • Python
  • java
  • Gulp
  • 其它
  • 网站
  • 资源
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • Html5

  • JavaScript

  • Nodejs

  • express

  • electron

    • electron窗体操作
    • electron应用更新
    • 02.electron打包操作(electron-builder)
    • electron调用打印机打印
    • electron和页面通信
    • electron文件缓存
    • electron常见问题
  • Android

  • 微信公众号

  • 框架

  • 其他

  • selenium

  • Sequelize

  • 大前端
  • electron
mrcdh
2022-11-29

electron调用打印机打印

electron main.js

const {app, BrowserWindow, ipcMain} = require('electron')


function createWindow(){
    const win = new BrowserWindow({
        width: 800,
        height: 600,
        webPreferences:{
            nodeIntegration: true,
            contextIsolation: false,
            webviewTag: true // 这个必须设置,不然webview无法使用
        }
    })

    win.webContents.openDevTools()

    win.loadFile('index.html')
    
    // 监听获取打印机列表
    ipcMain.on('getPrinterList', event => {
        win.webContents.getPrintersAsync().then(res => {
            // 告知列表
            win.webContents.send('getPrinterList', res)
        })
    })

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

首页 index.html

我的提示

注意webview的nodeintegration和webpreferences="contextIsolation=no"属性,通过这两个属性让打印页面支持require

<div id="app">
    <h3>打印机列表(默认打印机:{{printerName}})</h3>
    <ul>
        <li v-for="item in printerSelection" :key="item.name">
            <input type="radio" v-model="printerName" :value="item.name"> {{item.name}}
        </li>
    </ul>
    <webview id="printWebview" ref="printWebview" webpreferences="contextIsolation=no" src="./print.html"
        nodeintegration style="width:100px;height:650px;display:none"></webview>
    <button @click="onPrint">打印</button>
</div>
<script>
    const { ipcRenderer } = require('electron')
    new Vue({
        el: '#app',
        data() {
            return {
                printerSelection: [],
                printerName: ''
            }
        },
        created() {
            // 打印机
            // 渲染线程主动发送getPrinterList事件到主线程请求打印机列表
            ipcRenderer.send('getPrinterList')
            // 监听主线程获取到打印机列表后的回调
            ipcRenderer.once('getPrinterList', (event, data) => {
                // data就是打印机列表
                for (var i = 0; i < data.length; i++) {
                    this.printerSelection.push(data[i])
                    // 设置默认打印机
                    if (data[i].isDefault) {
                        this.printerName = data[i].name
                    }
                }
            })
        },
        mounted() {
            const webview = this.$refs.printWebview;
            // 监听ipc消息
            webview.addEventListener("ipc-message", (event) => {
                // 如果是打印页面发送的打印消息,则打印
                if (event.channel === "webview-print-do") {
                    webview.print({
                        silent: true,//静默打印
                        printBackground: true,
                        deviceName: this.printerName, //打印机名称
                    }).then((res) => {
                        console.log(res);
                    }).catch((err) => {
                        console.log(err);
                    });
                }
            });
            // 5秒后打印一次
            setTimout(() => {
                this.onPrint()
            }, 5000)
        },
        methods: {
            onPrint() {
                const webview = this.$refs.printWebview;
                // 告知打印页面渲染打印信息
                webview.send("webview-print-render", {name:'打印测试'})
            }
        }
    })
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

打印页面 print.html

<div>
    打印测试,你好:<span id="name"></span>
</div>
<script>
    const { ipcRenderer } = require('electron')
    // 监听渲染事件
    ipcRenderer.on('webview-print-render', (event, data) => {
        document.getElementById('name').innerText = data.name
        // 告知渲染完成,可以打印
        ipcRenderer.sendToHost('webview-print-do')
    })
</script>
1
2
3
4
5
6
7
8
9
10
11
12
#electron
上次更新: 2023/09/22, 16:54:32
02.electron打包操作(electron-builder)
electron和页面通信

← 02.electron打包操作(electron-builder) electron和页面通信→

最近更新
01
uniapp常见问题
03-19
02
Vue3项目搭建
12-25
03
使用pnpm的monorepo组织项目
11-01
更多文章>
Theme by Vdoing | Copyright © 2020-2025 Mrcdh | 苏ICP备2020058908号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×