A convenience wrapper for building Flutter apps with PDFTron mobile SDK
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 |
---|---|
Installation
The complete installation and API guides can be found at https://www.pdftron.com/documentation/android/guides/flutter
Android
-
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
. -
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
-
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" + } ... }
-
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" /> ...
-
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
-
Replace
lib/main.dart
with what is shown here -
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. -
Run the app with the command
flutter run
.
iOS
-
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
-
Open
myapp
folder in a text editor. Then openmyapp/pubspec.yaml
file, add:dependencies: flutter: sdk: flutter + pdftron_flutter: + git: + url: git://github.com/PDFTron/pdftron-flutter.git + permission: 0.1.0
-
Run
flutter packages get
-
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
-
Run
flutter build ios --no-codesign
to ensure integration process is sucessful -
Replace
lib/main.dart
with what is shown here -
Run
flutter emulators --launch apple_ios_simulator
-
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