Talisman LogoTalisman Logo Design System
Browse documentation
Components

PDF viewer

A PDF reader built on EmbedPDF with zoom, search, thumbnails, full screen and programmatic navigation and highlights.

Stableimport { PdfViewer } from "@repo/ui/pdf-viewer";Source

The sample is a three page PDF built in memory and passed through pdfBuffer. The viewer reads it with PDFium, a WebAssembly engine, which the page must serve at /embedpdf/pdfium.wasm. If this site does not serve that file, the preview shows an error message instead of the document: open the Storybook workshop (pnpm storybook) to see it working.

When to use

  • To read case files, subsidies, petitions and contracts inside the product without downloading them.
  • When the user needs to search the text, jump to a page, zoom, rotate or see thumbnails.
  • When the product must point to a place in the document: a highlighted area, a quoted passage or a page range.

When not to use

  • Images. Use the Image viewer.
  • Spreadsheets and CSV files. Use the Tabular viewer.
  • A small preview of the first page. Use PDFThumbnail (see below) or a thumbnail image from the server.
  • Editing or annotating the PDF. The viewer is read-only.

Variants

Variant Use for
default A reader panel: toolbar with page navigation, zoom, search, download and full screen, and thumbnails.
embedded Pages only. The parent provides any controls. Used inside chat and home panels.
review A document under review: white toolbar, page framed with a border, and download left to the feature.

theme="dark" changes the toolbar surface.

Examples

Highlight an area

highlightAreas draws percentage rectangles on a page. pageIndex is zero-based; top, left, width and height are percentages of the page. Add focusArea to scroll to one of them.

usePdfViewerNavigation returns a viewerRef for the navigationRef prop and the helpers jumpToPage (one-based) and jumpToHighlightArea.

Other ways to point at content

Prop Use for
highlightQuotes Passages quoted from the document. They are found in the text layer and marked. Needs a searchable PDF; unmatched quotes are not marked.
scrollToFirstHighlightQuote Opens on the first marked quote.
focusedPageRange A source that covers whole pages. Opens on its first page and selects the range in the thumbnails.
linkTargets Turns URI link annotations that start with a prefix into in-app actions.
sections and activeSectionId A list of named pages for the section picker.

Content

  • Pass labels for every string in the toolbar, search and thumbnails. The defaults are English.
  • Pass pageTotalPrefix (“de “) so the page count reads “1 de 12”.
  • Set documentTitle or downloadFileName so the downloaded file has a meaningful name.
  • documentKey should identify the document, not the URL: signed URLs change on every fetch and would reload the file and lose scroll and zoom.

Accessibility

  • Built on EmbedPDF. Pages are rendered as images with a text layer, so text can be selected and copied: Cmd or Ctrl + C copies the selection and a menu offers “Copy”.
  • Keyboard: Cmd or Ctrl + F opens search, F3 and Shift + F3 go to the next and previous result, Cmd or Ctrl + G focuses the page field, arrows and Page Up / Page Down change pages, Escape closes search or leaves full screen. Shortcuts apply while the viewer has focus.
  • Toolbar controls are buttons with accessible names from labels; icons are aria-hidden.
  • Loading uses role="status" with aria-busy; a load failure uses role="alert".
  • Thumbnails that belong to the focused page range include a check icon and the text of selectedThumbnailPageLabel in their accessible name.
  • Full screen uses the browser Fullscreen API on the viewer root, so the toolbar goes with the pages.
  • A PDF without a text layer (a scan) cannot be searched or selected; the search bar shows searchNoSearchableTextHint.

Code

import { PdfViewer, usePdfViewerNavigation } from "@repo/ui/pdf-viewer";

const { jumpToPage, viewerRef } = usePdfViewerNavigation();

<div className="h-[600px]">
  <PdfViewer
    documentKey={document.id}
    documentTitle={document.name}
    labels={labels}
    navigationRef={viewerRef}
    onDocumentLoad={({ doc }) => setPageCount(doc.numPages)}
    onPageChange={({ currentPage }) => setPage(currentPage + 1)}
    pageTotalPrefix={t("pageTotalPrefix")}
    src={document.url}
  />
</div>;

onPageChange reports a zero-based currentPage. jumpToPage takes a one-based page. The viewer fills its parent, so give the parent a height.

Engine setup

Need Detail
WebAssembly file Serve pdfium.wasm at /embedpdf/pdfium.wasm from the app’s public folder. packages/ui/scripts/copy-pdfium-wasm.mjs copies it from @embedpdf/pdfium; apps/app runs it in predev and prebuild.
Content Security Policy script-src needs 'wasm-unsafe-eval' and worker-src needs blob:.
Server rendering The component is client-only. In Next.js load it with dynamic(..., { ssr: false }) when it sits at page level.
Another path Wrap it in PdfViewerEngineProvider with wasmUrl.

Thumbnail of the first page

PDFThumbnail from @repo/ui/pdf-thumbnail draws one page of a PDF into a canvas with pdfjs-dist. It is separate from the viewer and has no preview here, because it loads the PDF.js worker from a CDN.

import { PDFThumbnail } from "@repo/ui/pdf-thumbnail";

