electron+vue3+vite2 如何使用打印

electron打印大概有两种:
第一种:通过window的webcontent对象,使用此种方式需要单独开出一个打印的窗口,可以将该窗口隐藏,但是通信调用相对复杂。
第二种:使用页面的webview元素调用打印,可以将webview隐藏在调用的页面中,通信方式比较简单。
两个对象调用打印方法的使用方式都一样。
本文是通过第二种方法实现静默打印。

注意: 渲染进程通信需要使用ipcRenderer,页面只能通过require引入ipcRenderer,必须得支持node,如果你的electron版本 > 6.0的话使用webview需要配置webviewTag。我的配置如下

new BrowserWindow({
   webPreferences: {
      // 允许使用node
      nodeIntegration: true,
      contextIsolation: false,
      // 允许使用remote
      enableRemoteModule: true,
      // 允许跨域
      webSecurity: false,
      // 允许使用webview
      webviewTag: true
    }
  })

1. 首先获取打印机列表

在需要获取打印机列表的页面请求获取


const { ipcRenderer } = require("electron")

// 发送给主进程消息
ipcRenderer.send('allPrint')

ipcRenderer.once('printName',(event,data)=>{
   console.log(data) // data就是返回的打印机数据列表
})

主进程 main.js 中2     

// 监听渲染进程发送的消息
ipcMain.on('allPrint',()=>{
  // 获取到打印机列表
  const printers = win.webContents.getPrinters()
  // 把获取的打印机列表发送给渲染进程
  win.webContents.send('printName',printers)
})

由于我是再page1渲染进程中又打开的子渲染进程page2 (若只有page1渲染进程可以跳过这一步)

 // 监听获取打印机列表中发送给page2
ipcRenderer.on('printName',(event, data)=>{
     page2Win.webContents.send('printName', data)
})

 2.页面引用webview

<template>
    <el-button @click="printView">开始打印</el-button>
    <webview ref="printWebview" :src="url" nodeintegration hidden>
</template>
<script>
    import { defineComponent, reactive, toRefs, onMounted, ref, unref} from "vue"
    import config from "@/common/lib/config"
    const { ipcRenderer } = require("electron")
  
    export default defineComponent({
        setup(){
            
            let data = reactive({
               url: process.env.CVE_ENV === 'development' ? config.dev_url.print : config.pro_url.print
            })

            const printWebview = ref(null)
            onMounted(()=>{
                getPrint()
            })

            const printView = () =>{
                  unref(printWebview).print({
                    //是否是静默打印
                    silent: true,
                    printBackground: true,
                    deviceName: '你的获取的打印机列表的打印机名称',
                  })
            }

            //获取打印机列表
            const getPrint = () => {
                ipcRenderer.send('allPrint')
                ipcRenderer.once('printName',(event,data)=>{
                    console.log(data) //data就是返回的打印机数据列表
                })
            }

           
            return {
                ...toRefs(data),
                printWebview,
                printView
            }
        }
    })
</script>

 当然看完上面的你可能不知道webview引入的页面从哪里来,接着往下看

3.配置vite.config.js

export default defineConfig({
  // publicDir,
  resolve: {
    // 目录别名配置
    alias: {
      "@": path.resolve(__dirname, "src")
    },
  },
  build:{
    rollupOptions: {
      // 多页应用配置
      input: {
        main: path.resolve(__dirname, 'index.html'), // 主应用入口
        // 这里这个打印页面需要再vite中配置多页面应用
        settle: path.resolve(__dirname, 'projects/settle/index.html'), // 结算中心
        print: path.resolve(__dirname, 'projects/settle/print.html') // 小票打印页面
      },
      external: ['electron'], // 告诉 Rollup 不要去打包 electron
    }
  },
  optimizeDeps: {
    exclude: ['electron'], // 告诉 Vite 不要转换 electron
  },
  plugins: [
    vue({
        script: {
            refSugar: true
        }
    })
  ]
})

然后再你的根目录下创建projects文件夹,再projects文件夹下面创建settle文件夹 settle文件夹下面创建print.html 这个入口文件地址和你vite配置的多页应用入口文件地址对应就行

附上我上面使用webview开发环境和编译后的环境地址

// 开发环境运行地址
    dev_url: {
        print: `http://localhost:${process.env.CVE_PORT || 3000}/projects/settle/print.html`,
    },
    // 生产环境运行地址
    pro_url:{
        print: "app://./projects/settle/print.html"

    }

最后附上print.html内容

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <meta http-equiv="X-UA-Compatible" content="ie=edge">
 <title>电子小票</title>
</head>
<body id='bd'>
   张三
</body>
<script>
  //引入ipcRenderer对象
 const {ipcRenderer} = require('electron')

 //监听渲染线程传过来的webview-print-render事件
 ipcRenderer.on('webview-print-render', (event, deviceInfo) => {
   console.log(deviceInfo)
    //当图片插入到页面后,通过ipcRenderer对象的sendToHost方法和渲染线程通讯,告诉渲染线程打印的内容已经准备完毕,请开始打印操作
    ipcRenderer.sendToHost('webview-print-do')
 })
</script>
</html>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
Electron是一个开源框架,用于构建跨平台的桌面应用程序,结合Vue.js和Vite(一个快速的前端构建工具)可以让您创建现代、高性能的Web视图层,嵌入到 Electron 应用中。RTSP(Real-Time Streaming Protocol)是一种网络协议,用于实时视频流传输。 要在 Electron + Vue3 + Vite 中实现 RTSP 播放,您可以按照以下步骤操作: 1. **安装依赖**: - 安装 `vue-video-player` 或类似的库,它提供了处理媒体流的功能,如 `vue-streaming-api`。 - 如果需要,安装支持 RTSP 解码的库,比如 `fluent-ffmpeg` 或 `mediasoup-client`。 ```bash npm install vue-video-player fluent-ffmpeg --save ``` 2. **设置配置**: 在项目中创建一个配置文件(例如 `config.js`),用于管理 RTSP 地址等信息。 ```javascript export default { rtspsource: 'rtsp://your_rtspserver_url/stream' } ``` 3. **在 Vue 组件中使用**: 使用 Vue 插件或组件内的方法加载并播放 RTSP 流。例如,使用 `vue-video-player`: ```html <template> <div id="player"> <VideoPlayer v-bind:src="config.rtspsource"></VideoPlayer> </div> </template> <script> import VideoPlayer from 'vue-video-player'; import { defineComponent, ref } from 'vue'; import config from '@/config'; export default defineComponent({ components: { VideoPlayer, }, setup() { const player = ref(null); // 当组件挂载后尝试播放 mounted() { player.value.play(); } return { player, }; }, }); </script> ``` 4. **错误处理**: 添加错误处理代码来捕获并处理播放过程中可能出现的问题,如连接失败、编码不兼容等。 5. **优化和调整**: 根据应用需求可能还需要对视频质量、缓冲策略等进行优化,并确保在不同平台上运行稳定。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值