Skip to content

Commit 347d983

Browse files
Add a PDF document information class and the retrieval of this information
1 parent dd6e673 commit 347d983

File tree

9 files changed

+224
-120
lines changed

9 files changed

+224
-120
lines changed

.idea/workspace.xml

Lines changed: 29 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ public class PdfTextPlugin: FlutterPlugin, MethodCallHandler {
5959
override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
6060
thread (start = true) {
6161
when (call.method) {
62-
"getDocLength" -> {
62+
"initDoc" -> {
6363
val args = call.arguments as Map<String, Any>
6464
val path = args["path"] as String
6565
val password = args["password"] as String
66-
getDocLength(result, path, password)
66+
initDoc(result, path, password)
6767
}
6868
"getDocPageText" -> {
6969
val args = call.arguments as Map<String, Any>
@@ -91,14 +91,54 @@ public class PdfTextPlugin: FlutterPlugin, MethodCallHandler {
9191
}
9292

9393
/**
94-
Gets the length of the PDF document in pages.
94+
Initializes the PDF document and returns some information into the channel.
9595
*/
96-
private fun getDocLength(result: Result, path: String, password: String) {
96+
private fun initDoc(result: Result, path: String, password: String) {
9797
val doc = getDoc(result, path, password) ?: return
98+
// Getting the length of the PDF document in pages.
9899
val length = doc.numberOfPages
100+
101+
val info = doc.documentInformation
102+
103+
var creationDate: String? = null
104+
if (info.creationDate != null) {
105+
creationDate = info.creationDate.time.toString()
106+
}
107+
var modificationDate: String? = null
108+
if (info.modificationDate != null) {
109+
modificationDate = info.modificationDate.time.toString()
110+
}
111+
val data = hashMapOf<String, Any>(
112+
"length" to length,
113+
"info" to hashMapOf("author" to info.author,
114+
"creationDate" to creationDate,
115+
"modificationDate" to modificationDate,
116+
"creator" to info.creator, "producer" to info.producer,
117+
"keywords" to splitKeywords(info.keywords),
118+
"title" to info.title, "subject" to info.subject
119+
)
120+
)
121+
99122
Handler(Looper.getMainLooper()).post {
100-
result.success(length)
123+
result.success(data)
124+
}
125+
}
126+
127+
/**
128+
* Splits a string of keywords into a list of strings.
129+
*/
130+
private fun splitKeywords(keywordsString: String?): List<String>? {
131+
if (keywordsString == null) {
132+
return null
133+
}
134+
var keywords = keywordsString.split(",").toMutableList()
135+
for (i in keywords.indices) {
136+
var keyword = keywords[i]
137+
keyword = keyword.dropWhile { it == ' ' }
138+
keyword = keyword.dropLastWhile { it == ' ' }
139+
keywords[i] = keyword
101140
}
141+
return keywords
102142
}
103143

104144
/**

example/ios/Podfile

Lines changed: 15 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -10,81 +10,32 @@ project 'Runner', {
1010
'Release' => :release,
1111
}
1212

13-
def parse_KV_file(file, separator='=')
14-
file_abs_path = File.expand_path(file)
15-
if !File.exists? file_abs_path
16-
return [];
13+
def flutter_root
14+
generated_xcode_build_settings_path = File.expand_path(File.join('..', 'Flutter', 'Generated.xcconfig'), __FILE__)
15+
unless File.exist?(generated_xcode_build_settings_path)
16+
raise "#{generated_xcode_build_settings_path} must exist. If you're running pod install manually, make sure flutter pub get is executed first"
1717
end
18-
generated_key_values = {}
19-
skip_line_start_symbols = ["#", "/"]
20-
File.foreach(file_abs_path) do |line|
21-
next if skip_line_start_symbols.any? { |symbol| line =~ /^\s*#{symbol}/ }
22-
plugin = line.split(pattern=separator)
23-
if plugin.length == 2
24-
podname = plugin[0].strip()
25-
path = plugin[1].strip()
26-
podpath = File.expand_path("#{path}", file_abs_path)
27-
generated_key_values[podname] = podpath
28-
else
29-
puts "Invalid plugin specification: #{line}"
30-
end
18+
19+
File.foreach(generated_xcode_build_settings_path) do |line|
20+
matches = line.match(/FLUTTER_ROOT\=(.*)/)
21+
return matches[1].strip if matches
3122
end
32-
generated_key_values
23+
raise "FLUTTER_ROOT not found in #{generated_xcode_build_settings_path}. Try deleting Generated.xcconfig, then run flutter pub get"
3324
end
3425

26+
require File.expand_path(File.join('packages', 'flutter_tools', 'bin', 'podhelper'), flutter_root)
27+
28+
flutter_ios_podfile_setup
29+
3530
target 'Runner' do
3631
use_frameworks!
3732
use_modular_headers!
38-
39-
# Flutter Pod
4033

41-
copied_flutter_dir = File.join(__dir__, 'Flutter')
42-
copied_framework_path = File.join(copied_flutter_dir, 'Flutter.framework')
43-
copied_podspec_path = File.join(copied_flutter_dir, 'Flutter.podspec')
44-
unless File.exist?(copied_framework_path) && File.exist?(copied_podspec_path)
45-
# Copy Flutter.framework and Flutter.podspec to Flutter/ to have something to link against if the xcode backend script has not run yet.
46-
# That script will copy the correct debug/profile/release version of the framework based on the currently selected Xcode configuration.
47-
# CocoaPods will not embed the framework on pod install (before any build phases can generate) if the dylib does not exist.
48-
49-
generated_xcode_build_settings_path = File.join(copied_flutter_dir, 'Generated.xcconfig')
50-
unless File.exist?(generated_xcode_build_settings_path)
51-
raise "Generated.xcconfig must exist. If you're running pod install manually, make sure flutter pub get is executed first"
52-
end
53-
generated_xcode_build_settings = parse_KV_file(generated_xcode_build_settings_path)
54-
cached_framework_dir = generated_xcode_build_settings['FLUTTER_FRAMEWORK_DIR'];
55-
56-
unless File.exist?(copied_framework_path)
57-
FileUtils.cp_r(File.join(cached_framework_dir, 'Flutter.framework'), copied_flutter_dir)
58-
end
59-
unless File.exist?(copied_podspec_path)
60-
FileUtils.cp(File.join(cached_framework_dir, 'Flutter.podspec'), copied_flutter_dir)
61-
end
62-
end
63-
64-
# Keep pod path relative so it can be checked into Podfile.lock.
65-
pod 'Flutter', :path => 'Flutter'
66-
67-
# Plugin Pods
68-
69-
# Prepare symlinks folder. We use symlinks to avoid having Podfile.lock
70-
# referring to absolute paths on developers' machines.
71-
system('rm -rf .symlinks')
72-
system('mkdir -p .symlinks/plugins')
73-
plugin_pods = parse_KV_file('../.flutter-plugins')
74-
plugin_pods.each do |name, path|
75-
symlink = File.join('.symlinks', 'plugins', name)
76-
File.symlink(path, symlink)
77-
pod name, :path => File.join(symlink, 'ios')
78-
end
34+
flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
7935
end
8036

81-
# Prevent Cocoapods from embedding a second Flutter framework and causing an error with the new Xcode build system.
82-
install! 'cocoapods', :disable_input_output_paths => true
83-
8437
post_install do |installer|
8538
installer.pods_project.targets.each do |target|
86-
target.build_configurations.each do |config|
87-
config.build_settings['ENABLE_BITCODE'] = 'NO'
88-
end
39+
flutter_additional_ios_build_settings(target)
8940
end
9041
end

example/ios/Podfile.lock

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,33 @@ PODS:
22
- file_picker (0.0.1):
33
- Flutter
44
- Flutter (1.0.0)
5-
- flutter_plugin_android_lifecycle (0.0.1):
6-
- Flutter
75
- path_provider (0.0.1):
86
- Flutter
9-
- path_provider_macos (0.0.1):
10-
- Flutter
117
- pdf_text (0.0.1):
128
- Flutter
139

1410
DEPENDENCIES:
1511
- file_picker (from `.symlinks/plugins/file_picker/ios`)
1612
- Flutter (from `Flutter`)
17-
- flutter_plugin_android_lifecycle (from `.symlinks/plugins/flutter_plugin_android_lifecycle/ios`)
1813
- path_provider (from `.symlinks/plugins/path_provider/ios`)
19-
- path_provider_macos (from `.symlinks/plugins/path_provider_macos/ios`)
2014
- pdf_text (from `.symlinks/plugins/pdf_text/ios`)
2115

2216
EXTERNAL SOURCES:
2317
file_picker:
2418
:path: ".symlinks/plugins/file_picker/ios"
2519
Flutter:
2620
:path: Flutter
27-
flutter_plugin_android_lifecycle:
28-
:path: ".symlinks/plugins/flutter_plugin_android_lifecycle/ios"
2921
path_provider:
3022
:path: ".symlinks/plugins/path_provider/ios"
31-
path_provider_macos:
32-
:path: ".symlinks/plugins/path_provider_macos/ios"
3323
pdf_text:
3424
:path: ".symlinks/plugins/pdf_text/ios"
3525

3626
SPEC CHECKSUMS:
3727
file_picker: 408623be2125b79a4539cf703be3d4b3abe5e245
3828
Flutter: 0e3d915762c693b495b44d77113d4970485de6ec
39-
flutter_plugin_android_lifecycle: 47de533a02850f070f5696a623995e93eddcdb9b
4029
path_provider: abfe2b5c733d04e238b0d8691db0cfd63a27a93c
41-
path_provider_macos: f760a3c5b04357c380e2fddb6f9db6f3015897e0
4230
pdf_text: 850e21d1a62672674099a07818c95a3f55030c81
4331

44-
PODFILE CHECKSUM: 1b66dae606f75376c5f2135a8290850eeb09ae83
32+
PODFILE CHECKSUM: aafe91acc616949ddb318b77800a7f51bffa2a4c
4533

4634
COCOAPODS: 1.9.1

example/ios/Runner.xcodeproj/project.pbxproj

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,18 @@
262262
files = (
263263
);
264264
inputPaths = (
265+
"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh",
266+
"${PODS_ROOT}/../Flutter/Flutter.framework",
267+
"${BUILT_PRODUCTS_DIR}/file_picker/file_picker.framework",
268+
"${BUILT_PRODUCTS_DIR}/path_provider/path_provider.framework",
269+
"${BUILT_PRODUCTS_DIR}/pdf_text/pdf_text.framework",
265270
);
266271
name = "[CP] Embed Pods Frameworks";
267272
outputPaths = (
273+
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Flutter.framework",
274+
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/file_picker.framework",
275+
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/path_provider.framework",
276+
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/pdf_text.framework",
268277
);
269278
runOnlyForDeploymentPostprocessing = 0;
270279
shellPath = /bin/sh;
@@ -307,7 +316,6 @@
307316
/* Begin XCBuildConfiguration section */
308317
249021D3217E4FDB00AE95B9 /* Profile */ = {
309318
isa = XCBuildConfiguration;
310-
baseConfigurationReference = 7AFA3C8E1D35360C0083082E /* Release.xcconfig */;
311319
buildSettings = {
312320
ALWAYS_SEARCH_USER_PATHS = NO;
313321
CLANG_ANALYZER_NONNULL = YES;
@@ -385,7 +393,6 @@
385393
};
386394
97C147031CF9000F007C117D /* Debug */ = {
387395
isa = XCBuildConfiguration;
388-
baseConfigurationReference = 9740EEB21CF90195004384FC /* Debug.xcconfig */;
389396
buildSettings = {
390397
ALWAYS_SEARCH_USER_PATHS = NO;
391398
CLANG_ANALYZER_NONNULL = YES;
@@ -441,7 +448,6 @@
441448
};
442449
97C147041CF9000F007C117D /* Release */ = {
443450
isa = XCBuildConfiguration;
444-
baseConfigurationReference = 7AFA3C8E1D35360C0083082E /* Release.xcconfig */;
445451
buildSettings = {
446452
ALWAYS_SEARCH_USER_PATHS = NO;
447453
CLANG_ANALYZER_NONNULL = YES;

example/lib/main.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ class _MyAppState extends State<MyApp> {
9393
Future _pickPDFText() async {
9494
File file = await FilePicker.getFile();
9595
_pdfDoc = await PDFDoc.fromFile(file);
96+
print(_pdfDoc.info.producer);
97+
print(_pdfDoc.info.creator);
98+
print(_pdfDoc.info.creationDate);
99+
print(_pdfDoc.info.modificationDate);
100+
print(_pdfDoc.info.subject);
101+
print(_pdfDoc.info.keywords);
102+
print(_pdfDoc.info.title);
103+
print(_pdfDoc.info.author);
96104
setState(() {});
97105
}
98106

0 commit comments

Comments
 (0)