PDFTron Flutter Wrapper

  • A valid evaluation or commercial license key. If you do not have a license key, please contact sales for a commercial license key or click here to get an evaluation key.
  • PDFTron gradle credentials that comes with your license key (Android)
  • PDFTron SDK >= 6.9.0
  • Flutter >= 1.0.0

Preview

Android iOS
flutter-pdftron-demo-android flutter-pdftron-demo-ios

Installation

The complete installation and API guides can be found at https://www.pdftron.com/documentation/android/guides/flutter

Android

  1. First follow the Flutter getting started guides to install, set up an editor, and create a Flutter Project. The rest of this guide assumes your project is created by running flutter create myapp.

  2. Add the following dependency to your Flutter project in myapp/pubspec.yaml:

    dependencies:
       flutter:
         sdk: flutter
    +  pdftron_flutter:
    +    git:
    +      url: git://github.com/PDFTron/pdftron-flutter.git
    +  permission: 0.1.0
    
    
  3. Now add the following items in your myapp/android/app/build.gradle file:

    android {
    -   compileSdkVersion 27
    +   compileSdkVersion 28
    
        lintOptions {
    	disable 'InvalidPackage'
        }
    
        defaultConfig {
    	applicationId "com.example.myapp"
    -       minSdkVersion 16
    +       minSdkVersion 21
    -       targetSdkVersion 27
    +       targetSdkVersion 28
    +       multiDexEnabled true
    	versionCode flutterVersionCode.toInteger()
    	versionName flutterVersionName
    	testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
    
    +   configurations.all {
    +       resolutionStrategy.force "com.android.support:appcompat-v7:28.0.0"
    +       resolutionStrategy.force "com.android.support:support-v4:28.0.0"
    +       resolutionStrategy.force "android.arch.lifecycle:runtime:1.0.3"
    +   }
    	...
    }
    
  4. In your myapp\android\app\src\main\AndroidManifest.xml file, add the following lines to the <application> tag:

    ...
    <application
    	android:name="io.flutter.app.FlutterApplication"
    	android:label="myapp"
    	android:icon="@mipmap/ic_launcher"
    +	android:largeHeap="true"
    +	android:usesCleartextTraffic="true">
      ...
    

    Additionally, add the required permissions for your app in the <manifest> tag:

    	...
    	<uses-permission android:name="android.permission.INTERNET" />
    	<!-- Required to read and write documents from device storage -->
    +	<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    	<!-- Required if you want to record audio annotations -->
    +	<uses-permission android:name="android.permission.RECORD_AUDIO" />
    	...
    
  5. Add your PDFTron credentials in to the myapp/android/gradle.properties file.

    org.gradle.jvmargs=-Xmx1536M
    
    AWS_ACCESS_KEY=YOUR_ACCESS_KEY_GOES_HERE
    AWS_SECRET_KEY=YOUR_SECRET_KEY_GOES_HERE
    
  6. Replace lib/main.dart with what is shown here

  7. Check that your Android device is running by running the command flutter devices. If none are available, follow the device set up instructions in the Install guides for your platform.

  8. Run the app with the command flutter run.

iOS

  1. First, follow the official getting started guide on installation, setting up an editor, and create a Flutter project, the following steps will assume your app is created through flutter create myapp

  2. Open myapp folder in a text editor. Then open myapp/pubspec.yaml file, add:

    dependencies:
       flutter:
         sdk: flutter
    +  pdftron_flutter:
    +    git:
    +      url: git://github.com/PDFTron/pdftron-flutter.git
    +  permission: 0.1.0
    
  3. Run flutter packages get

  4. Open myapp/ios/Podfile, add:

     # Uncomment this line to define a global platform for your project
    -# platform :ios, '9.0'
    +platform :ios, '9.3'
    ...
     target 'Runner' do
       ...
    +  # PDFTron Pods
    +  use_frameworks!
    +  pod 'PDFNet', podspec: 'POD_LINK_GOES_HERE'
     end
    
  5. Run flutter build ios --no-codesign to ensure integration process is sucessful

  6. Replace lib/main.dart with what is shown here

  7. Run flutter emulators --launch apple_ios_simulator

  8. Run flutter run

Usage

Open lib/main.dart, replace the entire file with the following:

Replace your_pdftron_license_key string with your license key

import 'dart:async';
import 'dart:io' show Platform;

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:pdftron_flutter/pdftron_flutter.dart';
import 'package:permission/permission.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _version = 'Unknown';
  String _document = "https://pdftron.s3.amazonaws.com/downloads/pdfref.pdf";

  @override
  void initState() {
    super.initState();
    initPlatformState();

    if (Platform.isIOS) {
      // Open the document for iOS, no need for permission
      PdftronFlutter.openDocument(_document);
    } else {
      // Request for permissions for android before opening document
      requestPermission();
    }
  }

  Future<void> requestPermission() async {
    final res = await Permission.requestSinglePermission(PermissionName.Storage);
    if (granted(res)) {
      PdftronFlutter.openDocument(_document);
    }
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String version;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      PdftronFlutter.initialize("your_pdftron_license_key");
      version = await PdftronFlutter.version;
    } on PlatformException {
      version = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _version = version;
    });
  }

  bool granted(PermissionStatus status) {
    return status == PermissionStatus.allow;
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('PDFTron flutter app'),
        ),
        body: Center(
          child: Text('Running on: $_version\n'),
        ),
      ),
    );
  }
}

APIs

  • PdftronFlutter.version

Obtain PDFTron SDK version

  • PdftronFlutter.initialize(String)

Initializes PDFTron SDK

  • PdftronFlutter.openDocument(String)

Opens a document in the viewer

GitHub

https://github.com/PDFTron/pdftron-flutter