新增增加打印机

This commit is contained in:
gyq
2024-04-03 15:57:58 +08:00
parent da00851195
commit 07d7df0416
19 changed files with 1562 additions and 540 deletions

View File

@@ -1,6 +1,5 @@
import path from "path";
import { app, BrowserWindow, ipcMain } from "electron";
const { PosPrinter } = require("electron-pos-printer-vue");
let win;
app.whenReady().then(() => {
@@ -40,118 +39,62 @@ app.whenReady().then(() => {
app.quit();
});
// 给渲染进程返回打印机列表
ipcMain.on('getPrintList', () => {
win.webContents.getPrintersAsync().then(res => {
win.webContents.send('printList', res)
})
})
// 创建打印小票子窗口
// const printWin = new BrowserWindow({
// show: true,
// webPreferences: {
// // 集成网页和 Node.js也就是在渲染进程中可以调用 Node.js 方法
// nodeIntegration: true,
// contextIsolation: false,
// },
// });
const printWin = new BrowserWindow({
show: true,
width: 464,
height: 2206,
webPreferences: {
// 集成网页和 Node.js也就是在渲染进程中可以调用 Node.js 方法
nodeIntegration: true,
contextIsolation: false,
},
});
// if (process.env.VITE_DEV_SERVER_URL) {
// // 加载打印的html文件
// printWin.loadFile(path.join(__dirname, "../public/print.html"));
// } else {
// printWin.loadFile(path.resolve(__dirname, "../dist/print.html")); // 打包后使用文件路径访问应用
// }
if (process.env.VITE_DEV_SERVER_URL) {
// 加载打印的html文件
printWin.loadFile(path.join(__dirname, "../public/print.html"));
} else {
printWin.loadFile(path.resolve(__dirname, "../dist/print.html")); // 打包后使用文件路径访问应用
}
// win.webContents.getPrintersAsync().then(res => {
// // console.log('list', res)
// })
// ipcMain.on("printerInfoSync", (event, params) => {
// // console.log(JSON.parse(params))
// printWin.webContents.send("getParams", params);
// });
// 接收订单页面发过来的参数发送给打印页
ipcMain.on('printerInfoSync', (event, arg) => {
printWin.webContents.send('getParams', arg)
})
// 执行打印操作
ipcMain.on('printStart', (event, arg) => {
console.log('开始打印', arg)
let name = 'Xprinter XP-365B'
// let name = 'Generic / Text Only'
const options = {
printWin.on('printStart', (event, arg) => {
const _parmas = JSON.parse(arg)
let name = _parmas.deviceName
printWin.webContents.print({
silent: true,
preview: false, // width of content body
margin: '0 0 0 0', // margin of content body
copies: 1, // Number of copies to print
printerName: name, // printerName: string, check with webContent.getPrinters()
// timeOutPerLine: 400,
pageSize: { height: 22860, width: 30480 } // page size
}
const data = [
{
type: 'text', // 'text' | 'barCode' | 'qrCode' | 'image' | 'table
value: '#B5 B6',
style: { "font-weight": "700", "font-size": "18px" }
}, {
type: 'text', // 'text' | 'barCode' | 'qrCode' | 'image' | 'table
value: '#B5 B6',
style: { "font-weight": "700", "font-size": "18px" }
}, {
type: 'text', // 'text' | 'barCode' | 'qrCode' | 'image' | 'table
value: '#B5 B6',
style: { "font-weight": "700", "font-size": "18px" }
}, {
type: 'text', // 'text' | 'barCode' | 'qrCode' | 'image' | 'table
value: '#B5 B6',
style: { "font-weight": "700", "font-size": "18px" }
}, {
type: 'text', // 'text' | 'barCode' | 'qrCode' | 'image' | 'table
value: '#B5 B6',
style: { "font-weight": "700", "font-size": "18px" }
}, {
type: 'text', // 'text' | 'barCode' | 'qrCode' | 'image' | 'table
value: '#B5 B6',
style: { "font-weight": "700", "font-size": "18px" }
}, {
type: 'text', // 'text' | 'barCode' | 'qrCode' | 'image' | 'table
value: '#B5 B6',
style: { "font-weight": "700", "font-size": "18px" }
}, {
type: 'text', // 'text' | 'barCode' | 'qrCode' | 'image' | 'table
value: '#B5 B6',
style: { "font-weight": "700", "font-size": "18px" }
}, {
type: 'text', // 'text' | 'barCode' | 'qrCode' | 'image' | 'table
value: '#B5 B6',
style: { "font-weight": "700", "font-size": "18px" }
deviceName: name,
pageSize: {
width: 58000,
height: 276000
},
{
type: 'text', // 'text' | 'barCode' | 'qrCode' | 'image' | 'table'
value: '喔喔奶茶 x100',
style: { "font-size": "18px" },
scaleFactor: 80,
landscape: false,
margins: {
marginType: "none",
top: 0,
bottom: 0,
left: 0,
right: 0
},
{
type: 'text', // 'text' | 'barCode' | 'qrCode' | 'image' | 'table'
value: '麻辣味',
style: { "font-size": "18px" },
},
{
type: 'text', // 'text' | 'barCode' | 'qrCode' | 'image' | 'table'
value: '03-08 16:32 ¥123.00',
style: { "font-size": "18px" },
dpi: {
horizontal: 203,
vertical: 203
}
]
PosPrinter.print(data, options)
.then(() => { })
.catch((error) => {
console.error(error);
});
// printWin.webContents.print({
// silent: true,
// deviceName: name,
// pageSize: {
// width: 30480,
// height: 22860
// }
// })
})
})
});