fix(ui): render PDFs with pdf.js instead of an iframe
Nightly Build / build (push) Successful in 7m59s
Nightly Build / build (push) Successful in 7m59s
On iOS the file viewer showed only the first page of a PDF, with no way to scroll to the rest — the document had to be downloaded and opened in another app. The cause was not ours: WebKit refuses to mount its PDF viewer inside an <iframe>/<object>/<embed> and paints a static first-page thumbnail instead. That hits Safari on iOS and every WKWebView, so the native shell too. The full viewer only exists for a top-level navigation. The desktop browsers do mount a viewer, but each mounts its own — Chrome's toolbar, Safari's page-index sidebar — so the same document also looked different on every machine. Both are answered by drawing the pages ourselves. New <pdf-view> (web/components/shared/pdf-view.js) renders a continuous scroll of canvas pages on the vendored pdf.js, with a zoom control and a page counter, and replaces the iframe for both native .pdf files and server-compiled LaTeX. Three properties are load-bearing: - pdf.js is imported lazily (~450 KB + a 1.2 MB worker), so a session that never opens a PDF never pays for it. - Canvases are created and destroyed as pages scroll. iOS caps the total canvas backing store a page may hold and silently blanks canvases past it, so an eager render would come out empty on exactly the platform this was written for. Off-screen pages keep only a correctly-sized box, which is also what keeps the scrollbar honest. - The text layer (selection, in-page find) is best-effort: it is transparent DOM over the pixels, so its failures are swallowed rather than surfaced. Vendored from pdfjs-dist 6.2.108: pdf.min.mjs, pdf.worker.min.mjs, the standard-font data (needed by PDFs that reference Helvetica/Times without embedding them) and the .textLayer block of pdf_viewer.css. CJK cmaps are deliberately left out. pdf.js 6 needs Safari/iOS 17.4+. Verified in headless Chromium against both a synthetic 12-page PDF using non-embedded Helvetica and a real 14-page paper with embedded fonts and figures: all pages present, last page renders after scrolling, page 1 released off-screen, text layer populated, zoom re-renders at the new scale, no JS errors.
This commit is contained in:
+115
-1
@@ -262,13 +262,127 @@
|
||||
|
||||
/* ── PDF preview ─────────────────────────────────────────────────────────────── */
|
||||
|
||||
/* PDFs are drawn by <pdf-view> (web/components/shared/pdf-view.js) on canvas, not
|
||||
handed to the browser's built-in viewer: WebKit paints only a static first page
|
||||
inside a frame — which is why iOS showed page 1 and nothing else — and the
|
||||
desktop browsers each mount a different viewer chrome. `.fv-pdf` is the block
|
||||
the viewer body gives it; everything below is that component's own layout. */
|
||||
.fv-pdf {
|
||||
display: block;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
border: none;
|
||||
min-height: 0;
|
||||
}
|
||||
|
||||
.pdfv {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
height: 100%;
|
||||
min-height: 0;
|
||||
}
|
||||
|
||||
.pdfv-toolbar {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.4rem;
|
||||
padding: 0.35rem 0.6rem;
|
||||
border-bottom: 1px solid var(--bs-border-color);
|
||||
background: var(--bs-tertiary-bg);
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.pdfv-btn {
|
||||
border: 1px solid var(--bs-border-color);
|
||||
background: var(--bs-body-bg);
|
||||
color: var(--bs-body-color);
|
||||
border-radius: var(--radius-sm);
|
||||
padding: 0.15rem 0.5rem;
|
||||
line-height: 1.4;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.pdfv-btn:hover:not(:disabled) { background: var(--bs-secondary-bg); }
|
||||
.pdfv-btn:disabled { opacity: 0.4; cursor: default; }
|
||||
|
||||
.pdfv-zoom {
|
||||
min-width: 3.5rem;
|
||||
text-align: center;
|
||||
font-size: 0.85rem;
|
||||
color: var(--bs-secondary-color);
|
||||
font-variant-numeric: tabular-nums;
|
||||
}
|
||||
|
||||
.pdfv-pageno {
|
||||
margin-left: auto;
|
||||
font-size: 0.85rem;
|
||||
color: var(--bs-secondary-color);
|
||||
font-variant-numeric: tabular-nums;
|
||||
}
|
||||
|
||||
/* The scroll root. `position: relative` makes it the offsetParent of every page
|
||||
box, which is what lets the component compare `offsetTop` against `scrollTop`
|
||||
when it decides which pages hold pixels. */
|
||||
.pdfv-scroll {
|
||||
position: relative;
|
||||
flex: 1;
|
||||
min-height: 0;
|
||||
overflow: auto;
|
||||
overscroll-behavior: contain;
|
||||
-webkit-overflow-scrolling: touch;
|
||||
background: var(--bs-secondary-bg);
|
||||
}
|
||||
|
||||
/* `width: max-content` + `min-width: 100%` is what makes zoom usable: with a
|
||||
plain `align-items: center`, a page wider than the scroll box overflows on
|
||||
*both* sides and the left half becomes unreachable — centring happens before
|
||||
scrolling exists. Growing this box to the widest page instead gives an honest
|
||||
horizontal scroll range, while min-width keeps pages centred when they fit. */
|
||||
.pdfv-pages {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
width: max-content;
|
||||
min-width: 100%;
|
||||
gap: 0.6rem;
|
||||
padding: 0.6rem;
|
||||
}
|
||||
|
||||
/* A page box is sized before it is drawn, so the scrollbar is honest from the
|
||||
start and a page that is currently released leaves no gap. The pdf.js text
|
||||
layer positions itself against these CSS variables. */
|
||||
.pdfv-page {
|
||||
position: relative;
|
||||
background: #fff;
|
||||
box-shadow: 0 1px 6px rgb(0 0 0 / 0.35);
|
||||
overflow: hidden;
|
||||
--user-unit: 1;
|
||||
--total-scale-factor: calc(var(--scale-factor) * var(--user-unit));
|
||||
--scale-round-x: 1px;
|
||||
--scale-round-y: 1px;
|
||||
}
|
||||
|
||||
.pdfv-canvas {
|
||||
display: block;
|
||||
}
|
||||
|
||||
.pdfv-page-failed::after {
|
||||
content: "!";
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
color: var(--bs-danger, #b91c1c);
|
||||
font-size: 2rem;
|
||||
}
|
||||
|
||||
.pdfv-error-detail {
|
||||
margin-top: 0.4rem;
|
||||
font-size: 0.8rem;
|
||||
opacity: 0.75;
|
||||
word-break: break-word;
|
||||
}
|
||||
|
||||
/* ── HTML live preview ───────────────────────────────────────────────────────── */
|
||||
|
||||
/* Rendered in an origin-isolated iframe (srcdoc + sandbox="allow-scripts").
|
||||
|
||||
@@ -797,6 +797,19 @@ mobile-app[data-native] .chat-page-input-area { padding-bottom: 10px; }
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
/* PDF viewer (<pdf-view>): the desktop toolbar is mouse-sized. Give the zoom
|
||||
buttons a real tap target — on mobile they are the *only* zoom, since
|
||||
mobile.html ships `user-scalable=no` and pinch is disabled page-wide. */
|
||||
.mobile-file-viewer .pdfv-toolbar {
|
||||
padding: 0.4rem 0.75rem;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
.mobile-file-viewer .pdfv-btn {
|
||||
min-width: 44px;
|
||||
min-height: 36px;
|
||||
font-size: 1rem;
|
||||
}
|
||||
|
||||
/* ── Settings ─────────────────────────────────────────────────────────────── */
|
||||
|
||||
.mobile-settings {
|
||||
|
||||
Reference in New Issue
Block a user