<PDFThumbnail maxHeight={240} page="first" pdfUrl={document.url} />;
PDFThumbnail
PropTypeDefaultDescription
pdfUrlstring—URL of the PDF. Renders nothing when empty. Required.
maxHeightnumber800Maximum height of the canvas in pixels. The page is scaled down to fit and never up.
page"first" | "last""first"Which page to draw.
onLoadSuccess(pdf: any) => void—Called with the loaded PDF.js document.

Props

PdfViewer: document and layout
PropTypeDefaultDescription
srcstring—URL of the PDF. Required. Nothing renders when empty.
pdfBufferArrayBuffer—Bytes already downloaded. Used instead of fetching `src`.
documentKeystring—Stable identity of the document. Falls back to `src`.
documentTitlestring—Title of the document. Default name of the download.
downloadFileNamestring—File name used by the download button.
variant"default" | "embedded" | "review""default"Layout of the viewer.
theme"light" | "dark""light"Surface of the toolbar.
labelsPdfViewerNavigationLabelOverrides—Overrides for toolbar, search and thumbnail copy.
pageTotalPrefixstring—Text before the total pages, such as "de ".
classNamestring—Extra classes for the root. The root fills its parent.
contentHeaderReactNode—Notice between the toolbar and the pages. Also shown in full screen.
PdfViewer: chrome and position
PropTypeDefaultDescription
showToolbarbooleantrueShows the toolbar. Ignored by `embedded`.
showThumbnailsbooleantrueShows the thumbnail sidebar. Ignored by `embedded`.
showDownloadbooleantrueShows the download button.
showFullscreenbooleantrueShows the full screen button.
isFullscreenboolean—Controlled full screen state.
onToggleFullscreen() => void—Called to toggle full screen when controlled.
fullscreenContainerRefRefObject<HTMLElement | null>—Element that goes full screen and hosts popovers.
initialPagenumber—One-based page to open on.
initialZoomnumber | string—Zoom to open with. Defaults to fit width.
sectionsPdfViewerSection[]—Named pages (`id`, `label`, one-based `page`) for the section picker.
activeSectionIdstring—Id of the active section.
onSectionSelect(section: PdfViewerSection) => void—Called when a section is picked.
onSectionPickerOpenChange(open: boolean) => void—Called when the picker opens or closes.
PdfViewer: pointing at content
PropTypeDefaultDescription
highlightAreasPdfViewerHighlightArea[]—Percentage rectangles with a zero-based `pageIndex`.
highlightQuotesreadonly string[]—Literal passages to mark in a searchable PDF.
scrollToFirstHighlightQuotebooleanfalseOpen on the first located quote. Ignored when `focusArea` or `highlightAreas` set the position.
focusAreaPdfViewerHighlightArea | null—Area to scroll to.
focusScroll"center" | "reveal""center"How `focusArea` comes into view. `reveal` scrolls only as much as needed.
focusExtentPdfViewerHighlightArea | null—The whole passage that `focusArea` belongs to, for `reveal`.
zoomToFocusAreabooleanfalseFit the page width before scrolling to `focusArea`.
focusedPageRangePdfViewerPageRange | null—Inclusive one-based range selected in thumbnails. Opens on its first page.
linkTargetsPdfViewerLinkTargets—Makes link annotations with a URI prefix clickable in the app.
PdfViewer: events
PropTypeDefaultDescription
navigationRefRef<PdfViewerHandle>—Handle with `jumpToPage` (one-based), `jumpToHighlightArea`, `scrollToPosition`, `getCurrentPage`.
onDocumentLoad(event: { doc: { numPages: number } }) => void—Called when the document opens.
onDocumentLoadError() => void—Called once when the document cannot be opened.
onPageChange(event: { currentPage: number }) => void—Called when the page changes. `currentPage` is zero-based.
onZoomChange(event: PdfZoomChangeEvent) => void—Called when the zoom changes.
PdfViewer: performance
PropTypeDefaultDescription
pageImageType"image/png" | "image/bmp""image/png"Format of the page bitmaps. BMP skips PNG encoding and uses more memory.
renderVisiblePagesFirstbooleanfalseRender the visible pages before the scroll buffer.
selectionPages"none" | "rendered" | "visible""rendered"Which pages load text selection data.
onVisiblePagesRendered() => void—Called when every page in the viewport has its image.

Known issues

Issue Effect
The load error and the engine error use text-destructive, which the Tailwind preset does not define (only old-destructive). The message takes the inherited text color, not a critical color.
Default labels are English text inside the package (DEFAULT_PDF_VIEWER_NAVIGATION_LABELS). An app that passes only part of labels shows English in the rest of the toolbar.
The engine loads /embedpdf/pdfium.wasm, and a font fallback may load from a CDN. Without the file, the viewer shows an error. Each host app must serve it and allow it in the CSP.
PDFThumbnail sets the PDF.js worker to https://unpkg.com/pdfjs-dist@3.11.174/build/pdf.worker.js. It fails on networks that block the CDN, and the version can differ from the installed pdfjs-dist.
PDFThumbnail renders a canvas with no accessible name, and a failed load leaves it blank with no message. Screen readers get nothing; users see an empty space.
Edit this page on GitHub