默认打印机
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, // 显示打印对话框
|
||||
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) =>
|
||||
|
||||
19
src/electron.d.ts
vendored
19
src/electron.d.ts
vendored
@@ -1,9 +1,26 @@
|
||||
// Electron API 类型声明
|
||||
interface ElectronPrinterInfo {
|
||||
name: string;
|
||||
displayName?: string;
|
||||
description?: string;
|
||||
status?: number;
|
||||
isDefault?: boolean;
|
||||
options?: Record<string, string>;
|
||||
}
|
||||
|
||||
interface ElectronAPI {
|
||||
fetchPdf: (
|
||||
pdfUrl: string
|
||||
) => Promise<{ success: boolean; data?: string; error?: string }>;
|
||||
printPdf: (pdfUrl: string) => Promise<{ success: boolean; error?: string }>;
|
||||
printPdf: (
|
||||
pdfUrl: string,
|
||||
options?: { printerName?: string }
|
||||
) => Promise<{ success: boolean; error?: string }>;
|
||||
getPrinters: () => Promise<{
|
||||
success: boolean;
|
||||
printers?: ElectronPrinterInfo[];
|
||||
error?: string;
|
||||
}>;
|
||||
startIdCardListen: () => Promise<any>;
|
||||
stopIdCardListen: () => Promise<any>;
|
||||
onIdCardData: (callback: (data: any) => void) => void;
|
||||
|
||||
Reference in New Issue
Block a user