Skip to content

Commit 70ba7bd

Browse files
Add fastInit option in document initialization
Using this option it can be decided whether to initialize the text stripper engine on Android immediately or at the first text read.
1 parent 48af65c commit 70ba7bd

File tree

2 files changed

+38
-15
lines changed

2 files changed

+38
-15
lines changed

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 {

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.

0 commit comments

Comments
 (0)