Skip to content

Commit

Permalink
Add fromURL, deleteFile and deleteAllExternalFiles methods to PDFDoc
Browse files Browse the repository at this point in the history
  • Loading branch information
AlessioLuciani committed May 8, 2020
1 parent 2618df1 commit ccf561d
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 13 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 0.2.0

* Some new methods have been added to PDFDoc:
* fromURL to load a PDF document from an URL.
* deleteFile to delete the file related to the document.
* deleteAllExternalFiles to delete all the files of the document imported
from outside the local file system.

## 0.1.3

* Part of the internal logic has been simplified.
Expand Down
33 changes: 32 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Add this to your package's `pubspec.yaml` file:

```yaml
dependencies:
pdf_text: ^0.1.3
pdf_text: ^0.2.0
```
## Usage
Expand All @@ -41,6 +41,12 @@ or using a path string:
PDFDoc doc = await PDFDoc.fromPath(path);
```

or using a URL string:

```dart
PDFDoc doc = await PDFDoc.fromURL(url);
```

Read the text of the entire document:

```dart
Expand All @@ -65,6 +71,24 @@ Read the text of a page of the document:
String pageText = await page.text;
```

Optionally, you can delete the file of a document when you no longer need it.
This can be useful when you import a PDF document from outside the local
file system (e.g using a URL), since it is automatically stored in the temporary
directory of the app.

Delete the file of a single document:

```dart
doc.deleteFile();
```

or delete all the files of all the documents imported from outside the local
file system:

```dart
PDFDoc.deleteAllExternalFiles();
```

## Functioning

This plugin applies lazy loading for the text contents of the pages. The text is cached page per page. When you request the text of a page for the first time, it is parsed and stored in memory, so that the second access will be faster. Anyway, the text of pages that are not requested is not loaded. This mechanism
Expand All @@ -82,6 +106,13 @@ allows you not to waste time loading text that you will probably not use. When y

| static Future\<PDFDoc> | **fromPath(String path)** <br> Creates a PDFDoc object with a file path. |

| static Future\<PDFDoc> | **fromURL(String url)** <br> Creates a PDFDoc object with a url. It downloads the PDF file located in the given URL and saves it in the app's temporary directory. |

| void | **deleteFile()** <br> Deletes the file related to this PDFDoc.
Throws an exception if the FileSystemEntity cannot be deleted. |

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

## Objects

```dart
Expand Down
12 changes: 12 additions & 0 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ PODS:
- Flutter (1.0.0)
- flutter_plugin_android_lifecycle (0.0.1):
- Flutter
- path_provider (0.0.1):
- Flutter
- path_provider_macos (0.0.1):
- Flutter
- pdf_text (0.0.1):
- Flutter

DEPENDENCIES:
- file_picker (from `.symlinks/plugins/file_picker/ios`)
- Flutter (from `Flutter`)
- flutter_plugin_android_lifecycle (from `.symlinks/plugins/flutter_plugin_android_lifecycle/ios`)
- path_provider (from `.symlinks/plugins/path_provider/ios`)
- path_provider_macos (from `.symlinks/plugins/path_provider_macos/ios`)
- pdf_text (from `.symlinks/plugins/pdf_text/ios`)

EXTERNAL SOURCES:
Expand All @@ -20,13 +26,19 @@ EXTERNAL SOURCES:
:path: Flutter
flutter_plugin_android_lifecycle:
:path: ".symlinks/plugins/flutter_plugin_android_lifecycle/ios"
path_provider:
:path: ".symlinks/plugins/path_provider/ios"
path_provider_macos:
:path: ".symlinks/plugins/path_provider_macos/ios"
pdf_text:
:path: ".symlinks/plugins/pdf_text/ios"

SPEC CHECKSUMS:
file_picker: 408623be2125b79a4539cf703be3d4b3abe5e245
Flutter: 0e3d915762c693b495b44d77113d4970485de6ec
flutter_plugin_android_lifecycle: 47de533a02850f070f5696a623995e93eddcdb9b
path_provider: abfe2b5c733d04e238b0d8691db0cfd63a27a93c
path_provider_macos: f760a3c5b04357c380e2fddb6f9db6f3015897e0
pdf_text: 850e21d1a62672674099a07818c95a3f55030c81

PODFILE CHECKSUM: 1b66dae606f75376c5f2135a8290850eeb09ae83
Expand Down
60 changes: 58 additions & 2 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,20 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
http:
dependency: transitive
description:
name: http
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.1"
http_parser:
dependency: transitive
description:
name: http_parser
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.4"
image:
dependency: transitive
description:
Expand Down Expand Up @@ -116,20 +130,62 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.6.4"
path_provider:
dependency: transitive
description:
name: path_provider
url: "https://pub.dartlang.org"
source: hosted
version: "1.6.7"
path_provider_macos:
dependency: transitive
description:
name: path_provider_macos
url: "https://pub.dartlang.org"
source: hosted
version: "0.0.4+2"
path_provider_platform_interface:
dependency: transitive
description:
name: path_provider_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.1"
pdf_text:
dependency: "direct dev"
description:
path: ".."
relative: true
source: path
version: "0.1.2"
version: "0.2.0"
pedantic:
dependency: transitive
description:
name: pedantic
url: "https://pub.dartlang.org"
source: hosted
version: "1.9.0"
petitparser:
dependency: transitive
description:
name: petitparser
url: "https://pub.dartlang.org"
source: hosted
version: "2.4.0"
platform:
dependency: transitive
description:
name: platform
url: "https://pub.dartlang.org"
source: hosted
version: "2.2.1"
plugin_platform_interface:
dependency: transitive
description:
name: plugin_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.2"
quiver:
dependency: transitive
description:
Expand Down Expand Up @@ -207,4 +263,4 @@ packages:
version: "3.6.1"
sdks:
dart: ">=2.6.0 <3.0.0"
flutter: ">=1.12.13 <2.0.0"
flutter: ">=1.12.13+hotfix.5 <2.0.0"
54 changes: 46 additions & 8 deletions lib/pdf_text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ import 'dart:async';
import 'dart:io';

