调试打印

This commit is contained in:
gyq
2024-03-05 09:52:20 +08:00
parent 0fa0d56558
commit d00612da5a
10 changed files with 143 additions and 93 deletions

View File

@@ -1,10 +1,10 @@
"use strict";
const electron = require("electron");
const path = require("path");
function printUtils(data) {
function printUtils(params) {
return new Promise(async (resolvePrint, rejectPrint) => {
let subMainWindow = new electron.BrowserWindow({
show: false,
// show: false,
webPreferences: {
nodeIntegration: true,
// For electron >= 4.0.0
@@ -13,38 +13,18 @@ function printUtils(data) {
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.loadFile(path.join(__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));
subMainWindow.webContents.openDevTools();
console.log("网页加载完成", res);
subMainWindow.webContents.send("getParams", params);
return;
});
electron.ipcMain.on("printStart", () => {
console.log("开始打印");
subMainWindow.webContents.print({
silent: true
});
});
});
}
@@ -54,9 +34,9 @@ electron.app.whenReady().then(() => {
width: 1200,
height: 800,
fullscreenable: true,
fullscreen: false,
fullscreen: true,
simpleFullscreen: true,
frame: true,
frame: false,
webPreferences: {
// 集成网页和 Node.js也就是在渲染进程中可以调用 Node.js 方法
nodeIntegration: true,
@@ -68,7 +48,6 @@ electron.app.whenReady().then(() => {
} else {
win.loadFile("dist/index.html");
}
win.webContents.openDevTools();
electron.app.on("activate", () => {
if (electron.BrowserWindow.getAllWindows().length === 0) {
createWindow();
@@ -78,10 +57,7 @@ electron.app.whenReady().then(() => {
electron.app.quit();
});
electron.ipcMain.on("printerInfoSync", async (event, params) => {
console.log("接收到打印消息", params);
const res = await printUtils();
event.returnValue = res;
console.log("已打印", res);
await printUtils(params);
});
});
electron.app.on("window-all-closed", () => {