PDF Component API
Complete API reference for the Pdf component in react-native-pdf-jsi.
Import
const PdfModule = require('react-native-pdf-jsi');
const Pdf = PdfModule.default;
Props
source (required)
PDF source object with the following properties:
| Property | Type | Default | Description |
|---|---|---|---|
uri | string | required | PDF source URI (URL, file path, base64, etc.) |
cache | boolean | false | Enable caching for the PDF |
cacheFileName | string | SHA1(uri) | Custom cache file name |
expiration | number | 0 | Cache expiration in seconds (0 = never expires) |
method | string | "GET" | HTTP method for URL sources |
headers | object | HTTP headers for URL sources |
Source URI Types:
http://orhttps://- Load from URLfile://- Load from local file systemdata:application/pdf;base64,- Load from base64 stringbundle-assets://- Load from app bundle/assetsrequire('./file.pdf')- Load from bundled asset (iOS only)
page
| Type | Default | Description |
|---|---|---|
| number | 1 | Initial page to display (1-indexed) |
scale
| Type | Default | Description |
|---|---|---|
| number | 1.0 | Initial zoom scale (must be between minScale and maxScale) |
minScale
| Type | Default | Description |
|---|---|---|
| number | 1.0 | Minimum zoom scale |
maxScale
| Type | Default | Description |
|---|---|---|
| number | 3.0 | Maximum zoom scale |
horizontal
| Type | Default | Description |
|---|---|---|
| boolean | false | Display pages horizontally (true) or vertically (false) |
fitPolicy
| Type | Default | Description |
|---|---|---|
| number | 2 | Fit policy: 0 = fit width, 1 = fit height, 2 = fit both |
spacing
| Type | Default | Description |
|---|---|---|
| number | 10 | Spacing between pages in pixels |
password
| Type | Default | Description |
|---|---|---|
| string | "" | Password for password-protected PDFs |
enablePaging
| Type | Default | Description |
|---|---|---|
| boolean | false | Show only one page at a time |
enableRTL
| Type | Default | Description |
|---|---|---|
| boolean | false | Enable right-to-left page order |
enableAntialiasing
| Type | Default | Description |
|---|---|---|
| boolean | true | Enable antialiasing for better rendering (Android only) |
enableAnnotationRendering
| Type | Default | Description |
|---|---|---|
| boolean | true | Enable annotation rendering |
enableDoubleTapZoom
| Type | Default | Description |
|---|---|---|
| boolean | true | Enable double tap to zoom gesture |
singlePage
| Type | Default | Description |
|---|---|---|
| boolean | false | Show only the first page (useful for thumbnails) |
trustAllCerts
| Type | Default | Description |
|---|---|---|
| boolean | true | Allow connections to servers with self-signed certificates |
style
| Type | Default | Description |
|---|---|---|
| object | React Native style object for the PDF container |
Event Handlers
onLoadProgress
Called during PDF loading to report progress.
onLoadProgress={(percent) => {
console.log(`Loading: ${(percent * 100).toFixed(1)}%`);
}}
Parameters:
percent(number): Loading progress from 0 to 1
onLoadComplete
As of v4.1.0, this callback now works reliably on Android. The fix addresses timing issues and error handling specific to Android's implementation. iOS has always worked correctly using a notification-based approach. See What's New in v4.1.0 for details.
Called when PDF loading is complete.
onLoadComplete={(numberOfPages, filePath, size, tableContents) => {
console.log(`PDF loaded: ${numberOfPages} pages`);
console.log(`File path: ${filePath}`);
console.log(`Size: ${size.width} x ${size.height}`);
console.log(`Table of contents:`, tableContents);
}}
Parameters:
numberOfPages(number): Total number of pages in the PDFfilePath(string): Local file path of the loaded PDFsize(object): Page size withwidthandheightpropertiestableContents(array): Table of contents structure (if available)
onPageChanged
Called when the displayed page changes.
onPageChanged={(page, numberOfPages) => {
console.log(`Page ${page} of ${numberOfPages}`);
}}
Parameters:
page(number): Current page number (1-indexed)numberOfPages(number): Total number of pages
onError
Called when an error occurs.
onError={(error) => {
console.error('PDF Error:', error);
if (error.message && error.message.includes('password')) {
// Handle password-protected PDF
}
}}
Parameters:
error(object): Error object withmessageproperty
onPageSingleTap
Called when a page is single-tapped.
onPageSingleTap={(page) => {
console.log(`Tapped page ${page}`);
}}
Parameters:
page(number): Page number that was tapped (1-indexed)
onScaleChanged
Called when the zoom scale changes.
onScaleChanged={(scale) => {
console.log(`Scale: ${scale}`);
}}
Parameters:
scale(number): Current zoom scale
onPressLink
Called when a link in the PDF is tapped.
onPressLink={(uri) => {
console.log(`Link pressed: ${uri}`);
// Handle link navigation
}}
Parameters:
uri(string): URI of the pressed link
Methods
setPage(pageNumber)
Programmatically navigate to a specific page.
const pdfRef = useRef(null);
<Pdf ref={pdfRef} source={source} />
// Navigate to page 5
pdfRef.current?.setPage(5);
Parameters:
pageNumber(number): Page number to navigate to (1-indexed)
Returns: void
Platform Support
| Feature | iOS | Android | Windows |
|---|---|---|---|
| Basic viewing | ✅ | ✅ | ✅ |
| Zoom & pan | ✅ | ✅ | ✅ |
| Password protection | ✅ | ✅ | ✅ |
| onLoadComplete | ✅ | ✅ (v4.1.0+) | ✅ |
| onPageChanged | ✅ | ✅ | ✅ |
| onError | ✅ | ✅ | ✅ |
| onPageSingleTap | ✅ | ✅ | ✅ |
| onScaleChanged | ✅ | ✅ | ✅ |
| onPressLink | ✅ | ❌ | ❌ |
| enableAntialiasing | ❌ | ✅ | ❌ |
Examples
Basic Usage
const PdfModule = require('react-native-pdf-jsi');
const Pdf = PdfModule.default;
<Pdf
source={{ uri: 'https://example.com/document.pdf' }}
style={{ flex: 1 }}
/>
With Event Handlers
const PdfModule = require('react-native-pdf-jsi');
const Pdf = PdfModule.default;
<Pdf
source={{ uri: 'https://example.com/document.pdf' }}
style={{ flex: 1 }}
onLoadComplete={(numberOfPages, filePath, size, tableContents) => {
console.log(`Loaded ${numberOfPages} pages`);
}}
onPageChanged={(page, numberOfPages) => {
console.log(`Page ${page} of ${numberOfPages}`);
}}
onError={(error) => {
console.error('Error:', error);
}}
/>
Password-Protected PDF
const PdfModule = require('react-native-pdf-jsi');
const Pdf = PdfModule.default;
<Pdf
source={{ uri: 'https://example.com/protected.pdf' }}
password="yourpassword"
style={{ flex: 1 }}
onError={(error) => {
if (error.message && error.message.includes('password')) {
// Handle password error
}
}}
/>
Related Documentation
- Quick Start Guide
- Installation
- What's New in v4.1.0 - Android onLoadComplete fix