Skip to content
RPProvider

The RPProvider component is a required component that loads the PDF document and initializes the React PDF states.

It serves as a context provider that manages the PDF viewer’s core functionality, including document loading, page rendering, zoom levels, scroll modes and more.

React components which need to access states and functionalities of RPProvider must be wrapped within RPProvider.

nametypedescriptiondefault
characterMapCharacterMap(optional) Specify the character map (CMap) to be used for text renderingundefined
darkModeboolean(optional) Set the state of dark themefalse
disableAutoFetchboolean(optional) Disable pre-fetching of PDF file data. When set to true, PDF.js will not automatically fetch more data even if it isn’t needed to display the current page, helping to reduce bandwidth usage.
NOTE: It is also necessary to disable streaming (see disableStream) in order for disabling of pre-fetching to work correctly.
false
disableStreamboolean(optional) Disable streaming of PDF file data. When set to true, PDF.js will use range requests instead of streaming to load the PDF document.false
downloadFilenamestring(optional) The name of the PDF file to be used when downloading. Remark: If undefined, the original filename will be used.undefined
initialPagenumber(optional) Set the initial page number1
initialRotationnumber(optional) Set the initial rotate degree0 (0, 90, 180, 270)
initialScalenumber | ZoomLevel(optional) The initial zoom level of the PDF document with respect to React PDF. If unspecified, it is determined by the page dimensions and the width of the container.ZoomLevel.PAGE_FIT (Scale ranges from 0 to 100.)
initialScrollModeScrollMode(optional) Set the initial scroll modeScrollMode.VERTICAL_SCROLLING
initialSearchstring(optional) Set initial text to search in PDFundefined
initialSelectionModeSelectionMode(optional) Set the initial selection modeSelectionMode.TEXT
initialThumbnailsVisibleboolean(optional) Set the initial thumbnail sidebar visibilityfalse
initialViewModeViewMode(optional) Set the initial view modeViewMode.SINGLE_PAGE
interactiveFormboolean(optional) Enable or disable form field interactions in the PDF. When set to false, form fields (text inputs, checkboxes, dropdowns, etc.) will be displayed but not interactive.true
loaderImageReact.ReactNode(optional) Custom loader to display while loading. If not provided, a default loader will be used.undefined
localestring(optional) Set the The locale string key, which must match the key of the localization object; otherwise, it will fall back to en_USen_US
localizationRecord<string, Localization>(optional) The locale object that will be used in the component{ en_US: {}, zh_CN: {}, it_IT: {}, pt_PT: {}, th_TH: {} }
onDarkModeChange(darkMode: boolean) => void(optional) A function that will be called when darkMode is changedundefined
onLoaded(pdf: PDFDocumentProxy) => void(optional) A function that will be called when the layout is loadedundefined
onLoadError(error: any) => void(optional) A function that will be called if there is an error when loading the PDF documentundefined
onLoadProgress(progress: number) => void(optional) A function that will be called when the progress of loading the PDF document is updatedundefined
onPageChange(page: number) => void(optional) A function that will be called when the current focused page is changedundefined
onRotate(rotation: Record<number, number>) => void(optional) A function that will be called when a PDF page or document is rotated in a certain directionundefined
onScroll(scrollEvent: Event) => void(optional) A function that will be called when the Viewer is scrolledundefined
rangeChunkSizenumberSpecify the maximum size (in bytes) of PDF data to be requested in each Range Request. This facilitates sectional loading of large documents.
Important: The server must support partial content requests by including the Accept-Ranges: bytes HTTP header in its response. Without this header, range requests will not work as expected.
65536 bytes
(64 KB)
srcstringSource of the PDF document.
To handle the PDF document, please refer to Handling Source of PDF File in Basic Usage.
-
import {
RPProvider,
RPDefaultLayout,
RPPages,
RPConfig,
} from '@pdf-viewer/react'
export const AppPdfViewer = () => {
const onLoadError = (error) => {
// Do something with the error
}
const onLoaded = (pdf) => {
// Do something with the pdf
}
const onDarkModeChange = (darkMode) => {
// Do something with the dark mode
}
return (
<RPConfig>
<RPProvider
src="https://cdn.codewithmosh.com/image/upload/v1721763853/guides/web-roadmap.pdf"
onLoaded={onLoaded}
characterMap={{
url: 'https://cdn.jsdelivr.net/npm/pdfjs-dist@2.0.288/cmaps/',
isCompressed: true
}}
onLoadError={onLoadError}
onDarkModeChange={onDarkModeChange}
>
<RPDefaultLayout>
<RPPages />
</RPDefaultLayout>
</RPProvider>
</RPConfig>
)
}

RPProvider provides hooks that can be used to access the PDF document and control the viewer behavior. You may refer to the Hooks Overview page for more details.