"use strict"; const electron = require("electron"); const path = require("path"); function printUtils(data) { 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 } }); function renderPrintDocument(window, data2) { return new Promise(async (resolve, reject) => { electron.ipcMain.on("load-ok", (event, res) => { setTimeout(() => { resolve({ message: "page-rendered", ...res }); }, 500); }); }); } subMainWindow.on("closed", () => { subMainWindow = null; }); subMainWindow.loadFile(path.resolve(__dirname, "./public/print.html")); subMainWindow.webContents.on("did-finish-load", async (res) => { return renderPrintDocument().then(async (result) => { 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: options.printerName, copies: 1 }, (success) => { if (success) { resolvePrint({ type: "success" }); } subMainWindow.close(); }); }).catch((err) => console.warn(33, err)); }); }); } electron.app.whenReady().then(() => { const win = new electron.BrowserWindow({ title: "Main window", width: 1200, height: 800, fullscreenable: true, fullscreen: false, simpleFullscreen: true, frame: true, webPreferences: { // 集成网页和 Node.js,也就是在渲染进程中,可以调用 Node.js 方法 nodeIntegration: true, contextIsolation: false } }); if (process.env.VITE_DEV_SERVER_URL) { win.loadURL(process.env.VITE_DEV_SERVER_URL); } else { win.loadFile("dist/index.html"); } win.webContents.openDevTools(); electron.app.on("activate", () => { if (electron.BrowserWindow.getAllWindows().length === 0) { createWindow(); } }); electron.ipcMain.on("quitHandler", (_, msg) => { electron.app.quit(); }); electron.ipcMain.on("printerInfoSync", async (event, params) => { console.log("接收到打印消息", params); const res = await printUtils(); event.returnValue = res; console.log("已打印", res); }); }); electron.app.on("window-all-closed", () => { if (process.platform !== "darwin") electron.app.quit(); });