优化打印效果

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,38 +1,11 @@
"use strict";
const electron = require("electron");
const path = require("path");
function printUtils(params) {
return new Promise(async (resolvePrint, rejectPrint) => {
let subMainWindow = new electron.BrowserWindow({
// show: false,
webPreferences: {
nodeIntegration: true,
// For electron >= 4.0.0
contextIsolation: false,
webSecurity: false,
enableRemoteModule: true
}
});
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;
});
electron.ipcMain.on("printStart", () => {
console.log("开始打印");
subMainWindow.webContents.print({
silent: true
});
});
});
}
electron.app.whenReady().then(() => {
const win = new electron.BrowserWindow({
title: "银收客",
width: 1200,
height: 800,
width: 1024,
height: 768,
fullscreenable: true,
fullscreen: false,
simpleFullscreen: true,
@@ -48,7 +21,6 @@ electron.app.whenReady().then(() => {
} else {
win.loadFile(path.resolve(__dirname, "../dist/index.html"));
}
win.webContents.openDevTools();
electron.app.on("activate", () => {
if (electron.BrowserWindow.getAllWindows().length === 0) {
createWindow();
@@ -57,8 +29,29 @@ electron.app.whenReady().then(() => {
electron.ipcMain.on("quitHandler", (_, msg) => {
electron.app.quit();
});
electron.ipcMain.on("printerInfoSync", async (event, params) => {
await printUtils(params);
const printWin = new electron.BrowserWindow({
show: false,
webPreferences: {
// 集成网页和 Node.js也就是在渲染进程中可以调用 Node.js 方法
nodeIntegration: true,
contextIsolation: false
}
});
if (process.env.VITE_DEV_SERVER_URL) {
printWin.loadFile(path.join(__dirname, "../public/print.html"));
} else {
printWin.loadFile(path.resolve(__dirname, "../dist/print.html"));
}
printWin.webContents.openDevTools();
electron.ipcMain.on("printerInfoSync", (event, params) => {
console.log(JSON.parse(params));
printWin.webContents.send("getParams", params);
});
electron.ipcMain.on("printStart", () => {
console.log("开始打印");
printWin.webContents.print({
silent: true
});
});
});
electron.app.on("window-all-closed", () => {