Integrate Nutrient React Native dependency into Android and iOS applications

This guide walks you through the steps necessary to integrate Nutrient React Native dependency into your Android or iOS application. By the end, you’ll be able to present a PDF document in the default Nutrient user interface (UI).

Requirements

Install the following required packages depending on your platform (Android or iOS):

The Nutrient React Native dependency is installed from the GitHub repository and not the npm registry. To install the Nutrient React Native dependency, run yarn add react-native-pspdfkit@github:PSPDFKit/react-native in your project directory or npm install github:PSPDFKit/react-native if you’re using npm.

Creating a new project

If you already have an existing React Native project that runs on both Android and iOS using the latest version of React Native, skip to the Installation step. Otherwise, create a new one by following the steps below:

  1. In the terminal, change the current working directory to where you’ll save your project. This example uses the pdf folder created in the home directory:

    Terminal window
    mkdir ~/pdf
    cd ~/pdf
  2. Create the React Native project by running the following command:

    Terminal window
    npx @react-native-community/cli init PSPDFKitDemo

Installing the Nutrient dependency

  1. Open the terminal app and change the location of the current working directory inside your project:

    Terminal window
    cd path/to/YourProject
  2. Add the Nutrient plugin:

    Terminal window
    yarn add react-native-pspdfkit@github:PSPDFKit/react-native
  3. Install all the dependencies for the project:

    Terminal window
    yarn install
  4. Based on whether you are installing Nutrient React Native dependency into an Android or iOS application, make your selection and follow the steps below.

4.1. Open your project’s build.gradle file:

Terminal window
open android/build.gradle

4.2. Add the Nutrient repository to download the Nutrient library:

...
allprojects {
repositories {
mavenLocal()
maven {
url 'https://my.nutrient.io/maven/'
}
...

4.3. Open the app’s build.gradle file:

Terminal window
open android/app/build.gradle

4.4. Modify the compile SDK version and the minimum SDK version:

...
android {
compileSdkVersion rootProject.ext.compileSdkVersion
compileSdkVersion 34
...
defaultConfig {
applicationId "com.pspdfkitdemo"
minSdkVersion rootProject.ext.minSdkVersion
minSdkVersion 21
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "1.0"
}
}
...

Displaying a PDF

  1. Add the PDF document you want to display to your application by dragging it into your project. On the dialog that’s displayed, select Finish to accept the default integration options. You can use this Quickstart Guide PDF as an example.

    drag-and-drop-document
  2. Change the location of the current working directory back to the root project folder:

    Terminal window
    cd ..
  3. Create the assets directory if you don’t have one already:

    Terminal window
    mkdir android/app/src/main/assets
  4. Copy a PDF document into your assets directory:

    Terminal window
    cp ~/Downloads/Document.pdf android/app/src/main/assets/Document.pdf
  5. Use the PSPDFKitView React component in your project:

    import { Platform } from 'react-native';
    import PSPDFKitView from 'react-native-pspdfkit';
    import { NativeModules } from 'react-native';
    ...
    const PSPDFKit = NativeModules.PSPDFKit;
    PSPDFKit.setLicenseKey(null); // Or your valid license keys using `setLicenseKeys`.
    const DOCUMENT =
    Platform.OS === 'ios' ? 'Document.pdf' : 'file:///android_asset/Document.pdf';
    ...
    var pdfRef: React.RefObject<PSPDFKitView> = React.createRef();
    <PSPDFKitView
    document={DOCUMENT}
    configuration={{
    showThumbnailBar: 'scrollable',
    pageTransition: 'scrollContinuous',
    scrollDirection: 'vertical',
    }}
    ref={pdfRef}
    fragmentTag="PDF1"
    style={{flex: 1}}
    />
  6. The app is now ready to launch! Go back to the terminal app and run:

    Terminal window
    npx react-native run-android
    Terminal window
    npx react-native run-ios

Expo

If you’re using Expo, refer to the blog post that explains how to use Nutrient for React Native with Expo.

Next steps

To learn more about React Native, make sure to check out the following blog posts: