Skip to content

Commit 3aea262

Browse files
Merge pull request #12 from AlessioLuciani/edit/doc-init
Add fastInit option in document initialization
2 parents 48af65c + 381776d commit 3aea262

File tree

6 files changed

+54
-21
lines changed

6 files changed

+54
-21
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.3.1
2+
3+
* The possibility to initialize a document faster (without immediately initializing the text stripper engine) on Android has been added.
4+
15
## 0.3.0
26

37
* A class for the PDF document information has been added. Now this information

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Add this to your package's `pubspec.yaml` file:
1818

1919
```yaml
2020
dependencies:
21-
pdf_text: ^0.3.0
21+
pdf_text: ^0.3.1
2222
```
2323
2424
## Usage
@@ -53,6 +53,12 @@ Pass a password for encrypted PDF documents:
5353
PDFDoc doc = await PDFDoc.fromFile(file, password: password);
5454
```
5555

56+
Use faster initialization on Android:
57+
58+
```dart
59+
PDFDoc doc = await PDFDoc.fromFile(file, fastInit: true);
60+
```
61+
5662
*Read the text of the entire document:*
5763

5864
```dart
@@ -113,9 +119,9 @@ allows you not to waste time loading text that you will probably not use. When y
113119
| Return | Description |
114120
|---|---|
115121
| PDFPage | **pageAt(int pageNumber)** <br> Gets the page of the document at the given page number. |
116-
| static Future\<PDFDoc> | **fromFile(File file, {String password = ""})** <br> Creates a PDFDoc object with a File instance. Optionally, takes a password for encrypted PDF documents.|
117-
| static Future\<PDFDoc> | **fromPath(String path, {String password = ""})** <br> Creates a PDFDoc object with a file path. Optionally, takes a password for encrypted PDF documents.|
118-
| static Future\<PDFDoc> | **fromURL(String url, {String password = ""})** <br> Creates a PDFDoc object with a url. Optionally, takes a password for encrypted PDF documents. It downloads the PDF file located in the given URL and saves it in the app's temporary directory. |
122+
| static Future\<PDFDoc> | **fromFile(File file, {String password = "", bool fastInit = false})** <br> Creates a PDFDoc object with a File instance. Optionally, takes a password for encrypted PDF documents. If fastInit is true, the initialization of the document will be faster on Android. In that case, the text stripper engine will not be initialized with this call, but later when some text is read. This means that the first text read will take some time but the document data can be accessed immediately.|
123+
| static Future\<PDFDoc> | **fromPath(String path, {String password = "", bool fastInit = false})** <br> Creates a PDFDoc object with a file path. Optionally, takes a password for encrypted PDF documents. If fastInit is true, the initialization of the document will be faster on Android. In that case, the text stripper engine will not be initialized with this call, but later when some text is read. This means that the first text read will take some time but the document data can be accessed immediately.|
124+
| static Future\<PDFDoc> | **fromURL(String url, {String password = "", bool fastInit = false})** <br> Creates a PDFDoc object with a url. Optionally, takes a password for encrypted PDF documents. If fastInit is true, the initialization of the document will be faster on Android. In that case, the text stripper engine will not be initialized with this call, but later when some text is read. This means that the first text read will take some time but the document data can be accessed immediately. It downloads the PDF file located in the given URL and saves it in the app's temporary directory. |
119125
| void | **deleteFile()** <br> Deletes the file related to this PDFDoc.<br>Throws an exception if the FileSystemEntity cannot be deleted. |
120126
| static Future | **deleteAllExternalFiles()** <br> Deletes all the files of the documents that have been imported from outside the local file system (e.g. using fromURL). |
121127

