Getting started on Flutter

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

You can find our Flutter library on pub.dev(opens in a new tab) and GitHub(opens in a new tab).

Requirements

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

Creating a new project

If you already have an existing project that runs on Android, iOS, and web using the latest version of Flutter, skip to the Installation step. Otherwise, create a new one called pspdfkit_demo with the flutter CLI:

Terminal window
flutter create --org com.example.pspdfkit_demo pspdfkit_demo

Installing the Nutrient dependency

In the terminal app, change the location of the current working directory to your project and run the following command to add the Nutrient dependency to your project:

Terminal window
flutter pub add pspdfkit_flutter

Depending on the platform (Android, iOS, or Web) you want to run your project on, follow the setup instructions below.

Android setup

  1. Open the app’s Gradle build file, android/app/build.gradle:

    Terminal window
    open android/app/build.gradle
  2. Modify the compile SDK version and the minimum SDK version:

    android {
    compileSdkVersion flutter.compileSdkVersion
    compileSdkVersion 35
    ...
    defaultConfig {
    minSdkVersion flutter.minSdkVersion
    minSdkVersion 21
    ...
    }
    compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
    sourceCompatibility JavaVersion.VERSION_17
    targetCompatibility JavaVersion.VERSION_17
    }
    // If you have this block, update the `jvmTarget` to 17.
    kotlinOptions {
    jvmTarget = '1.8'
    jvmTarget = '17'
    }
    ...
    }
  3. Add the AppCompat AndroidX library to your android/app/build.gradle file:

    dependencies {
    ...
    implementation "androidx.appcompat:appcompat:<version>"
    }
  4. Change the base Activity to extend FlutterAppCompatActivity:

    import io.flutter.embedding.android.FlutterActivity;
    import io.flutter.embedding.android.FlutterAppCompatActivity;
    public class MainActivity extends FlutterActivity {
    public class MainActivity extends FlutterAppCompatActivity {
    }

    Alternatively, update the AndroidManifest.xml file to use FlutterAppCompatActivity as the launcher activity:

    <activity
    android:name=".MainActivity"
    android:name="io.flutter.embedding.android.FlutterAppCompatActivity"
    android:launchMode="singleTop"
    android:theme="@style/LaunchTheme"
    android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
    android:hardwareAccelerated="true"
    android:windowSoftInputMode="adjustResize"
    android:exported="true">

    FlutterAppCompatActivity isn’t an official part of the Flutter SDK. It’s a custom Activity that extends AppCompatActivity from the AndroidX AppCompat library, and it’s necessary to use Nutrient Android SDK with Flutter.

  5. Update the theme in android/app/src/main/res/values/styles.xml and android/app/src/main/res/values-night/styles.xml to use PSPDFKit.Theme.default as the parent:

    <style name="NormalTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <style name="NormalTheme" parent="PSPDFKit.Theme.Default">

    This is to customize the theme of the Nutrient UI. You can read more about this in the appearance styling guide.

iOS setup

  1. Open Runner.xcworkspace from the ios folder in Xcode:

    Terminal window
    open ios/Runner.xcworkspace
  2. Ensure the iOS deployment target is set to 16.0 or higher.

    iOS deployment target
  3. Open your project’s Podfile in a text editor:

    Terminal window
    open ios/Podfile
  4. Update the platform to iOS 16, which is the minimum required version for Nutrient iOS SDK:

    # platform :ios, '9.0'
    platform :ios, '16.0'

Web setup

The most direct and recommended way to get started is using our CDN to load Nutrient Web SDK:

  1. Add the following script to the <head> section of your index.html file:

    <!DOCTYPE html>
    <html>
    <head>
    <!-- ... other head elements ... -->
    <script src="https://cdn.cloud.pspdfkit.com/[email protected]/nutrient-viewer.js"></script>
    </head>
    <body>
    <!-- ... body content ... -->
    </body>
    </html>
  2. Make sure to replace <version> with the version you want to use. You can find the latest version in the Nutrient Web SDK changelog.

    Using the CDN option is recommended for both development and production, as it ensures you’re always using a specific, consistent version of the SDK.

Displaying a PDF

Integrating into an existing project consists of initializing the SDK and presenting it with a document.

  1. Import the PSPDFKit package:

    import 'package:pspdfkit_flutter/pspdfkit_widget.dart';
  2. Show the PDF document inside your Flutter app as demonstrated below:

    Scaffold(
    body: PspdfkitWidget(
    documentPath: 'file:///path/to/Document.pdf',
    ),
    );

If you’re having trouble with integration, go through the new project tab to see if there’s anything missing in your setup.

With your project now configured, proceed with the steps below to add a PDF document and run your application:

  1. Add the PDF document you want to display in your project’s assets directory. You can use this Quickstart Guide PDF as an example.

  2. Create a PDFs directory:

    Terminal window
    mkdir PDFs
  3. Copy the sample document into the newly created PDFs directory:

    Terminal window
    cp ~/Downloads/Document.pdf PDFs/Document.pdf
  4. Specify the assets directory in pubspec.yaml:

    # The following section is specific to Flutter.
    flutter:
    assets:
    - PDFs/
    ...
  5. Depending on your platform, Start your Android emulator(opens in a new tab) or iOS simulator(opens in a new tab), or connect a device. If Chrome is installed on your computer, it’ll be launched automatically.

  6. Run the app with:

    Terminal window
    flutter run

Next steps

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