From 07e46c77f10e98d89ad45ccd0c320dc65000a2d0 Mon Sep 17 00:00:00 2001 From: Stommer Date: Thu, 22 Oct 2026 13:29:23 +0200 Subject: [PATCH] Stelle PDFs auf iPhone und iPad seitenweise dar und nutze das Legacy-Build von pdf.js. Co-Authored-By: Claude Sonnet 5.5 --- frontend/src/library/library.css | 3 + frontend/src/pages/DocumentView.jsx | 82 ++++++++++++++++++++++++++ frontend/src/pages/ReadingPane.jsx | 12 ++-- frontend/src/pages/Sources.jsx | 3 +- frontend/src/pages/widgets.jsx | 3 +- frontend/src/sources/builtin-views.jsx | 7 ++- 6 files changed, 100 insertions(+), 10 deletions(-) create mode 100644 frontend/src/pages/DocumentView.jsx diff --git a/frontend/src/library/library.css b/frontend/src/library/library.css index 2049875..12f8272 100644 --- a/frontend/src/library/library.css +++ b/frontend/src/library/library.css @@ -134,3 +134,6 @@ body.is-resizing .library__resizer { background: color-mix(in srgb, var(--accent @media (max-width: 900px) { .library__resizer { display: none; } } .inline-actions { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; } + +.document-pages { width: 100%; } +.document-pages canvas { display: block; width: 100%; margin: 0 0 8px; background: #fff; } diff --git a/frontend/src/pages/DocumentView.jsx b/frontend/src/pages/DocumentView.jsx new file mode 100644 index 0000000..8ed10fb --- /dev/null +++ b/frontend/src/pages/DocumentView.jsx @@ -0,0 +1,82 @@ +import { useEffect, useRef, useState } from "react"; +import workerSrc from "pdfjs-dist/legacy/build/pdf.worker.min.mjs?url"; + +// Safari on iPhone and iPad shows a PDF in an iframe as a white area, or only its first page. +export function isAppleTouch() { + if (typeof navigator === "undefined") return false; + return /iP(hone|ad|od)/.test(navigator.userAgent) || (navigator.platform === "MacIntel" && navigator.maxTouchPoints > 1); +} + +// Safari limits a canvas to about 16.7 million pixels. Stay below that. +const MAX_PIXELS = 12_000_000; + +// Shows a document from a blob URL. Desktop browsers use their own viewer in a frame; +// on Apple touch devices a PDF is drawn page by page, which works there. +export function DocumentView({ url, title, className = "reader-frame", frameRef = null }) { + const holder = useRef(null); + const [mode, setMode] = useState(isAppleTouch() ? "loading" : "frame"); + const [failed, setFailed] = useState(false); + + useEffect(() => { + if (!url || !isAppleTouch()) { + setMode("frame"); + return undefined; + } + let alive = true; + let doc = null; + setMode("loading"); + setFailed(false); + (async () => { + try { + const blob = await (await fetch(url)).blob(); + if (!alive) return; + if (!/pdf/i.test(blob.type)) { + setMode("frame"); + return; + } + const pdfjs = await import("pdfjs-dist/legacy/build/pdf.mjs"); + pdfjs.GlobalWorkerOptions.workerSrc = workerSrc; + doc = await pdfjs.getDocument({ data: await blob.arrayBuffer() }).promise; + if (!alive) return; + setMode("pages"); + await new Promise((resolve) => requestAnimationFrame(resolve)); + const target = holder.current; + if (!target) return; + target.replaceChildren(); + const width = target.clientWidth || 320; + const ratio = Math.min(window.devicePixelRatio || 1, 2); + for (let number = 1; number <= doc.numPages; number += 1) { + const page = await doc.getPage(number); + if (!alive) return; + const base = page.getViewport({ scale: 1 }); + let scale = (width / base.width) * ratio; + scale = Math.min(scale, Math.sqrt(MAX_PIXELS / (base.width * base.height))); + const viewport = page.getViewport({ scale }); + const canvas = document.createElement("canvas"); + canvas.width = Math.floor(viewport.width); + canvas.height = Math.floor(viewport.height); + canvas.style.width = "100%"; + canvas.style.height = "auto"; + canvas.className = "reader-canvas"; + target.appendChild(canvas); + await page.render({ canvasContext: canvas.getContext("2d"), viewport }).promise; + } + } catch { + if (alive) setFailed(true); + } + })(); + return () => { + alive = false; + doc?.destroy?.(); + }; + }, [url]); + + if (mode === "frame") return