Skip to content
React PDF Viewer

Search

PdfViewer reference

A PDF viewer: one page at a time with a toolbar for zoom, navigation, fullscreen, rotation, download and print.

Default props give a classic single-page layout; every additional feature (text layer, links, continuous layout, thumbnails, search, …) is opt-in.

Used by

Import

import { PdfViewer } from '@kingtech/react-pdf-viewer';

Props

NameTypeDefaultRequiredDescription
sourcePdfSource | null—YesThe document: a URL string or URL, or ArrayBuffer / Uint8Array / Blob / File. null, undefined and '' show the empty state.
fileNamestringderived from the sourceNoDownload file name. Default: a File’s name, the URL’s last .pdf segment, else document.pdf.
pagenumber—NoControlled 1-based page. Out-of-range values are clamped and reported through onPageChange.
defaultPagenumber1NoInitial page while uncontrolled, and the page every new document opens on.
scalenumber—NoControlled scale: 1 = 100%, one PDF point per CSS pixel.
defaultScalenumber1NoInitial scale while uncontrolled; also the reset-zoom target and the scale at which fitting applies.
rotationRotation—NoControlled clockwise rotation, added to each page’s intrinsic rotation.
defaultRotationRotation0NoInitial rotation while uncontrolled.
fullscreenboolean—NoControlled fullscreen state. Passing it selects fullscreenMode="controlled" unless a mode is set.
defaultFullscreenbooleanfalseNoInitial fullscreen state while uncontrolled.
fullscreenModeFullscreenMode'controlled' when fullscreen is passed, else 'native'Nocontrolled: layout only, the parent presents fullscreen (e.g. in its own dialog). native: the browser Fullscreen API on the viewer root, falling back to overlay. overlay: a fixed full-viewport layer with a focus trap; Esc closes it.
minScalenumber0.25NoZoom floor (25%).
maxScalenumber5NoZoom ceiling (500%).
scaleStepnumber0.05NoIncrement of the zoom buttons and the + / - keys.
zoomLevelsreadonly number[]—NoPreset ladder, e.g. [0.5, 1, 1.5, 2, 4]: the zoom buttons step between presets instead of by scaleStep.
fitWidthAtDefaultScalebooleantrueNoAt the default scale, shrink the page to the available width.
fitMode'page' | 'none' | 'width''none'NoAt the default scale, fill the width (width) or fit the whole page (page). Zooming away shows the page at true size.
passwordstring—NoPassword for encrypted documents. Changing it reloads the document.
pdfjsOptionsPdfJsDocumentOptions—NoPer-instance getDocument overrides such as cMapUrl, standardFontDataUrl or verbosity. Source-related options are managed by the viewer.
toolbarboolean | ToolbarConfigtrueNofalse hides the toolbar; an object sets position, actions and hiddenWhenCompact.
compactBreakpointnumber960NoViewport width (px) below which the compact layout applies: less padding, and hiddenWhenCompact actions hidden outside fullscreen.
keyboardShortcutsbooleantrueNoKeyboard shortcuts while focus is inside the viewer.
printMode'render' | 'open-url''render'Norender prints through a hidden iframe, with progress and Cancel; open-url opens the PDF in a new tab.
maxCanvasPixelsnumber16777216NoCanvas pixel budget per page (4096 × 4096). Above it, resolution degrades gracefully instead of failing.
labelsPartial<PdfViewerLabels>English (defaultLabels)NoOverrides for any visible string or accessible name. Keep the object stable (e.g. at module scope).
localestringthe browser localeNoBCP 47 locale for number formatting, e.g. hu-HU.
layout'single' | 'continuous''single'NoOne page at a time or vertical scrolling through all pages. Only the visible pages ±1 are rendered; the page indicator follows the scroll position.
textLayerbooleanfalseNoSelectable, copyable text that assistive technology can read.
annotationLayerbooleanfalseNoClickable links: internal links navigate, external links open in a new tab with rel="noopener noreferrer".
pageInputbooleanfalseNoEditable page number in the toolbar.
zoomResetbooleanfalseNoThe zoom label becomes a button that resets zoom.
wheelZoombooleanfalseNoCtrl / ⌘ + wheel and trackpad pinch zoom, anchored at the pointer.
passwordPromptbooleanfalseNoAsk for the password of encrypted documents instead of reporting PASSWORD_REQUIRED.
renderPasswordPrompt(context: { incorrect: boolean; submit: (password: string) => void }) => ReactNode—NoReplaces the built-in password form (with passwordPrompt).
thumbnailsbooleanfalseNoLazily rendered page thumbnails sidebar, hidden in compact, non-fullscreen mode.
renderLoading() => ReactNode—NoReplaces the loading view.
renderError(error: PdfViewerError, actions: { retry: () => void }) => ReactNode—NoReplaces the document and page error views.
renderToolbar(api: PdfViewerApi) => ReactNode—NoReplaces the toolbar. Render <PdfToolbar api={api} /> inside to reuse parts.
renderEmpty() => ReactNode—NoReplaces the view shown without a source.
classNamestring—NoAdded to the root (.rpv-root). rpv-theme-dark and rpv-theme-auto select the dark presets.
styleCSSProperties—NoInline styles on the root, e.g. CSS variables.
idstring—NoId of the root element.
aria-labelstringlabels.viewerNoAccessible name of the viewer region.
requestInitRequestInit—NoExtra fetch options for URL sources: headers, credentials, mode, …
fetcher(url: string, init: RequestInit) => Promise<Response>—NoReplaces fetch entirely, e.g. with an authenticated HTTP client.
getHttpErrorMessage(response: Response) => Promise<string>—NoBuilds the HTTP_ERROR message. Return undefined for the default: an RFC 7807 detail, then title, then a JSON string body, then Failed to fetch PDF: {status} {statusText}.

