Repository navigation
feat(pdf): PDF files open view only, drawn by pdf.js, their text selectable with the mouse - #69
Merged
Merged
Conversation
…ctable with the mouse pdfjs-dist (4 or later) is an optional peer dependency: the host gives it in the `pdfjs` prop (an import() function, the URL of its module file, or the module) and it is loaded only when a PDF file opens. Without it a PDF file says it cannot be shown and offers its download. The worker tells a PDF by its header and answers pdf.js's range requests (`pdf.read`), so a URL is read by HTTP Range. Pages near the view are drawn on a canvas with pdf.js's text layer over them; a drag over the gaps between runs of text keeps the selection on the lines beside the pointer. Links, bookmarks as the outline, zoom, page commands, a password prompt. File menu: the file itself, its plain text, details, Print prints the file. Save, Edit, comments and collaboration are off; @p10node/convert refuses a PDF file. Closes #68 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…r (TypeScript 7) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #68.
PDF files open in the viewer, view only, drawn by pdf.js, with their text laid over the pages so it can be selected with the mouse and copied.
How a host turns it on
pdfjs-dist(4 or later) is an optional peer dependency. The library does not bundle it, and nothing is loaded until a PDF file opens:pdfjsalso takes the URL of the module file (its worker andcmaps//standard_fonts//wasm/are found beside it), the module itself, or{ module, assets }.<office-viewer pdfjs="…/build/pdf.min.mjs">for the standalone embed. Withoutpdfjs, a PDF file says it cannot be shown and offers its download.What changes
engine.ts,filetype.ts): a file starting with%PDF-(or holding it in its first 1,024 bytes when its name or type says PDF) opens as kindpdf; nothing is parsed there.pdf.read(start, end)serves pdf.js's range requests, so a URL is read by HTTP Range.savegives the file's own bytes;dirtyis always false.FileKind = DocKind | 'pdf'.src/react/pdf/): only pages near the view are drawn; a canvas per page, drawn again 150 ms after a zoom settles (≤ 4096² pixels), with pdf.js'sTextLayerover it.select.tskeeps a drag over the gaps between runs on the lines beside the pointer (the browser would otherwise jump to the start of the page or the next page). Links (http(s),mailto,tel, destinations in the file), bookmarks as the controller'soutline,setZoom/gotoPage/goto, the place in view kept through a zoom, a password prompt (PromptSpecgainssecretandonCancel).FileOps.print.@p10node/convertrejects a PDF withcode: 'unsupported'.docs/api.md#pdf-files,docs/features.md, README; theme tokenkindPdf; German strings.locale-built.spec.tsloads the built embed as a host serves it, a static file. Through Vite's dev server, itsimport()of pdf.js by URL was rewritten with animportstatement, which a classic script cannot hold. Hosts that serve the file statically are not affected; the ES build keeps/* @vite-ignore */ /* webpackIgnore: true */.Tests
test/pdf-open.test.ts: detection, range reads, the same bytes saved, File → Open, asset URLs, convert refused. Unit suite: 1772 pass.e2e/pdf.spec.ts(11 tests) on PDF files written by the engine's own PDF writer (test/pdf-builder.ts,scripts/pdf-sample.ts→playground/public/samples/sample.pdf): mouse selection and copy across lines and pages, links, bookmarks, zoom, a 300-page file, File menu, pdf.js by URL, no pdf.js, a damaged file, File → Open.docx-longscroll,docx-split,docx-qa2,pptx-qa2,codeandmarkdown-mathfail the same way onmainon this machine;pptx.spec's long deck needslarge-deck.pptx.Not in this PR
A find bar over the whole file (the browser's find covers the pages near the view), page thumbnails, forms and annotations other than links.
🤖 Generated with Claude Code