android/src/main/kotlin/dev/aluc/pdf_text/PdfTextPlugin.kt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ public class PdfTextPlugin: FlutterPlugin, MethodCallHandler {
6363
val args = call.arguments as Map<String, Any>
6464
val path = args["path"] as String
6565
val password = args["password"] as String
66-
initDoc(result, path, password)
66+
val fastInit = args["fastInit"] as Boolean
67+
initDoc(result, path, password, fastInit)
6768
}
6869
"getDocPageText" -> {
6970
val args = call.arguments as Map<String, Any>
@@ -93,8 +94,8 @@ public class PdfTextPlugin: FlutterPlugin, MethodCallHandler {
9394
/**
9495
Initializes the PDF document and returns some information into the channel.
9596
*/
96-
private fun initDoc(result: Result, path: String, password: String) {
97-
val doc = getDoc(result, path, password) ?: return
97+
private fun initDoc(result: Result, path: String, password: String, fastInit: Boolean) {
98+
val doc = getDoc(result, path, password, !fastInit) ?: return
9899
// Getting the length of the PDF document in pages.
99100
val length = doc.numberOfPages
100101

@@ -173,8 +174,10 @@ public class PdfTextPlugin: FlutterPlugin, MethodCallHandler {
173174

174175
/**
175176
Gets a PDF document, given its path.
177+
Initializes the text stripper engine if initTextStripper is true.
176178
*/
177-
private fun getDoc(result: Result, path: String, password: String = ""): PDDocument? {
179+
private fun getDoc(result: Result, path: String, password: String = "",
180+
initTextStripper: Boolean = true): PDDocument? {
178181
// Checking for cached document
179182
if (cachedDoc != null && cachedDocPath == path) {
180183
return cachedDoc
@@ -183,7 +186,9 @@ public class PdfTextPlugin: FlutterPlugin, MethodCallHandler {
183186
val doc = PDDocument.load(File(path), password)
184187
cachedDoc = doc
185188
cachedDocPath = path
186-
initTextStripperEngine(doc)
189+
if (initTextStripper) {
190+
initTextStripperEngine(doc)
191+
}
187192
doc
188193
} catch (e: Exception) {
189194
Handler(Looper.getMainLooper()).post {

example/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ packages:
143143
path: ".."
144144
relative: true
145145
source: path
146-
version: "0.3.0"
146+
version: "0.3.1"
147147
pedantic:
148148
dependency: transitive
149149
description:

lib/pdf_text.dart

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,20 @@ class PDFDoc {
1919
PDFDoc._internal();
2020

2121
/// Creates a [PDFDoc] object with a [File] instance.
22-
/// Optionally, takes a password for encrypted PDF documents.
23-
static Future<PDFDoc> fromFile(File file, {String password = ""}) async {
22+
/// Optionally, takes a [password] for encrypted PDF documents.
23+
/// If [fastInit] is true, the initialization of the document will
24+
/// be faster on Android. In that case, the text stripper engine
25+
/// will not be initialized with this call, but later when some text
26+
/// is read. This means that the first text read will take some time
27+
/// but the document data can be accessed immediately.
28+
static Future<PDFDoc> fromFile(File file,
29+
{String password = "", bool fastInit = false}) async {
2430
var doc = PDFDoc._internal();
2531
doc._file = file;
2632
Map data;
2733
try {
28-
data = await _CHANNEL
29-
.invokeMethod('initDoc', {"path": file.path, "password": password});
34+
data = await _CHANNEL.invokeMethod('initDoc',
35+
{"path": file.path, "password": password, "fastInit": fastInit});
3036
} on Exception catch (e) {
3137
return Future.error(e);
3238
}
@@ -39,16 +45,28 @@ class PDFDoc {
3945
}
4046

4147
/// Creates a [PDFDoc] object with a file path.
42-
/// Optionally, takes a password for encrypted PDF documents.
43-
static Future<PDFDoc> fromPath(String path, {String password = ""}) async {
44-
return await fromFile(File(path), password: password);
48+
/// Optionally, takes a [password] for encrypted PDF documents.
49+
/// If [fastInit] is true, the initialization of the document will
50+
/// be faster on Android. In that case, the text stripper engine
51+
/// will not be initialized with this call, but later when some text
52+
/// is read. This means that the first text read will take some time
53+
/// but the document data can be accessed immediately.
54+
static Future<PDFDoc> fromPath(String path,
55+
{String password = "", bool fastInit = false}) async {
56+
return await fromFile(File(path), password: password, fastInit: fastInit);
4557
}
4658

4759
/// Creates a [PDFDoc] object with a URL.
48-
/// Optionally, takes a password for encrypted PDF documents.
60+
/// Optionally, takes a [password] for encrypted PDF documents.
61+
/// If [fastInit] is true, the initialization of the document will
62+
/// be faster on Android. In that case, the text stripper engine
63+
/// will not be initialized with this call, but later when some text
64+
/// is read. This means that the first text read will take some time
65+
/// but the document data can be accessed immediately.
4966
/// It downloads the PDF file located in the given URL and saves it
5067
/// in the app's temporary directory.
51-
static Future<PDFDoc> fromURL(String url, {String password = ""}) async {
68+
static Future<PDFDoc> fromURL(String url,
69+
{String password = "", bool fastInit = false}) async {
5270
File file;
5371
try {
5472
String tempDirPath = (await getTemporaryDirectory()).path;
@@ -62,7 +80,7 @@ class PDFDoc {
6280
} on Exception catch (e) {
6381
return Future.error(e);
6482
}
65-
return await fromFile(file, password: password);
83+
return await fromFile(file, password: password, fastInit: fastInit);
6684
}
6785

6886
/// Gets the page of the document at the given page number.

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: pdf_text
22
description: This plugin for Flutter allows you to read the text content of PDF documents and convert it into strings. It works on iOS and Android.
3-
version: 0.3.0
3+
version: 0.3.1
44
homepage: https://github.com/AlessioLuciani/flutter-pdf-text
55

66
environment:

0 commit comments

Comments
 (0)