Skip to content

feat(pdf): PDF files open view only, drawn by pdf.js, their text selectable with the mouse - #69

Merged
pierreneter merged 2 commits into
mainfrom
feat/pdf-view
Dec 4, 2026
Merged

pierreneter merged 2 commits into
mainfrom
feat/pdf-view

Conversation

@pierreneter

Copy link
Copy Markdown
Member

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:

import workerUrl from 'pdfjs-dist/build/pdf.worker.min.mjs?url'

const pdfjs = async () => {
  const lib = await import('pdfjs-dist')
  lib.GlobalWorkerOptions.workerSrc = workerUrl
  return lib
}

<DocViewer src={file} pdfjs={pdfjs} />

pdfjs also takes the URL of the module file (its worker and cmaps/ / standard_fonts/ / wasm/ are found beside it), the module itself, or { module, assets }. <office-viewer pdfjs="…/build/pdf.min.mjs"> for the standalone embed. Without pdfjs, a PDF file says it cannot be shown and offers its download.

What changes

  • Worker (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 kind pdf; nothing is parsed there. pdf.read(start, end) serves pdf.js's range requests, so a URL is read by HTTP Range. save gives the file's own bytes; dirty is always false. FileKind = DocKind | 'pdf'.
  • View (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's TextLayer over it. select.ts keeps 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's outline, setZoom / gotoPage / goto, the place in view kept through a zoom, a password prompt (PromptSpec gains secret and onCancel).
  • File menu: Download as gives the file itself and its plain text; Document details its title, author, pages, page size, version, producer; Print prints the file itself (hidden frame, else a new tab) through a new FileOps.print.
  • Off for PDF: Save, Edit, comments, collaboration. @p10node/convert rejects a PDF with code: 'unsupported'.
  • Docs: docs/api.md#pdf-files, docs/features.md, README; theme token kindPdf; German strings.
  • Test fix: locale-built.spec.ts loads the built embed as a host serves it, a static file. Through Vite's dev server, its import() of pdf.js by URL was rewritten with an import statement, 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.
  • Full e2e locally: the 7 failures left in docx-longscroll, docx-split, docx-qa2, pptx-qa2, code and markdown-math fail the same way on main on this machine; pptx.spec's long deck needs large-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

pierreneter and others added 2 commits December 4, 2026 23:12
…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>
@pierreneter
pierreneter merged commit 6bfc034 into main Dec 4, 2026
2 checks passed
@pierreneter
pierreneter deleted the feat/pdf-view branch December 4, 2026 17:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

View PDF files: pdf.js as an optional peer, text that can be selected

1 participant