日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > vue >内容正文

vue

electron+vue3+vite2 如何使用打印

發(fā)布時(shí)間:2024/1/8 vue 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 electron+vue3+vite2 如何使用打印 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

electron打印大概有兩種:
第一種:通過(guò)window的webcontent對(duì)象,使用此種方式需要單獨(dú)開出一個(gè)打印的窗口,可以將該窗口隱藏,但是通信調(diào)用相對(duì)復(fù)雜。
第二種:使用頁(yè)面的webview元素調(diào)用打印,可以將webview隱藏在調(diào)用的頁(yè)面中,通信方式比較簡(jiǎn)單。
兩個(gè)對(duì)象調(diào)用打印方法的使用方式都一樣。
本文是通過(guò)第二種方法實(shí)現(xiàn)靜默打印。

注意: 渲染進(jìn)程通信需要使用ipcRenderer,頁(yè)面只能通過(guò)require引入ipcRenderer,必須得支持node,如果你的electron版本 > 6.0的話使用webview需要配置webviewTag。我的配置如下

new BrowserWindow({webPreferences: {// 允許使用nodenodeIntegration: true,contextIsolation: false,// 允許使用remoteenableRemoteModule: true,// 允許跨域webSecurity: false,// 允許使用webviewwebviewTag: true}})

1. 首先獲取打印機(jī)列表

在需要獲取打印機(jī)列表的頁(yè)面請(qǐng)求獲取

const { ipcRenderer } = require("electron")// 發(fā)送給主進(jìn)程消息 ipcRenderer.send('allPrint')ipcRenderer.once('printName',(event,data)=>{console.log(data) // data就是返回的打印機(jī)數(shù)據(jù)列表 })

主進(jìn)程 main.js 中2?????

// 監(jiān)聽渲染進(jìn)程發(fā)送的消息 ipcMain.on('allPrint',()=>{// 獲取到打印機(jī)列表const?printers?=?win.webContents.getPrinters()// 把獲取的打印機(jī)列表發(fā)送給渲染進(jìn)程win.webContents.send('printName',printers) })

由于我是再page1渲染進(jìn)程中又打開的子渲染進(jìn)程page2 (若只有page1渲染進(jìn)程可以跳過(guò)這一步)

// 監(jiān)聽獲取打印機(jī)列表中發(fā)送給page2 ipcRenderer.on('printName',(event, data)=>{page2Win.webContents.send('printName', data) })

?2.頁(yè)面引用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: '你的獲取的打印機(jī)列表的打印機(jī)名稱',})}//獲取打印機(jī)列表const getPrint = () => {ipcRenderer.send('allPrint')ipcRenderer.once('printName',(event,data)=>{console.log(data) //data就是返回的打印機(jī)數(shù)據(jù)列表})}return {...toRefs(data),printWebview,printView}}}) </script>

?當(dāng)然看完上面的你可能不知道webview引入的頁(yè)面從哪里來(lái),接著往下看

3.配置vite.config.js

export default defineConfig({// publicDir,resolve: {// 目錄別名配置alias: {"@": path.resolve(__dirname, "src")},},build:{rollupOptions: {// 多頁(yè)應(yīng)用配置input: {main: path.resolve(__dirname, 'index.html'), // 主應(yīng)用入口// 這里這個(gè)打印頁(yè)面需要再vite中配置多頁(yè)面應(yīng)用settle: path.resolve(__dirname, 'projects/settle/index.html'), // 結(jié)算中心print: path.resolve(__dirname, 'projects/settle/print.html') // 小票打印頁(yè)面},external: ['electron'], // 告訴 Rollup 不要去打包 electron}},optimizeDeps: {exclude: ['electron'], // 告訴 Vite 不要轉(zhuǎn)換 electron},plugins: [vue({script: {refSugar: true}})] })

然后再你的根目錄下創(chuàng)建projects文件夾,再projects文件夾下面創(chuàng)建settle文件夾 settle文件夾下面創(chuàng)建print.html 這個(gè)入口文件地址和你vite配置的多頁(yè)應(yīng)用入口文件地址對(duì)應(yīng)就行

附上我上面使用webview開發(fā)環(huán)境和編譯后的環(huán)境地址

// 開發(fā)環(huán)境運(yùn)行地址dev_url: {print: `http://localhost:${process.env.CVE_PORT || 3000}/projects/settle/print.html`,},// 生產(chǎn)環(huán)境運(yùn)行地址pro_url:{print: "app://./projects/settle/print.html"}

最后附上print.html內(nèi)容

<!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對(duì)象const {ipcRenderer} = require('electron')//監(jiān)聽渲染線程傳過(guò)來(lái)的webview-print-render事件ipcRenderer.on('webview-print-render', (event, deviceInfo) => {console.log(deviceInfo)//當(dāng)圖片插入到頁(yè)面后,通過(guò)ipcRenderer對(duì)象的sendToHost方法和渲染線程通訊,告訴渲染線程打印的內(nèi)容已經(jīng)準(zhǔn)備完畢,請(qǐng)開始打印操作ipcRenderer.sendToHost('webview-print-do')}) </script> </html>

總結(jié)

以上是生活随笔為你收集整理的electron+vue3+vite2 如何使用打印的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。