读取本地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");
|
||||
@@ -146,6 +147,24 @@ ipcMain.handle("print-pdf", async (event, pdfDataOrUrl) => {
|
||||
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
|
||||
printWindow = new BrowserWindow({
|
||||
|
||||
BIN
src/assets/testPdf.pdf
Normal file
BIN
src/assets/testPdf.pdf
Normal file
Binary file not shown.
@@ -8,13 +8,13 @@ import BackButton from "../../components/BackButton";
|
||||
import ConfirmButton from "../../components/ConfirmButton";
|
||||
import ui8A from "../../assets/ui8A.png";
|
||||
import ui8B from "../../assets/ui8B.png";
|
||||
|
||||
import testPdf from "../../assets/testPdf.pdf";
|
||||
|
||||
// 设置 PDF.js worker(react-pdf 5.7.2 使用 pdfjs-dist 2.12.313)
|
||||
pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/2.12.313/pdf.worker.min.js`;
|
||||
|
||||
const PDF_URL = "https://alist.ambigrat.com/d/cos/test/testPdf.pdf?sign=mELe-vb-ShXHDCtZrP2Hw5nlOvEMEsNkJzaGUUyqDg4=:0";
|
||||
|
||||
// 使用本地 PDF 文件进行测试
|
||||
const PDF_URL = testPdf;
|
||||
const UI8: React.FC = () => {
|
||||
const navigate = useNavigate();
|
||||
const [numPages, setNumPages] = useState<number>(0);
|
||||
@@ -28,17 +28,21 @@ const UI8: React.FC = () => {
|
||||
const scrollContainerRef = useRef<HTMLDivElement>(null);
|
||||
const touchStartRef = useRef<{ x: number; y: number } | null>(null);
|
||||
|
||||
// 加载PDF数据(绕过CORS)
|
||||
// 加载PDF数据
|
||||
useEffect(() => {
|
||||
const loadPdf = async () => {
|
||||
if (!window.electronAPI?.fetchPdf) {
|
||||
// 非Electron环境,直接使用URL
|
||||
try {
|
||||
setLoading(true);
|
||||
|
||||
// 本地文件直接使用,无需通过 fetchPdf
|
||||
if (PDF_URL.startsWith("/") || PDF_URL.startsWith("blob:") || PDF_URL.includes("assets")) {
|
||||
setPdfData(PDF_URL);
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
setLoading(true);
|
||||
// 远程 URL 需要通过 Electron 绕过 CORS
|
||||
if (window.electronAPI?.fetchPdf) {
|
||||
const result = await window.electronAPI.fetchPdf(PDF_URL);
|
||||
if (result.success && result.data) {
|
||||
// 将base64转换为data URL
|
||||
@@ -46,9 +50,15 @@ const UI8: React.FC = () => {
|
||||
} else {
|
||||
setError(`PDF加载失败: ${result.error || "未知错误"}`);
|
||||
}
|
||||
} else {
|
||||
// 非Electron环境,直接使用URL
|
||||
setPdfData(PDF_URL);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("PDF fetch error:", err);
|
||||
setError("PDF加载失败,请检查网络连接");
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -73,8 +83,9 @@ const UI8: React.FC = () => {
|
||||
|
||||
setIsPrinting(true);
|
||||
try {
|
||||
// 使用已加载的 PDF 数据(base64),避免重复下载
|
||||
const result = await window.electronAPI.printPdf(pdfData);
|
||||
// 本地文件直接传原始路径,远程文件传 base64 data URI
|
||||
const printData = pdfData.startsWith("data:") ? pdfData : PDF_URL;
|
||||
const result = await window.electronAPI.printPdf(printData);
|
||||
if (!result.success) {
|
||||
alert(`打印失败: ${result.error || "未知错误"}`);
|
||||
}
|
||||
|
||||
@@ -8,4 +8,5 @@ export default defineConfig({
|
||||
server: {
|
||||
port: 5173,
|
||||
},
|
||||
assetsInclude: ["**/*.pdf"], // 确保 PDF 文件被当作静态资源处理
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user