双面打印
This commit is contained in:
@@ -300,16 +300,18 @@ ipcMain.handle("print-pdf", async (event, pdfDataOrUrl, printOptions = {}) => {
|
||||
throw new Error(`PDF文件大小异常(${stats.size} bytes),可能文件损坏`);
|
||||
}
|
||||
|
||||
const duplexMode = printOptions?.duplex;
|
||||
log.info(
|
||||
`准备打印PDF: ${pdfFilePath}, 打印机: ${
|
||||
targetPrinterName || "默认打印机"
|
||||
}`
|
||||
}, 双面打印: ${duplexMode ? "是" : "否"}`
|
||||
);
|
||||
|
||||
// 使用 pdf-to-printer 打印
|
||||
const printOptions_ptp = {
|
||||
printer: targetPrinterName || undefined,
|
||||
silent: true, // 静默打印
|
||||
duplex: duplexMode || false, // 双面打印选项
|
||||
};
|
||||
|
||||
await ptp.print(pdfFilePath, printOptions_ptp);
|
||||
|
||||
2
src/electron.d.ts
vendored
2
src/electron.d.ts
vendored
@@ -14,7 +14,7 @@ interface ElectronAPI {
|
||||
) => Promise<{ success: boolean; data?: string; error?: string }>;
|
||||
printPdf: (
|
||||
pdfUrl: string,
|
||||
options?: { printerName?: string }
|
||||
options?: { printerName?: string; duplex?: boolean }
|
||||
) => Promise<{ success: boolean; error?: string }>;
|
||||
getPrinters: () => Promise<{
|
||||
success: boolean;
|
||||
|
||||
@@ -349,3 +349,34 @@
|
||||
font-size: 22px;
|
||||
color: #b12651;
|
||||
}
|
||||
|
||||
.ui8-duplex-option {
|
||||
margin-top: 15px;
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.ui8-duplex-label {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
cursor: pointer;
|
||||
user-select: none;
|
||||
border: none;
|
||||
background-color: inherit;
|
||||
}
|
||||
|
||||
.ui8-duplex-checkbox {
|
||||
width: 16px;
|
||||
height: 16px;
|
||||
cursor: pointer;
|
||||
accent-color: rgba(0, 45, 93, 0);
|
||||
border: none;
|
||||
background-color: inherit;
|
||||
}
|
||||
|
||||
.ui8-duplex-text {
|
||||
font-size: 20px;
|
||||
font-family: NotoSansCJKsc-Regular;
|
||||
color: rgba(0, 45, 93, 0);
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import ui8B from "../../assets/ui8B.png";
|
||||
import { getDaojiandanPdf } from "../../api/hisApi";
|
||||
|
||||
const PREFERRED_PRINTER_KEY = "preferredPrinterName";
|
||||
const PREFERRED_DUPLEX_KEY = "preferredDuplex";
|
||||
|
||||
type PrinterInfo = ElectronPrinterInfo;
|
||||
|
||||
@@ -74,6 +75,7 @@ const UI8: React.FC = () => {
|
||||
const printerDropdownRef = useRef<HTMLDivElement | null>(null);
|
||||
const pdfContainerRef = useRef<HTMLDivElement | null>(null);
|
||||
const [isRightSectionHidden, setIsRightSectionHidden] = useState(false);
|
||||
const [duplexEnabled, setDuplexEnabled] = useState<boolean>(false);
|
||||
|
||||
const getExamId = () => {
|
||||
const storedId = localStorage.getItem("selectedExamId");
|
||||
@@ -221,6 +223,12 @@ const UI8: React.FC = () => {
|
||||
|
||||
fetchPrinters();
|
||||
|
||||
// 恢复双面打印设置
|
||||
const storedDuplex = localStorage.getItem(PREFERRED_DUPLEX_KEY);
|
||||
if (storedDuplex === "true") {
|
||||
setDuplexEnabled(true);
|
||||
}
|
||||
|
||||
return () => {
|
||||
isMounted = false;
|
||||
};
|
||||
@@ -343,7 +351,10 @@ const UI8: React.FC = () => {
|
||||
|
||||
const printResult = await window.electronAPI.printPdf(
|
||||
printData,
|
||||
selectedPrinter ? { printerName: selectedPrinter } : undefined
|
||||
{
|
||||
...(selectedPrinter ? { printerName: selectedPrinter } : {}),
|
||||
duplex: duplexEnabled,
|
||||
}
|
||||
);
|
||||
|
||||
if (!printResult.success) {
|
||||
@@ -363,7 +374,7 @@ const UI8: React.FC = () => {
|
||||
} finally {
|
||||
setIsPrinting(false);
|
||||
}
|
||||
}, [originPdfUrls, pdfFiles, printers.length, selectedPrinter, navigate, isPrinting]);
|
||||
}, [originPdfUrls, pdfFiles, printers.length, selectedPrinter, navigate, isPrinting, duplexEnabled]);
|
||||
|
||||
const handlePageCountUpdate = useCallback((index: number, numPages: number) => {
|
||||
window.electronAPI?.log("info", `[UI8] PDF渲染成功 (index=${index}),共 ${numPages} 页`);
|
||||
@@ -436,6 +447,25 @@ const UI8: React.FC = () => {
|
||||
{printerError && (
|
||||
<div className="ui8-printer-error">{printerError}</div>
|
||||
)}
|
||||
<label className="ui8-duplex-label">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={duplexEnabled}
|
||||
onChange={(e) => {
|
||||
const enabled = e.target.checked;
|
||||
setDuplexEnabled(enabled);
|
||||
localStorage.setItem(PREFERRED_DUPLEX_KEY, enabled.toString());
|
||||
window.electronAPI?.log(
|
||||
"info",
|
||||
`[UI8] 双面打印设置为: ${enabled ? "启用" : "禁用"}`
|
||||
);
|
||||
}}
|
||||
className="ui8-duplex-checkbox"
|
||||
/>
|
||||
</label>
|
||||
<div className="ui8-duplex-option">
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="basic-white-block">
|
||||
|
||||
Reference in New Issue
Block a user