import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';
import 'package:http/http.dart' as http;


const MethodChannel _channel = const MethodChannel('pdf_text');
const MethodChannel _CHANNEL = const MethodChannel('pdf_text');
const String _TEMP_DIR_NAME = "/.flutter_pdf_text/";

/// Class representing a PDF document.
/// In order to create a new [PDFDoc] instance, one of these two static methods has
/// to be used: [PDFDoc.fromFile], [PDFDoc.fromPath].
class PDFDoc {


File _file;
List<PDFPage> _pages;

Expand All @@ -23,7 +24,7 @@ class PDFDoc {
doc._file = file;
int length;
try {
length = await _channel.invokeMethod('getDocLength', {"path": file.path});
length = await _CHANNEL.invokeMethod('getDocLength', {"path": file.path});
} on Exception catch (e) {
return Future.error(e);
}
Expand All @@ -39,7 +40,23 @@ class PDFDoc {
return await fromFile(File(path));
}


/// Creates a [PDFDoc] object with a URL.
/// It downloads the PDF file located in the given URL and saves it
/// in the app's temporary directory.
static Future<PDFDoc> fromURL(String url) async {
File file;
try {
String tempDirPath = (await getTemporaryDirectory()).path;
String filePath = tempDirPath + _TEMP_DIR_NAME
+ url.split("/").last.split(".").first + ".pdf";
file = File(filePath);
file.createSync(recursive: true);
file.writeAsBytesSync((await http.get(url)).bodyBytes);
} on Exception catch (e) {
return Future.error(e);
}
return await fromFile(file);
}

/// Gets the page of the document at the given page number.
PDFPage pageAt(int pageNumber) => _pages[pageNumber - 1];
Expand All @@ -66,7 +83,7 @@ class PDFDoc {
});
List<String> missingPagesTexts;
try {
missingPagesTexts = List<String>.from(await _channel.invokeMethod('getDocText', {"path": _file.path,
missingPagesTexts = List<String>.from(await _CHANNEL.invokeMethod('getDocText', {"path": _file.path,
"missingPagesNumbers": missingPagesNumbers}));
} on Exception catch (e) {
return Future.error(e);
Expand All @@ -82,9 +99,30 @@ class PDFDoc {
return text;
}

/// Deletes the file related to this [PDFDoc].
/// Throws an exception if the [FileSystemEntity] cannot be deleted.
void deleteFile() {
if (_file.existsSync()) {
_file.deleteSync();
}
}

}
/// Deletes all the files of the documents that have been imported
/// from outside the local file system (e.g. using [fromURL]).
static Future deleteAllExternalFiles() async {
try {
String tempDirPath = (await getTemporaryDirectory()).path;
String dirPath = tempDirPath + _TEMP_DIR_NAME;
Directory dir = Directory(dirPath);
if (dir.existsSync()) {
dir.deleteSync(recursive: true);
}
} catch (e) {
return Future.error(e);
}
}

}

/// Class representing a PDF document page.
/// It needs not to be directly instantiated, instances will be automatically
Expand All @@ -107,7 +145,7 @@ class PDFPage {
// Loading the text
if (_text == null) {
try {
_text = await _channel.invokeMethod('getDocPageText', {"path": _parentDoc._file.path,
_text = await _CHANNEL.invokeMethod('getDocPageText', {"path": _parentDoc._file.path,
"number": number});
} on Exception catch (e) {
return Future.error(e);
Expand Down
58 changes: 57 additions & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
http:
dependency: "direct main"
description:
name: http
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.1"
http_parser:
dependency: transitive
description:
name: http_parser
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.4"
image:
dependency: transitive
description:
Expand Down Expand Up @@ -95,13 +109,55 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.6.4"
path_provider:
dependency: "direct main"
description:
name: path_provider
url: "https://pub.dartlang.org"
source: hosted
version: "1.6.7"
path_provider_macos:
dependency: transitive
description:
name: path_provider_macos
url: "https://pub.dartlang.org"
source: hosted
version: "0.0.4+2"
path_provider_platform_interface:
dependency: transitive
description:
name: path_provider_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.1"
pedantic:
dependency: transitive
description:
name: pedantic
url: "https://pub.dartlang.org"
source: hosted
version: "1.9.0"
petitparser:
dependency: transitive
description:
name: petitparser
url: "https://pub.dartlang.org"
source: hosted
version: "2.4.0"
platform:
dependency: transitive
description:
name: platform
url: "https://pub.dartlang.org"
source: hosted
version: "2.2.1"
plugin_platform_interface:
dependency: transitive
description:
name: plugin_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.2"
quiver:
dependency: transitive
description:
Expand Down Expand Up @@ -179,4 +235,4 @@ packages:
version: "3.6.1"
sdks:
dart: ">=2.6.0 <3.0.0"
flutter: ">=1.12.0"
flutter: ">=1.12.13+hotfix.5 <2.0.0"
Loading

0 comments on commit ccf561d

Please sign in to comment.