On this page
Guides
Creating a New Project
Android
- Create a Flutter project called
example
with theflutter
CLI:
bash
flutter create --org com.compdfkit.flutter example
- In the terminal app, change the location of the current working directory to your project:
bash
cd example
- open
example/android/app/src/main/AndroidManifest.xml
, addInternet Permission
andStorage Permission
:
diff
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.compdfkit.flutter.example">
+ <uses-permission android:name="android.permission.INTERNET"/>
<!-- Required to read and write documents from device storage -->
+ <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
+ <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
+ <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
<application
+ android:requestLegacyExternalStorage="true">
</application>
</manifest>
- Open the app's Gradle build file,
android/app/build.gradle
:
bash
open android/app/build.gradle
- Modify the minimum SDK version, All this is done inside the
android
section:
diff
android {
defaultConfig {
- minSdkVersion flutter.minSdkVersion
+ minSdkVersion 21
...
}
}
- Add the ComPDFKit dependency in
pubspec.yaml
diff
dependencies:
flutter:
sdk: flutter
+ compdfkit_flutter: ^1.13.0
- From the terminal app, run the following command to get all the packages:
bash
flutter pub get
- Open
lib/main.dart
and replace the entire content with the following code. And fill in the license provided to you in theComPDFKit.init
method, this simple example will load a PDF document from the local device file system.
dart
import 'dart:io';
import 'package:compdfkit_flutter/compdfkit.dart';
import 'package:compdfkit_flutter/cpdf_configuration.dart';
import 'package:flutter/material.dart';
const String DOCUMENT_PATH = 'pdfs/PDF_Document.pdf';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
_init();
}
void _init() async {
/// Please replace it with your ComPDFKit license
ComPDFKit.initialize(androidOnlineLicense : 'your compdfkit key', iosOnlineLicense : 'your compdfkit key');
/// If you are using an offline certified license, please use init() method
/// ComPDFKit.init('your compdfkit key')
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SafeArea(
child: Center(
child: ElevatedButton(
onPressed: () async {
showDocument(context);
},
child: const Text(
'Open Document',
style: TextStyle(color: Colors.white),
)),
))),
);
}
void showDocument(BuildContext context) async {
final bytes = await DefaultAssetBundle.of(context).load(DOCUMENT_PATH);
final list = bytes.buffer.asUint8List();
final tempDir = await ComPDFKit.getTemporaryDirectory();
var pdfsDir = Directory('${tempDir.path}/pdfs');
pdfsDir.createSync(recursive: true);
final tempDocumentPath = '${tempDir.path}/$DOCUMENT_PATH';
final file = File(tempDocumentPath);
if (!file.existsSync()) {
file.create(recursive: true);
file.writeAsBytesSync(list);
}
var configuration = CPDFConfiguration();
// How to disable functionality:
// setting the default display mode when opening
// configuration.modeConfig = const ModeConfig(initialViewMode: CPreviewMode.annotations);
// top toolbar configuration:
// android:
// configuration.toolbarConfig = const ToolbarConfig(androidAvailableActions: [
// ToolbarAction.thumbnail, ToolbarAction.bota,
// ToolbarAction.search, ToolbarAction.menu
// ],
// availableMenus: [
// ToolbarMenuAction.viewSettings, ToolbarMenuAction.documentInfo, ToolbarMenuAction.security,
// ]);
// iOS:
// configuration.toolbarConfig = const ToolbarConfig(
// // ios top toolbar left buttons
// iosLeftBarAvailableActions: [
// ToolbarAction.back, ToolbarAction.thumbnail
// ],
// // ios top toolbar right buttons
// iosRightBarAvailableActions: [
// ToolbarAction.bota, ToolbarAction.search, ToolbarAction.menu
// ],
// availableMenus: [
// ToolbarMenuAction.viewSettings, ToolbarMenuAction.documentInfo, ToolbarMenuAction.security,
// ]);
// readerview configuration
// configuration.readerViewConfig = const ReaderViewConfig(linkHighlight: true, formFieldHighlight: true);
ComPDFKit.openDocument(tempDocumentPath,
password: '', configuration: configuration);
}
}
- Add the PDF documents you want to display in the project
create a
pdf
directorybashmkdir pdfs
Copy your example document into the newly created
pdfs
directory and name itPDF_Document.pdf
- Specify the
assets
directory inpubspec.yaml
diff
flutter:
+ assets:
+ - pdfs/
- Start your Android emulator, or connect a device.
- Run the app with:
bash
flutter run
iOS
- Create a Flutter project called
example
with theflutter
CLI:
bash
flutter create --org com.compdfkit.flutter example
- In the terminal app, change the location of the current working directory to your project:
bash
cd example
- Add the ComPDFKit dependency in
pubspec.yaml
diff
dependencies:
flutter:
sdk: flutter
+ compdfkit_flutter: ^1.13.0
- From the terminal app, run the following command to get all the packages:
bash
flutter pub get
- Open your project's Podfile in a text editor:
bash
open ios/Podfile
Note: If SSL network requests fail to download the ComPDFKit
library when you run pod install
, you can see the processing method in Troubleshooting.
- Update the platform to iOS 11 and add the ComPDFKit Podspec:
diff
- platform :ios, '9.0'
+ platform :ios, '11.0'
...
target 'Runner' do
use_frameworks!
use_modular_headers!`
flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
+ pod 'ComPDFKit_Tools', podspec:'https://www.compdf.com/download/ios/cocoapods/xcframeworks/compdfkit_tools/1.13.0.podspec'
+ pod 'ComPDFKit', podspec:'https://www.compdf.com/download/ios/cocoapods/xcframeworks/compdfkit/1.13.0.podspec'
end
- Go to the
example/ios
folder and run thepod install
command:
bash
pod install
- Open
lib/main.dart
and replace the entire content with the following code. And fill in the license provided to you in theComPDFKit.init
method, this simple example will load a PDF document from the local device file system.
dart
import 'dart:io';
import 'package:compdfkit_flutter/compdfkit.dart';
import 'package:compdfkit_flutter/cpdf_configuration.dart';
import 'package:flutter/material.dart';
const String DOCUMENT_PATH = 'pdfs/PDF_Document.pdf';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
_init();
}
void _init() async {
/// Please replace it with your ComPDFKit license
ComPDFKit.initialize(androidOnlineLicense : 'your compdfkit key', iosOnlineLicense : 'your compdfkit key');
/// If you are using an offline certified license, please use init() method
/// ComPDFKit.init('your compdfkit key')
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SafeArea(
child: Center(
child: ElevatedButton(
onPressed: () async {
showDocument(context);
},
child: const Text(
'Open Document',
style: TextStyle(color: Colors.white),
)),
))),
);
}
void showDocument(BuildContext context) async {
final bytes = await DefaultAssetBundle.of(context).load(DOCUMENT_PATH);
final list = bytes.buffer.asUint8List();
final tempDir = await ComPDFKit.getTemporaryDirectory();
var pdfsDir = Directory('${tempDir.path}/pdfs');
pdfsDir.createSync(recursive: true);
final tempDocumentPath = '${tempDir.path}/$DOCUMENT_PATH';
final file = File(tempDocumentPath);
if (!file.existsSync()) {
file.create(recursive: true);
file.writeAsBytesSync(list);
}
var configuration = CPDFConfiguration();
// How to disable functionality:
// setting the default display mode when opening
// configuration.modeConfig = const ModeConfig(initialViewMode: CPreviewMode.annotations);
// top toolbar configuration:
// android:
// configuration.toolbarConfig = const ToolbarConfig(androidAvailableActions: [
// ToolbarAction.thumbnail, ToolbarAction.bota,
// ToolbarAction.search, ToolbarAction.menu
// ],
// availableMenus: [
// ToolbarMenuAction.viewSettings, ToolbarMenuAction.documentInfo, ToolbarMenuAction.security,
// ]);
// iOS:
// configuration.toolbarConfig = const ToolbarConfig(iosLeftBarAvailableActions: [
// ToolbarAction.back, ToolbarAction.thumbnail
// ],
// iosRightBarAvailableActions: [
// ToolbarAction.bota, ToolbarAction.search, ToolbarAction.menu
// ],
// availableMenus: [
// ToolbarMenuAction.viewSettings, ToolbarMenuAction.documentInfo, ToolbarMenuAction.security,
// ]);
// readerview configuration:
// configuration.readerViewConfig = const ReaderViewConfig(linkHighlight: true, formFieldHighlight: true);
ComPDFKit.openDocument(tempDocumentPath,
password: '', configuration: configuration);
}
}
- Add the PDF documents you want to display in the project
create a
pdf
directorybashmkdir pdfs
Copy your example document into the newly created
pdfs
directory and name itPDF_Document.pdf
- Specify the
assets
directory inpubspec.yaml
diff
flutter:
+ assets:
+ - pdfs/
- To protect user privacy,
To protect user privacy, before accessing the sensitive privacy data, you need to find the "*Info*" configuration in your iOS 10.0 or higher iOS project and configure the relevant privacy terms as shown in the following picture.
objective-c
<key>NSCameraUsageDescription</key>
<string>Your consent is required before you could access the function.</string>
<key>NSMicrophoneUsageDescription</key>
<string>Your consent is required before you could access the function.</string>
<key>NSPhotoLibraryAddUsageDescription</key>
<string>Your consent is required before you could access the function.</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>Your consent is required before you could access the function.</string>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
- Start your Android emulator, or connect a device.
bash
flutter emulators --launch apple_ios_simulator
- Run the app with:
bash
flutter run