Merge branch 'main' of https://git.ambigrat.com/hom/yuanhe-checkin-electron
This commit is contained in:
@@ -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,27 +28,37 @@ 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
|
||||
setPdfData(PDF_URL);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
setLoading(true);
|
||||
const result = await window.electronAPI.fetchPdf(PDF_URL);
|
||||
if (result.success && result.data) {
|
||||
// 将base64转换为data URL
|
||||
setPdfData(`data:application/pdf;base64,${result.data}`);
|
||||
|
||||
// 本地文件直接使用,无需通过 fetchPdf
|
||||
if (PDF_URL.startsWith("/") || PDF_URL.startsWith("blob:") || PDF_URL.includes("assets")) {
|
||||
setPdfData(PDF_URL);
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
// 远程 URL 需要通过 Electron 绕过 CORS
|
||||
if (window.electronAPI?.fetchPdf) {
|
||||
const result = await window.electronAPI.fetchPdf(PDF_URL);
|
||||
if (result.success && result.data) {
|
||||
// 将base64转换为data URL
|
||||
setPdfData(`data:application/pdf;base64,${result.data}`);
|
||||
} else {
|
||||
setError(`PDF加载失败: ${result.error || "未知错误"}`);
|
||||
}
|
||||
} else {
|
||||
setError(`PDF加载失败: ${result.error || "未知错误"}`);
|
||||
// 非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 || "未知错误"}`);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user