聚合打印

This commit is contained in:
xianyi
2025-12-22 15:45:13 +08:00
parent 4a46a289b8
commit 1544bf38d5

View File

@@ -133,27 +133,75 @@ const UI8: React.FC = () => {
setPageCounts({}); setPageCounts({});
const examId = getExamId(); const examId = getExamId();
let pdfUrl = ""; let daojiandanUrl = "";
const consentSignature = localStorage.getItem("consentSignature"); const consentSignature = localStorage.getItem("consentSignature");
if (consentSignature) { if (consentSignature) {
pdfUrl = consentSignature; daojiandanUrl = consentSignature;
} else { } else {
window.electronAPI?.log("info", `[UI8] 开始获取导检单 PDFexam_id=${examId}`); window.electronAPI?.log(
"info",
`[UI8] 开始获取导检单 PDFexam_id=${examId}`
);
const res = await getDaojiandanPdf(parseInt(examId, 10)); const res = await getDaojiandanPdf(parseInt(examId, 10));
if (res.Status !== 200) { if (res.Status !== 200) {
throw new Error(res.Message || "获取导检单PDF失败"); throw new Error(res.Message || "获取导检单PDF失败");
} }
pdfUrl = res.Data?.pdf_url; daojiandanUrl = res.Data?.pdf_url;
if (!pdfUrl) { if (!daojiandanUrl) {
throw new Error("未获取到导检单 PDF"); throw new Error("未获取到导检单 PDF");
} }
} }
setOriginPdfUrls([pdfUrl]); // 读取知情同意书已签名 PDF 列表
let tongyishuUrls: string[] = [];
try {
const raw = localStorage.getItem("tongyishuSignedPdfUrls");
if (raw) {
const parsed = JSON.parse(raw) as unknown;
if (Array.isArray(parsed)) {
tongyishuUrls = parsed
.map((v) => (typeof v === "string" ? v : ""))
.filter((v): v is string => Boolean(v));
}
}
} catch (e) {
window.electronAPI?.log(
"warn",
`[UI8] 解析 tongyishuSignedPdfUrls 失败: ${(e as Error).message
}`
);
}
window.electronAPI?.log("info", `[UI8] 下载导检单 PDF: ${pdfUrl}`); // 聚合所有需要展示/打印的原始 URL导检单 + 知情同意书列表
const dataUrl = await fetchPdfDataUrl(pdfUrl); const allOriginUrls: string[] = [
setPdfFiles([dataUrl]); daojiandanUrl,
...tongyishuUrls.filter((u) => u && u !== daojiandanUrl),
];
setOriginPdfUrls(allOriginUrls);
// 依次下载为 base64用于预览和打印
const allDataUrls: string[] = [];
for (let i = 0; i < allOriginUrls.length; i++) {
const url = allOriginUrls[i];
window.electronAPI?.log(
"info",
`[UI8] 下载第 ${i + 1} 份 PDF: ${url}`
);
try {
const dataUrl = await fetchPdfDataUrl(url);
allDataUrls.push(dataUrl);
} catch (e) {
const msg = `[UI8] 第 ${i + 1} 份 PDF 下载失败: ${(e as Error).message
}`;
window.electronAPI?.log("error", msg);
}
}
if (!allDataUrls.length) {
throw new Error("所有 PDF 下载均失败");
}
setPdfFiles(allDataUrls);
} catch (err) { } catch (err) {
const errorMsg = `导检单PDF获取失败: ${(err as Error).message || err}`; const errorMsg = `导检单PDF获取失败: ${(err as Error).message || err}`;
console.error(errorMsg); console.error(errorMsg);
@@ -320,52 +368,68 @@ const UI8: React.FC = () => {
setIsPrinting(true); setIsPrinting(true);
try { try {
// 优先使用已加载的 base64 数据,确保数据完整 // 依次打印所有 PDF先用 base64不够则回退到 URL
let printData: string; for (let i = 0; i < Math.max(pdfFiles.length, originPdfUrls.length); i++) {
let printData: string | undefined;
let dataType: string; let dataType: string;
const base64 = pdfFiles[i];
const url = originPdfUrls[i];
if (pdfFiles[0] && pdfFiles[0].startsWith("data:application/pdf;base64,")) { if (base64 && base64.startsWith("data:application/pdf;base64,")) {
printData = pdfFiles[0]; printData = base64;
dataType = "base64数据"; dataType = "base64数据";
// 验证 base64 数据长度至少应该有几KB
const base64Part = printData.replace("data:application/pdf;base64,", ""); const base64Part = printData.replace("data:application/pdf;base64,", "");
const estimatedSize = (base64Part.length * 3) / 4; const estimatedSize = (base64Part.length * 3) / 4;
window.electronAPI.log("info", `使用base64数据打印估算大小: ${Math.round(estimatedSize)} bytes`); window.electronAPI.log(
"info",
if (estimatedSize < 1024) { `[UI8] 使用第 ${i + 1} 份 base64 数据打印,估算大小: ${Math.round(
window.electronAPI.log("warn", "base64数据可能不完整尝试使用原始URL"); estimatedSize
// 如果base64数据太小回退到URL )} bytes`
if (originPdfUrls[0]) {
printData = originPdfUrls[0];
dataType = "远程文件路径";
}
}
} else if (originPdfUrls[0]) {
printData = originPdfUrls[0];
dataType = "远程文件路径";
} else {
throw new Error("没有可用的PDF数据");
}
window.electronAPI.log("info", `开始打印PDF (${dataType}): ${dataType === "base64数据" ? `base64长度${printData.length}` : printData.substring(0, 100)}...`);
const printResult = await window.electronAPI.printPdf(
printData,
{
...(selectedPrinter ? { printerName: selectedPrinter } : {}),
duplex: duplexEnabled,
}
); );
if (estimatedSize < 1024 && url) {
window.electronAPI.log(
"warn",
`[UI8] 第 ${i + 1} 份 base64 数据可能不完整,回退到 URL`
);
printData = url;
dataType = "远程文件路径";
}
} else if (url) {
printData = url;
dataType = "远程文件路径";
} else {
window.electronAPI.log(
"warn",
`[UI8] 第 ${i + 1} 份 PDF 缺少可用数据,已跳过`
);
continue;
}
window.electronAPI.log(
"info",
`[UI8] 开始打印第 ${i + 1} 份 PDF (${dataType}): ${dataType === "base64数据"
? `base64长度${printData.length}`
: printData.substring(0, 100)
}...`
);
const printResult = await window.electronAPI.printPdf(printData, {
...(selectedPrinter ? { printerName: selectedPrinter } : {}),
duplex: duplexEnabled,
});
if (!printResult.success) { if (!printResult.success) {
const errorMsg = `打印失败: ${printResult.error || "未知错误"}`; const errorMsg = `${i + 1} 份 PDF 打印失败: ${printResult.error || "未知错误"
window.electronAPI.log("error", errorMsg); }`;
window.electronAPI.log("error", `[UI8] ${errorMsg}`);
alert(errorMsg); alert(errorMsg);
return; return;
} }
}
window.electronAPI.log("info", "PDF打印请求已发送成功"); window.electronAPI.log("info", "[UI8] 所有 PDF 打印请求已发送成功");
navigate("/UI81"); navigate("/UI81");
} catch (error) { } catch (error) {
const errorMsg = `打印错误: ${(error as Error).message || error}`; const errorMsg = `打印错误: ${(error as Error).message || error}`;