Some test text!

Flutter

In this document
chevron_rightSystem requirements
chevron_rightPreview
chevron_rightInstallation
chevron_rightAndroid
chevron_rightiOS
chevron_rightUsage
chevron_rightAPI
chevron_rightPdftronFlutter.version
chevron_rightPdftronFlutter.initialize
chevron_rightPdftronFlutter.openDocument
An evaluation or commercial license key is required to run PDFTron. If you are evaluating the SDK and do not have a key, please obtain one, and then visit this page via the link included in the trial email. If your company has licensed PDFTron then you already received your key in an email from team@pdftron.com. See sign in for more details.

Flutter allows you to build beautiful native apps on iOS and Android from a single codebase. Flutter reliably allows hot reloading in just a few seconds which greatly increases the iteration speed compared to native applications. This tutorial will walk you through how to add PDFTron SDK into your Flutter app.

Currently the PDFTron Flutter wrapper exposes DocumentActivity on Android and PTTabbedDocumentViewController on iOS. It is a all-in-one document reader and PDF editor. The behavior can be further customized by modifying native components.

linkSystem requirements

  • PDFTron SDK >= 6.9.0
  • Flutter >= 1.0.0

linkPreview

AndroidiOS

linkInstallation

linkAndroid

  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
  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 credentials in 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.

linkiOS

  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
  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

linkUsage

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

Replace your_pdftron_license_key string with your license key

import 'package:flutter/material.dart';
import 'dart:async';

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

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

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

class _MyAppState extends State<MyApp> {
  String _version = 'Unknown';

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

    PdftronFlutter.openDocument("https://pdftron.s3.amazonaws.com/downloads/pdfref.pdf");
  }

  // 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;
    });
  }

  @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'),
        ),
      ),
    );
  }
}

linkAPI

linkPdftronFlutter.version()

Obtain PDFTron SDK version.

Returns: String

linkPdftronFlutter.initialize(key)

Initializes PDFTron SDK.

linkParams

linkkey

Your PDFTron license key

TypeRequiredDefault
Stringtrue

Returns: N/A

linkPdftronFlutter.openDocument(path)

Opens a document in the viewer.

linkParams

linkpath

Path to the document

TypeRequiredDefault
Stringtrue

Returns: N/A