PdfViewer

A clean PDF reader with page navigation, zoom, fit to width and selectable text.

Installation

npx dafink-ui add pdfviewer

Also installs: Button, Select, Spinner, Icons

Requires: pdfjs-dist

Demo

Loading PDF…

Props

NameTypeDefaultDescription
srcstring | Uint8ArrayNoneURL of the PDF, or its bytes. Required. Cross-origin URLs need CORS headers.
titlestringNoneOptional heading shown on the left of the toolbar. Also names the scrolling region; falls back to "PDF document" when omitted.
initialPagenumber1Page to scroll to once the document has loaded.
heightnumber | string'40rem'Height of the viewer. Pages scroll inside it.
toolbarTargetHTMLElement | nullNoneWhere the zoom and page controls render. Omit for the built-in toolbar row; pass an element to portal the controls into your own toolbar (no title or border of their own); pass null to hide them.
onLoad(pageCount: number) => voidNoneCalled once the document has loaded.
onError(error: Error) => voidNoneCalled when the file cannot be loaded or parsed.
onPageChange(page: number) => voidNoneCalled when the current page changes, by scrolling, the toolbar or the page field.
onPageDoubleClick(point: { page: number; x: number; y: number }) => voidNoneCalled on a double click with the page number and the position in PDF points from the page top-left corner.

Accessibility

PdfViewer uses the native tab order: the toolbar controls come first, then the page area, which is a focusable scrolling region named after the title so keyboard users can read the document without a mouse. Each page is exposed as an image labelled "Page 2 of 8", and the text layer over it is real text, so it can be selected, copied and read by screen readers. Controls that cannot act yet (before load and at the zoom limits) are disabled, and a failed load is announced with an alert.

KeyFunction
TabMoves through the toolbar, then into the page area.
EnterIn the page field, jumps to the typed page.
↑+↓Scrolls the focused page area.
Page Up+Page DownScrolls the focused page area by one screen.
Home+EndJumps to the start or end of the document.