更新名称和图标
This commit is contained in:
@@ -64,6 +64,7 @@ function createWindow() {
|
||||
height: 1920,
|
||||
fullscreen: true, // 全屏
|
||||
frame: false, // 无边框
|
||||
icon: path.join(__dirname, "../resources/icon.ico"), // 设置窗口图标
|
||||
webPreferences: {
|
||||
preload: path.join(__dirname, "preload.js"),
|
||||
nodeIntegration: false,
|
||||
@@ -195,15 +196,22 @@ ipcMain.handle("print-pdf", async (event, pdfDataOrUrl, printOptions = {}) => {
|
||||
}
|
||||
|
||||
// 创建临时文件
|
||||
tempFilePath = path.join(os.tmpdir(), `print_${Date.now()}_${Math.random().toString(36).substring(7)}.pdf`);
|
||||
tempFilePath = path.join(
|
||||
os.tmpdir(),
|
||||
`print_${Date.now()}_${Math.random().toString(36).substring(7)}.pdf`
|
||||
);
|
||||
fs.writeFileSync(tempFilePath, buffer);
|
||||
pdfFilePath = tempFilePath;
|
||||
|
||||
const fileSize = fs.statSync(tempFilePath).size;
|
||||
log.info(`PDF written to temp file: ${tempFilePath}, size: ${fileSize} bytes`);
|
||||
log.info(
|
||||
`PDF written to temp file: ${tempFilePath}, size: ${fileSize} bytes`
|
||||
);
|
||||
|
||||
if (fileSize < 1024) {
|
||||
throw new Error(`PDF文件大小异常(${fileSize} bytes),可能数据不完整`);
|
||||
throw new Error(
|
||||
`PDF文件大小异常(${fileSize} bytes),可能数据不完整`
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
log.error("Base64解码或文件写入失败:", err);
|
||||
@@ -211,32 +219,39 @@ ipcMain.handle("print-pdf", async (event, pdfDataOrUrl, printOptions = {}) => {
|
||||
}
|
||||
}
|
||||
// 如果是HTTP/HTTPS URL,先下载
|
||||
else if (pdfDataOrUrl.startsWith("http://") || pdfDataOrUrl.startsWith("https://")) {
|
||||
else if (
|
||||
pdfDataOrUrl.startsWith("http://") ||
|
||||
pdfDataOrUrl.startsWith("https://")
|
||||
) {
|
||||
log.info("Downloading PDF from URL:", pdfDataOrUrl);
|
||||
const protocol = pdfDataOrUrl.startsWith("https") ? https : http;
|
||||
|
||||
const buffer = await new Promise((resolve, reject) => {
|
||||
protocol.get(pdfDataOrUrl, (response) => {
|
||||
// 处理重定向
|
||||
if (response.statusCode === 301 || response.statusCode === 302) {
|
||||
const redirectUrl = response.headers.location;
|
||||
protocol.get(redirectUrl, (redirectResponse) => {
|
||||
protocol
|
||||
.get(pdfDataOrUrl, (response) => {
|
||||
// 处理重定向
|
||||
if (response.statusCode === 301 || response.statusCode === 302) {
|
||||
const redirectUrl = response.headers.location;
|
||||
protocol
|
||||
.get(redirectUrl, (redirectResponse) => {
|
||||
const chunks = [];
|
||||
redirectResponse.on("data", (chunk) => chunks.push(chunk));
|
||||
redirectResponse.on("end", () => {
|
||||
resolve(Buffer.concat(chunks));
|
||||
});
|
||||
redirectResponse.on("error", reject);
|
||||
})
|
||||
.on("error", reject);
|
||||
} else {
|
||||
const chunks = [];
|
||||
redirectResponse.on("data", (chunk) => chunks.push(chunk));
|
||||
redirectResponse.on("end", () => {
|
||||
response.on("data", (chunk) => chunks.push(chunk));
|
||||
response.on("end", () => {
|
||||
resolve(Buffer.concat(chunks));
|
||||
});
|
||||
redirectResponse.on("error", reject);
|
||||
}).on("error", reject);
|
||||
} else {
|
||||
const chunks = [];
|
||||
response.on("data", (chunk) => chunks.push(chunk));
|
||||
response.on("end", () => {
|
||||
resolve(Buffer.concat(chunks));
|
||||
});
|
||||
response.on("error", reject);
|
||||
}
|
||||
}).on("error", reject);
|
||||
response.on("error", reject);
|
||||
}
|
||||
})
|
||||
.on("error", reject);
|
||||
});
|
||||
|
||||
// 验证PDF文件头
|
||||
@@ -244,12 +259,17 @@ ipcMain.handle("print-pdf", async (event, pdfDataOrUrl, printOptions = {}) => {
|
||||
throw new Error("下载的文件不是有效的PDF");
|
||||
}
|
||||
|
||||
tempFilePath = path.join(os.tmpdir(), `print_${Date.now()}_${Math.random().toString(36).substring(7)}.pdf`);
|
||||
tempFilePath = path.join(
|
||||
os.tmpdir(),
|
||||
`print_${Date.now()}_${Math.random().toString(36).substring(7)}.pdf`
|
||||
);
|
||||
fs.writeFileSync(tempFilePath, buffer);
|
||||
pdfFilePath = tempFilePath;
|
||||
|
||||
const fileSize = fs.statSync(tempFilePath).size;
|
||||
log.info(`PDF downloaded to temp file: ${tempFilePath}, size: ${fileSize} bytes`);
|
||||
log.info(
|
||||
`PDF downloaded to temp file: ${tempFilePath}, size: ${fileSize} bytes`
|
||||
);
|
||||
}
|
||||
// 如果是本地文件路径
|
||||
else if (pdfDataOrUrl.startsWith("file://")) {
|
||||
@@ -280,7 +300,11 @@ ipcMain.handle("print-pdf", async (event, pdfDataOrUrl, printOptions = {}) => {
|
||||
throw new Error(`PDF文件大小异常(${stats.size} bytes),可能文件损坏`);
|
||||
}
|
||||
|
||||
log.info(`准备打印PDF: ${pdfFilePath}, 打印机: ${targetPrinterName || "默认打印机"}`);
|
||||
log.info(
|
||||
`准备打印PDF: ${pdfFilePath}, 打印机: ${
|
||||
targetPrinterName || "默认打印机"
|
||||
}`
|
||||
);
|
||||
|
||||
// 使用 pdf-to-printer 打印
|
||||
const printOptions_ptp = {
|
||||
|
||||
Reference in New Issue
Block a user