React Native PDF View Component
react-native-pdf is an actively maintained React Native component designed for displaying PDF documents across iOS, Android, and Windows platforms. The current stable version is 7.0.4, with releases occurring frequently to address bugs and add features. It enables developers to render PDFs from various sources, including URLs, blobs, local files, and app assets, with built-in caching capabilities. Key features include horizontal or vertical display, drag and zoom gestures, double-tap zoom, support for password-protected PDFs, and the ability to jump to specific pages. It differentiates itself by offering comprehensive cross-platform support and robust viewing functionalities, leveraging `react-native-blob-util` for efficient file system interactions. For Expo users, it requires a Custom Dev Client and an Expo Config Plugin rather than Expo Go. This library is a strong choice for applications requiring native PDF rendering capabilities.
Common errors
-
java.lang.IllegalStateException: Tried to access a JS module before the React instance was fully set up
cause This error typically indicates that native modules are being accessed too early in the React Native lifecycle or that there's an issue with the native module linking/initialization.fixEnsure `react-native-pdf` and `react-native-blob-util` are correctly linked (automatic for RN 0.60+ with `pod install`, manual for older RN versions). Rebuild your native project after installation. This particular issue was fixed in `v6.7.6`, so upgrading might resolve it. -
Undefined symbol: _OBJC_CLASS_$_RCTPdfViewManager
cause This usually means the iOS native module for `react-native-pdf` is not correctly linked or compiled.fixFor React Native 0.60 and above, navigate to your `ios/` directory and run `pod install`. For older RN versions, ensure `react-native link react-native-pdf` was executed. Clean your Xcode build folder and try rebuilding. -
Error: unable to find or load PDF document
cause The `source` prop is invalid, the URI is incorrect, the file doesn't exist at the specified path, or there are network issues preventing download.fixDouble-check the `uri` or `filePath` in your `source` prop for typos or incorrect paths. Ensure network connectivity for remote PDFs. For local files, verify permissions and that the file actually exists on the device storage. -
Error: Invariant Violation: ViewPropTypes is deprecated and will be removed from React Native in a future release. Use Image.propTypes instead.
cause This error occurs in older versions of `react-native-pdf` or React Native when `ViewPropTypes` was directly imported from `react-native` instead of `deprecated-react-native-prop-types`.fixUpgrade `react-native-pdf` to `v6.6.0` or higher, which includes a fix to migrate to `deprecated-react-native-prop-types`. -
Android build error related to `pickFirst 'lib/x86/libc++_shared.so'` or similar shared library conflicts.
cause Missing or incorrect `packagingOptions` in your `android/app/build.gradle` file, leading to conflicts when bundling native libraries.fixAdd the `packagingOptions` block exactly as specified in the `react-native-pdf` Android installation instructions to your `android { ... }` block in `android/app/build.gradle`.
Warnings
- breaking Android builds for older projects might break due to the removal of `jcenter()` repository in favor of `mavenCentral()` (or `maven {}`).
- gotcha The package `react-native-blob-util` is a required peer and runtime dependency that must be explicitly installed alongside `react-native-pdf`. Failing to install it will lead to runtime errors or crashes related to file system operations.
- gotcha Expo Go does not support `react-native-pdf`. To use this package within an Expo project, you must use a Custom Dev Client with the provided Expo Config Plugin.
- gotcha For React Native 0.59.0 and above on Android, specific `packagingOptions` must be added to your `android/app/build.gradle` to prevent issues with native libraries (`libc++_shared.so`, `libjsc.so`).
- breaking Version 7.0.0 and 7.0.1 included updates for Android 16 KB Page Size support and upgrading Fabric Example to Latest React Native 0.81.0, which may imply breaking changes or require updates for projects using Fabric architecture with older React Native versions.
Install
-
npm install react-native-pdf -
yarn add react-native-pdf -
pnpm add react-native-pdf
Imports
- PDF
const PDF = require('react-native-pdf');import PDF from 'react-native-pdf';
- PDF source object
const source = { uri: 'http://example.com/test.pdf', cache: true }; - StyleSheet
import { StyleSheet } from 'react-native';
Quickstart
import React from 'react';
import { StyleSheet, View, Text, Dimensions } from 'react-native';
import PDF from 'react-native-pdf';
const MyPdfViewer = () => {
// Example source: a public PDF URL
const source = {
uri: 'https://www.africau.edu/images/default/sample.pdf',
cache: true,
};
// For a local file, you might use:
// const localSource = { uri: 'file:///data/user/0/com.yourpackage/files/my_document.pdf' };
// For an asset:
// const assetSource = require('./assets/document.pdf');
return (
<View style={styles.container}>
<Text style={styles.title}>Viewing a PDF Document</Text>
<PDF
source={source}
onLoadComplete={(numberOfPages, filePath) => {
console.log(`Number of pages: ${numberOfPages}`);
}}
onPageChanged={(page, numberOfPages) => {
console.log(`Current page: ${page} / ${numberOfPages}`);
}}
onError={(error) => {
console.log(error);
}}
onPressLink={(uri) => {
console.log(`Link pressed: ${uri}`);
}}
style={styles.pdf}
enablePaging={true} // Enable horizontal paging for multi-page PDFs
page={1}
scale={1.0}
minScale={0.5}
maxScale={3.0}
fitPolicy={0} // 0: width, 1: height, 2: both
/>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'flex-start',
alignItems: 'center',
marginTop: 25,
},
title: {
fontSize: 20,
fontWeight: 'bold',
marginBottom: 10,
},
pdf: {
flex: 1,
width: Dimensions.get('window').width,
height: Dimensions.get('window').height - 100, // Adjust height to leave space for title
},
});
export default MyPdfViewer;