默认打印机
This commit is contained in:
@@ -147,10 +147,31 @@ ipcMain.handle("fetch-pdf", async (event, pdfUrl) => {
|
||||
});
|
||||
});
|
||||
|
||||
ipcMain.handle("get-printers", async (event) => {
|
||||
try {
|
||||
const webContents = event.sender;
|
||||
if (!webContents) {
|
||||
return { success: false, error: "No webContents available" };
|
||||
}
|
||||
|
||||
if (typeof webContents.getPrintersAsync === "function") {
|
||||
const printers = await webContents.getPrintersAsync();
|
||||
return { success: true, printers };
|
||||
}
|
||||
|
||||
const printers = webContents.getPrinters();
|
||||
return { success: true, printers };
|
||||
} catch (error) {
|
||||
log.error("Failed to get printers:", error);
|
||||
return { success: false, error: error.message };
|
||||
}
|
||||
});
|
||||
|
||||
// 处理PDF打印请求
|
||||
ipcMain.handle("print-pdf", async (event, pdfDataOrUrl) => {
|
||||
ipcMain.handle("print-pdf", async (event, pdfDataOrUrl, printOptions = {}) => {
|
||||
let printWindow = null;
|
||||
let tempFilePath = null;
|
||||
const targetPrinterName = printOptions?.printerName;
|
||||
|
||||
try {
|
||||
let pdfPath = pdfDataOrUrl;
|
||||
@@ -210,14 +231,20 @@ ipcMain.handle("print-pdf", async (event, pdfDataOrUrl) => {
|
||||
|
||||
// 静默打印(直接调用系统打印对话框)
|
||||
return new Promise((resolve) => {
|
||||
printWindow.webContents.print(
|
||||
{
|
||||
silent: false, // 显示打印对话框
|
||||
printBackground: true,
|
||||
margins: {
|
||||
marginType: "none",
|
||||
},
|
||||
const basePrintOptions = {
|
||||
silent: Boolean(targetPrinterName),
|
||||
printBackground: true,
|
||||
margins: {
|
||||
marginType: "none",
|
||||
},
|
||||
};
|
||||
|
||||
if (targetPrinterName) {
|
||||
basePrintOptions.deviceName = targetPrinterName;
|
||||
}
|
||||
|
||||
printWindow.webContents.print(
|
||||
basePrintOptions,
|
||||
(success, errorType) => {
|
||||
// 清理临时文件
|
||||
if (tempFilePath && fs.existsSync(tempFilePath)) {
|
||||
|
||||
@@ -4,7 +4,9 @@ contextBridge.exposeInMainWorld("electronAPI", {
|
||||
// 获取PDF(绕过CORS)
|
||||
fetchPdf: (pdfUrl) => ipcRenderer.invoke("fetch-pdf", pdfUrl),
|
||||
// 打印PDF
|
||||
printPdf: (pdfUrl) => ipcRenderer.invoke("print-pdf", pdfUrl),
|
||||
printPdf: (pdfUrl, options) => ipcRenderer.invoke("print-pdf", pdfUrl, options),
|
||||
// 获取打印机列表
|
||||
getPrinters: () => ipcRenderer.invoke("get-printers"),
|
||||
startIdCardListen: () => ipcRenderer.invoke("start_idcard_listen"),
|
||||
stopIdCardListen: () => ipcRenderer.invoke("stop_idcard_listen"),
|
||||
onIdCardData: (callback) =>
|
||||
|
||||
Reference in New Issue
Block a user