Components
React PDF is built using a composable component architecture where each component serves a specific purpose. Here’s a detailed breakdown of how these components work together:
Anatomy
Section titled “Anatomy”<RPConfig> {/* Configure global settings and license */} <RPTheme> {/* Manage theming and styling (optional) */} <RPProvider> {/* Handle PDF document loading and state */} <RPDefaultLayout> {/* Provide the default toolbar structure */} <RPPages /> {/* Render the actual PDF content */} </RPDefaultLayout> </RPProvider> </RPTheme></RPConfig>Each component wraps the next in a hierarchical structure:
- RPConfig - To add a license key or override
pdfjs-distworker source. - RPTheme - To customize React PDF’s theme.
- RPProvider - To configure
srcof the PDF document and initialize default component states. - RPDefaultLayout - To render the default toolbar, including the top bar and sidebar.
- RPPages - To display all PDF pages.
- RPHorizontalBar - (optional) To display the tools of the horizontal bar.
- RPVerticalBar - (optional) To display the tools of the horizontal bar.
At the fundamental level, you need three components to render a bare minimum PDF viewer.
RPConfig- To ensure the license key is setRPProvider- To ensure the PDF document (src) is set and load the necessary statesRPPages- To render all of the PDF pages
Although RPDefaultLayout is not required, it is recommended if you are looking for a ready-to-use toolbar layout.
Here are some common examples:
Section titled “Here are some common examples:”Basic Setup (Recommended)
Section titled “Basic Setup (Recommended)”If you need to render a PDF document with the default layout.
Example
import { RPConfig, RPProvider, RPDefaultLayout, RPPages } from '@pdf-viewer/react'
export default () => ( <RPConfig licenseKey={...}> <RPProvider src={...}> <RPDefaultLayout> <RPPages /> </RPDefaultLayout> </RPProvider> </RPConfig> )Minimal Setup
Section titled “Minimal Setup”If you only need basic PDF rendering without the toolbar and sidebar.
Example
import { RPConfig, RPProvider, RPPages } from '@pdf-viewer/react'
export default () => ( <RPConfig licenseKey={...}> <RPProvider src={...}> {/* Ensure the container has height and width for proper PDF rendering */} <div style={{ height: '600px', width: '768px' }}> <RPPages /> </div> </RPProvider> </RPConfig> )Custom Theme
Section titled “Custom Theme”If you need to customize the appearance while keeping the default layout.
Example
import { RPConfig, RPTheme, RPProvider, RPDefaultLayout, RPPages } from '@pdf-viewer/react'
export default () => ( <RPConfig licenseKey={...}> <RPTheme customVariables={{ '--rp-toolbar-background': 'red', '--rp-text-color': 'yellow' }} > <RPProvider src={...}> <RPDefaultLayout> <RPPages /> </RPDefaultLayout> </RPProvider> </RPTheme> </RPConfig> )