读取本地pdf
This commit is contained in:
@@ -134,8 +134,9 @@ ipcMain.handle("print-pdf", async (event, pdfDataOrUrl) => {
|
||||
let tempFilePath = null;
|
||||
|
||||
try {
|
||||
// 如果是 base64 data URI,转换为临时文件
|
||||
let pdfPath = pdfDataOrUrl;
|
||||
|
||||
// 如果是 base64 data URI,转换为临时文件
|
||||
if (pdfDataOrUrl.startsWith("data:application/pdf;base64,")) {
|
||||
const base64Data = pdfDataOrUrl.replace("data:application/pdf;base64,", "");
|
||||
const buffer = Buffer.from(base64Data, "base64");
|
||||
@@ -145,6 +146,24 @@ ipcMain.handle("print-pdf", async (event, pdfDataOrUrl) => {
|
||||
fs.writeFileSync(tempFilePath, buffer);
|
||||
pdfPath = `file://${tempFilePath}`;
|
||||
log.info("PDF written to temp file:", tempFilePath);
|
||||
}
|
||||
// 如果是相对路径(Vite 打包后的资源),转换为绝对路径
|
||||
else if (pdfDataOrUrl.startsWith("/assets/")) {
|
||||
const isDev = !app.isPackaged;
|
||||
if (!isDev) {
|
||||
// 生产环境:从 dist 目录加载
|
||||
const absolutePath = path.join(__dirname, "..", "dist", pdfDataOrUrl);
|
||||
pdfPath = `file://${absolutePath}`;
|
||||
log.info("Using bundled PDF:", absolutePath);
|
||||
} else {
|
||||
// 开发环境:Vite 会处理,保持原样
|
||||
pdfPath = `http://localhost:5173${pdfDataOrUrl}`;
|
||||
log.info("Using dev server PDF:", pdfPath);
|
||||
}
|
||||
}
|
||||
// 如果已经是 file:// 或 http(s):// 协议,保持原样
|
||||
else if (!pdfDataOrUrl.startsWith("file://") && !pdfDataOrUrl.startsWith("http")) {
|
||||
pdfPath = `file://${pdfDataOrUrl}`;
|
||||
}
|
||||
|
||||
// 创建一个隐藏的窗口用于加载PDF
|
||||
|
||||
Reference in New Issue
Block a user