聚合打印
This commit is contained in:
@@ -133,27 +133,75 @@ const UI8: React.FC = () => {
|
||||
setPageCounts({});
|
||||
|
||||
const examId = getExamId();
|
||||
let pdfUrl = "";
|
||||
let daojiandanUrl = "";
|
||||
const consentSignature = localStorage.getItem("consentSignature");
|
||||
if (consentSignature) {
|
||||
pdfUrl = consentSignature;
|
||||
daojiandanUrl = consentSignature;
|
||||
} else {
|
||||
window.electronAPI?.log("info", `[UI8] 开始获取导检单 PDF,exam_id=${examId}`);
|
||||
window.electronAPI?.log(
|
||||
"info",
|
||||
`[UI8] 开始获取导检单 PDF,exam_id=${examId}`
|
||||
);
|
||||
const res = await getDaojiandanPdf(parseInt(examId, 10));
|
||||
if (res.Status !== 200) {
|
||||
throw new Error(res.Message || "获取导检单PDF失败");
|
||||
}
|
||||
pdfUrl = res.Data?.pdf_url;
|
||||
if (!pdfUrl) {
|
||||
daojiandanUrl = res.Data?.pdf_url;
|
||||
if (!daojiandanUrl) {
|
||||
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}`);
|
||||
const dataUrl = await fetchPdfDataUrl(pdfUrl);
|
||||
setPdfFiles([dataUrl]);
|
||||
// 聚合所有需要展示/打印的原始 URL:导检单 + 知情同意书列表
|
||||
const allOriginUrls: string[] = [
|
||||
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) {
|
||||
const errorMsg = `导检单PDF获取失败: ${(err as Error).message || err}`;
|
||||
console.error(errorMsg);
|
||||
@@ -320,52 +368,68 @@ const UI8: React.FC = () => {
|
||||
|
||||
setIsPrinting(true);
|
||||
try {
|
||||
// 优先使用已加载的 base64 数据,确保数据完整
|
||||
let printData: string;
|
||||
let dataType: string;
|
||||
// 依次打印所有 PDF:先用 base64,不够则回退到 URL
|
||||
for (let i = 0; i < Math.max(pdfFiles.length, originPdfUrls.length); i++) {
|
||||
let printData: string | undefined;
|
||||
let dataType: string;
|
||||
const base64 = pdfFiles[i];
|
||||
const url = originPdfUrls[i];
|
||||
|
||||
if (pdfFiles[0] && pdfFiles[0].startsWith("data:application/pdf;base64,")) {
|
||||
printData = pdfFiles[0];
|
||||
dataType = "base64数据";
|
||||
if (base64 && base64.startsWith("data:application/pdf;base64,")) {
|
||||
printData = base64;
|
||||
dataType = "base64数据";
|
||||
|
||||
// 验证 base64 数据长度(至少应该有几KB)
|
||||
const base64Part = printData.replace("data:application/pdf;base64,", "");
|
||||
const estimatedSize = (base64Part.length * 3) / 4;
|
||||
window.electronAPI.log("info", `使用base64数据打印,估算大小: ${Math.round(estimatedSize)} bytes`);
|
||||
const base64Part = printData.replace("data:application/pdf;base64,", "");
|
||||
const estimatedSize = (base64Part.length * 3) / 4;
|
||||
window.electronAPI.log(
|
||||
"info",
|
||||
`[UI8] 使用第 ${i + 1} 份 base64 数据打印,估算大小: ${Math.round(
|
||||
estimatedSize
|
||||
)} bytes`
|
||||
);
|
||||
|
||||
if (estimatedSize < 1024) {
|
||||
window.electronAPI.log("warn", "base64数据可能不完整,尝试使用原始URL");
|
||||
// 如果base64数据太小,回退到URL
|
||||
if (originPdfUrls[0]) {
|
||||
printData = originPdfUrls[0];
|
||||
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;
|
||||
}
|
||||
} 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)}...`);
|
||||
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,
|
||||
{
|
||||
const printResult = await window.electronAPI.printPdf(printData, {
|
||||
...(selectedPrinter ? { printerName: selectedPrinter } : {}),
|
||||
duplex: duplexEnabled,
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
if (!printResult.success) {
|
||||
const errorMsg = `打印失败: ${printResult.error || "未知错误"}`;
|
||||
window.electronAPI.log("error", errorMsg);
|
||||
alert(errorMsg);
|
||||
return;
|
||||
if (!printResult.success) {
|
||||
const errorMsg = `第 ${i + 1} 份 PDF 打印失败: ${printResult.error || "未知错误"
|
||||
}`;
|
||||
window.electronAPI.log("error", `[UI8] ${errorMsg}`);
|
||||
alert(errorMsg);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
window.electronAPI.log("info", "PDF打印请求已发送成功");
|
||||
window.electronAPI.log("info", "[UI8] 所有 PDF 打印请求已发送成功");
|
||||
navigate("/UI81");
|
||||
} catch (error) {
|
||||
const errorMsg = `打印错误: ${(error as Error).message || error}`;
|
||||
|
||||
Reference in New Issue
Block a user