优化打印效果

This commit is contained in:
gyq
2024-03-07 09:07:22 +08:00
parent 5d96435125
commit b550cf3fd8
12 changed files with 176 additions and 235 deletions

View File

@@ -1,12 +1,11 @@
import { app, BrowserWindow, ipcMain } from "electron";
import path from 'path'
import { printUtils } from './printUtils'
app.whenReady().then(() => {
const win = new BrowserWindow({
title: "银收客",
width: 1200,
height: 800,
width: 1024,
height: 768,
fullscreenable: true,
fullscreen: false,
simpleFullscreen: true,
@@ -22,10 +21,10 @@ app.whenReady().then(() => {
if (process.env.VITE_DEV_SERVER_URL) {
win.loadURL(process.env.VITE_DEV_SERVER_URL);
// 使用vite开发服务的url路径访问应用
// win.webContents.openDevTools();
} else {
win.loadFile(path.resolve(__dirname, '../dist/index.html')); // 打包后使用文件路径访问应用
}
win.webContents.openDevTools();
app.on("activate", () => {
// 在 macOS 系统内, 如果没有已开启的应用窗口
@@ -39,8 +38,36 @@ app.whenReady().then(() => {
app.quit();
});
ipcMain.on('printerInfoSync', async (event, params) => {
await printUtils(params)
// 创建打印小票子窗口
const printWin = new BrowserWindow({
show: false,
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')); // 打包后使用文件路径访问应用
}
printWin.webContents.openDevTools()
ipcMain.on('printerInfoSync', (event, params) => {
console.log(JSON.parse(params))
printWin.webContents.send('getParams', params)
})
// 执行打印操作
ipcMain.on('printStart', () => {
console.log('开始打印')
printWin.webContents.print({
silent: true
})
})
});

View File

@@ -1,54 +0,0 @@
import { BrowserWindow, ipcMain } from 'electron';
import path from "path";
export function printUtils(params) {
return new Promise(async (resolvePrint, rejectPrint) => {
let subMainWindow = new BrowserWindow({
// show: false,
webPreferences: {
nodeIntegration: true, // For electron >= 4.0.0
contextIsolation: false,
webSecurity: false,
enableRemoteModule: true
}
});
// 加载打印的html文件
subMainWindow.loadFile(path.join(__dirname, "../public/print.html"));
subMainWindow.webContents.on('did-finish-load', async (res) => {
subMainWindow.webContents.openDevTools();
console.log('网页加载完成', res)
subMainWindow.webContents.send('getParams', params)
return
let data = []
return renderPrintDocument(subMainWindow, data)
.then(async (result) => {
// let width = Math.ceil((result.width) * 264.5833);
let height = Math.ceil((result.height + 60) * 264.5833);
console.info('height', result, height);
subMainWindow.webContents.print({
silent: true,
margins: {
marginType: 'none'
},
printBackground: false,
deviceName: '',
copies: 1,
}, (success) => {
if (success) {
resolvePrint({ type: 'success' })
}
subMainWindow.close();
})
})
.catch(err => console.warn(33, err))
})
ipcMain.on('printStart', () => {
console.log('开始打印')
subMainWindow.webContents.print({
silent: true
})
})
})
}