修复打印

This commit is contained in:
xianyi
2025-11-21 10:36:00 +08:00
parent 196669c013
commit b954b927db
2 changed files with 79 additions and 26 deletions

View File

@@ -2,6 +2,8 @@ const { app, BrowserWindow, ipcMain } = require("electron");
const path = require("path");
const https = require("https");
const http = require("http");
const fs = require("fs");
const os = require("os");
let mainWindow = null;
const { Worker } = require("worker_threads");
@@ -127,47 +129,92 @@ ipcMain.handle("fetch-pdf", async (event, pdfUrl) => {
});
// 处理PDF打印请求
ipcMain.handle("print-pdf", async (event, pdfUrl) => {
ipcMain.handle("print-pdf", async (event, pdfDataOrUrl) => {
let printWindow = null;
let tempFilePath = null;
try {
// 如果是 base64 data URI转换为临时文件
let pdfPath = pdfDataOrUrl;
if (pdfDataOrUrl.startsWith("data:application/pdf;base64,")) {
const base64Data = pdfDataOrUrl.replace("data:application/pdf;base64,", "");
const buffer = Buffer.from(base64Data, "base64");
// 创建临时文件
tempFilePath = path.join(os.tmpdir(), `print_${Date.now()}.pdf`);
fs.writeFileSync(tempFilePath, buffer);
pdfPath = `file://${tempFilePath}`;
log.info("PDF written to temp file:", tempFilePath);
}
// 创建一个隐藏的窗口用于加载PDF
const printWindow = new BrowserWindow({
printWindow = new BrowserWindow({
width: 800,
height: 600,
show: false,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
plugins: true, // 启用插件以支持 PDF 查看
},
});
// 加载PDF
await printWindow.loadURL(pdfUrl);
await printWindow.loadURL(pdfPath);
log.info("PDF loaded for printing");
// 等待页面加载完成
await new Promise((resolve) => {
printWindow.webContents.on("did-finish-load", resolve);
});
// 等待更长时间确保 PDF 完全渲染
await new Promise(resolve => setTimeout(resolve, 2000));
// 静默打印(直接调用系统打印对话框)
printWindow.webContents.print(
{
silent: false, // 显示打印对话框
printBackground: true,
margins: {
marginType: "none",
return new Promise((resolve) => {
printWindow.webContents.print(
{
silent: false, // 显示打印对话框
printBackground: true,
margins: {
marginType: "none",
},
},
},
(success, errorType) => {
printWindow.close();
if (!success) {
console.error("Print failed:", errorType);
}
}
);
(success, errorType) => {
// 清理临时文件
if (tempFilePath && fs.existsSync(tempFilePath)) {
try {
fs.unlinkSync(tempFilePath);
log.info("Temp file deleted");
} catch (err) {
log.error("Failed to delete temp file:", err);
}
}
return { success: true };
// 关闭窗口
if (printWindow && !printWindow.isDestroyed()) {
printWindow.close();
}
if (!success) {
log.error("Print failed:", errorType);
resolve({ success: false, error: errorType });
} else {
log.info("Print completed");
resolve({ success: true });
}
}
);
});
} catch (error) {
console.error("Print error:", error);
log.error("Print error:", error);
// 清理临时文件
if (tempFilePath && fs.existsSync(tempFilePath)) {
try {
fs.unlinkSync(tempFilePath);
} catch (err) {
log.error("Failed to delete temp file:", err);
}
}
if (printWindow && !printWindow.isDestroyed()) {
printWindow.close();
}
return { success: false, error: error.message };
}
});