Events

NameTypeDefaultRequiredDescription
onPageChange(page: number) => void—NoCalled whenever the page changes, controlled or not.
onScaleChange(scale: number) => void—NoCalled whenever the scale changes.
onRotationChange(rotation: Rotation) => void—NoCalled whenever the rotation changes.
onFullscreenChange(fullscreen: boolean) => void—NoCalled whenever fullscreen is entered or left.
onDocumentLoad(info: { numPages: number; fingerprint: string }) => void—NoA document finished loading.
onPageRender(info: PageRenderInfo) => void—NoA page render completed.
onError(error: PdfViewerError) => void—NoEvery failure except aborted loads, canceled renders and HTTP errors handled by onHttpError.
onDownload(context: { fileName: string; data: Uint8Array }) => false | void—NoBefore saving; data holds the already-loaded bytes. Return false to handle the download yourself.
onPrint() => false | void—NoBefore printing. Return false to handle printing yourself.
onHttpError(response: Response) => boolean | void | Promise<boolean | void>—NoCalled for non-2xx responses. Return true when handled (e.g. after redirecting to sign-in): the viewer shows its empty state and does not call onError.

CSS classes

KeyClassDescription
rpv-root.rpv-rootThe root element. Add <code>rpv-theme-dark</code>, or <code>rpv-theme-auto</code> to follow the operating system, for the dark preset.
data-status[data-status]Document status.
data-layout[data-layout]The <code>layout</code> prop.
data-compact[data-compact]The viewport is narrower than <code>compactBreakpoint</code>.
data-fullscreen[data-fullscreen]In fullscreen, in any mode.
data-presentation[data-presentation]How fullscreen is presented; <code>layout</code> in controlled mode.
data-zoomed[data-zoomed]The scale is above <code>defaultScale</code>.

Source

packages/react-pdf-viewer/src/PdfViewer.tsx

Generated from content/react-pdf-viewer/reference/pdf-viewer.schema.json. Do not edit by hand.