1 | ###########################################################################
|
---|
2 | #
|
---|
3 | # PDFBoxConverter - helper plugin that does pdf document conversion with PDFBox
|
---|
4 | #
|
---|
5 | # A component of the Greenstone digital library software
|
---|
6 | # from the New Zealand Digital Library Project at the
|
---|
7 | # University of Waikato, New Zealand.
|
---|
8 | #
|
---|
9 | # Copyright (C) 2010 New Zealand Digital Library Project
|
---|
10 | #
|
---|
11 | # This program is free software; you can redistribute it and/or modify
|
---|
12 | # it under the terms of the GNU General Public License as published by
|
---|
13 | # the Free Software Foundation; either version 2 of the License, or
|
---|
14 | # (at your option) any later version.
|
---|
15 | #
|
---|
16 | # This program is distributed in the hope that it will be useful,
|
---|
17 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
19 | # GNU General Public License for more details.
|
---|
20 | #
|
---|
21 | # You should have received a copy of the GNU General Public License
|
---|
22 | # along with this program; if not, write to the Free Software
|
---|
23 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
24 | #
|
---|
25 | ###########################################################################
|
---|
26 | package PDFBoxConverter;
|
---|
27 |
|
---|
28 | use BaseMediaConverter;
|
---|
29 |
|
---|
30 | use strict;
|
---|
31 | no strict 'refs'; # allow filehandles to be variables and viceversa
|
---|
32 | no strict 'subs'; # allow barewords (eg STDERR) as function arguments
|
---|
33 |
|
---|
34 | use gsprintf 'gsprintf';
|
---|
35 |
|
---|
36 | # these two variables mustn't be initialised here or they will get stuck
|
---|
37 | # at those values.
|
---|
38 | our $pdfbox_conversion_available;
|
---|
39 | our $no_pdfbox_conversion_reason;
|
---|
40 |
|
---|
41 | BEGIN {
|
---|
42 | @PDFBoxConverter::ISA = ('BaseMediaConverter');
|
---|
43 |
|
---|
44 | # Check that PDFBox is installed and available on the path
|
---|
45 | $pdfbox_conversion_available = 1;
|
---|
46 | $no_pdfbox_conversion_reason = "";
|
---|
47 |
|
---|
48 | if (!defined $ENV{'GEXT_PDFBOX'}) {
|
---|
49 | $pdfbox_conversion_available = 0;
|
---|
50 | $no_pdfbox_conversion_reason = "gextpdfboxnotinstalled";
|
---|
51 | }
|
---|
52 | else {
|
---|
53 | my $gextpb_home = $ENV{'GEXT_PDFBOX'};
|
---|
54 | my $pbajar = &util::filename_cat($gextpb_home,"lib","java","pdfbox-app.jar");
|
---|
55 |
|
---|
56 | if (!-e $pbajar) {
|
---|
57 | &gsprintf(STDERR,"**** Failed to find $pbajar\n");
|
---|
58 | $pdfbox_conversion_available = 0;
|
---|
59 | $no_pdfbox_conversion_reason = "gextpdfboxjarnotinstalled";
|
---|
60 | }
|
---|
61 | else {
|
---|
62 | # test to see if java is in path
|
---|
63 | # Need to run java -version instead of just java, since the %ERRORLEVEL% returned
|
---|
64 | # for `java` (which is checked below for failure of the command) is 0 for JDK 1.6*
|
---|
65 | # while %ERRORLEVEL% is 1 for JDK 1.7*
|
---|
66 | # If `java -version` is run however, %ERRORLEVEL% returned is 0 if java is
|
---|
67 | # installed, regardless of whether the JDK version is 1.6* or 1.7*.
|
---|
68 | my $java = &get_java_command();
|
---|
69 | my $cmd = "$java -version";
|
---|
70 | if ($ENV{'GSDLOS'} =~ /^windows/i) {
|
---|
71 | $cmd .= " >nul 2>&1"; # java 2>&1 >null or java >null 2>&1 both work (%ERRORLEVEL% is 0)
|
---|
72 | }
|
---|
73 | else {
|
---|
74 | # On Ubuntu, java >/dev/null 2>&1 works,
|
---|
75 | # but java 2>&1 >/dev/null doesn't work: output goes to screen anyway
|
---|
76 | $cmd .= " >/dev/null 2>&1"; # " >/dev/null 2>&1 &" - don't need & at end for Linux Centos anymore (Ubuntu was already fine without it)
|
---|
77 | }
|
---|
78 |
|
---|
79 | my $status = system($cmd);
|
---|
80 |
|
---|
81 | if ($status != 0) {
|
---|
82 |
|
---|
83 | my $error_message = "**** Testing for java\n";
|
---|
84 | $error_message .= "Failed to run: $cmd\n";
|
---|
85 | $error_message .= "Error variable: |$!| and status: $status\n";
|
---|
86 |
|
---|
87 | &gsprintf(STDERR, "PDFBoxConverter: $error_message");
|
---|
88 |
|
---|
89 | $pdfbox_conversion_available = 0;
|
---|
90 | $no_pdfbox_conversion_reason = "couldnotrunjava";
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | }
|
---|
96 |
|
---|
97 | my $arguments = [ ];
|
---|
98 |
|
---|
99 | my $options = { 'name' => "PDFBoxConverter",
|
---|
100 | 'desc' => "{PDFBoxConverter.desc}",
|
---|
101 | 'abstract' => "yes",
|
---|
102 | 'inherits' => "yes",
|
---|
103 | 'args' => $arguments };
|
---|
104 |
|
---|
105 | sub new {
|
---|
106 | my ($class) = shift (@_);
|
---|
107 | my ($pluginlist,$inputargs,$hashArgOptLists,$auxilary) = @_;
|
---|
108 | push(@$pluginlist, $class);
|
---|
109 |
|
---|
110 | push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
|
---|
111 | push(@{$hashArgOptLists->{"OptList"}},$options);
|
---|
112 |
|
---|
113 |
|
---|
114 | my $self = new BaseMediaConverter($pluginlist, $inputargs,
|
---|
115 | $hashArgOptLists, $auxilary);
|
---|
116 |
|
---|
117 | if ($self->{'info_only'}) {
|
---|
118 | # don't worry about any options etc
|
---|
119 | return bless $self, $class;
|
---|
120 | }
|
---|
121 | if ($pdfbox_conversion_available) {
|
---|
122 | my $gextpb_home = $ENV{'GEXT_PDFBOX'};
|
---|
123 | my $pbajar = &util::filename_cat($gextpb_home,"lib","java","pdfbox-app.jar");
|
---|
124 | my $java = &get_java_command();
|
---|
125 | my $launch_cmd = "$java -cp \"$pbajar\" -Dline.separator=\"<br />\" org.apache.pdfbox.ExtractText";
|
---|
126 |
|
---|
127 | $self->{'pdfbox_launch_cmd'} = $launch_cmd;
|
---|
128 | }
|
---|
129 | else {
|
---|
130 | $self->{'no_pdfbox_conversion_reason'} = $no_pdfbox_conversion_reason;
|
---|
131 |
|
---|
132 | my $outhandle = $self->{'outhandle'};
|
---|
133 | &gsprintf($outhandle, "PDFBoxConverter: {PDFBoxConverter.noconversionavailable} ({PDFBoxConverter.$no_pdfbox_conversion_reason})\n");
|
---|
134 | }
|
---|
135 |
|
---|
136 | $self->{'pdfbox_conversion_available'} = $pdfbox_conversion_available;
|
---|
137 |
|
---|
138 | return bless $self, $class;
|
---|
139 |
|
---|
140 | }
|
---|
141 |
|
---|
142 | sub init {
|
---|
143 | my $self = shift(@_);
|
---|
144 | my ($verbosity, $outhandle, $failhandle) = @_;
|
---|
145 |
|
---|
146 | $self->{'pbtmp_file_paths'} = ();
|
---|
147 | }
|
---|
148 |
|
---|
149 | sub deinit {
|
---|
150 | my $self = shift(@_);
|
---|
151 |
|
---|
152 | $self->clean_up_temporary_files();
|
---|
153 | }
|
---|
154 |
|
---|
155 |
|
---|
156 | sub convert {
|
---|
157 | my $self = shift(@_);
|
---|
158 | my ($source_file_full_path, $target_file_type) = @_;
|
---|
159 |
|
---|
160 | return 0 unless $pdfbox_conversion_available;
|
---|
161 | # check the filename
|
---|
162 | return 0 if ( !-f $source_file_full_path);
|
---|
163 |
|
---|
164 | # the following line is necessary to avoid 'uninitialised variable' error
|
---|
165 | # messages concerning the converted_to member variable when PDFPlugin's
|
---|
166 | # use_sections option is checked.
|
---|
167 | # PDFBox plugin now processes use_sections option, when working with v1.5.0
|
---|
168 | # of the PDFBox jar file (which embeds each page in special <div> tags).
|
---|
169 | if ($target_file_type eq "html") {
|
---|
170 | $self->{'converted_to'} = "HTML";
|
---|
171 | } else {
|
---|
172 | $self->{'converted_to'} = "text";
|
---|
173 | }
|
---|
174 |
|
---|
175 | my $outhandle = $self->{'outhandle'};
|
---|
176 | my $verbosity = $self->{'verbosity'};
|
---|
177 |
|
---|
178 | my $source_file_no_path = &File::Basename::basename($source_file_full_path);
|
---|
179 | # Determine the full name and path of the output file
|
---|
180 | my $target_file_path;
|
---|
181 | if ($self->{'enable_cache'}) {
|
---|
182 | $self->init_cache_for_file($source_file_full_path);
|
---|
183 | my $cache_dir = $self->{'cached_dir'};
|
---|
184 | my $file_root = $self->{'cached_file_root'};
|
---|
185 | #$file_root .= "_$convert_id" if ($convert_id ne "");
|
---|
186 | my $target_file = "$file_root.$target_file_type";
|
---|
187 | $target_file_path = &util::filename_cat($cache_dir,$target_file);
|
---|
188 | }
|
---|
189 | else {
|
---|
190 | # this is in gsdl/tmp. get a tmp filename in collection instead???
|
---|
191 | $target_file_path = &util::get_tmp_filename($target_file_type);
|
---|
192 | push(@{$self->{'pbtmp_file_paths'}}, $target_file_path);
|
---|
193 | }
|
---|
194 |
|
---|
195 | # Generate and run the convert command
|
---|
196 | my $convert_cmd = $self->{'pdfbox_launch_cmd'};
|
---|
197 | $convert_cmd .= " -html" if ($target_file_type eq "html");
|
---|
198 | $convert_cmd .= " \"$source_file_full_path\" \"$target_file_path\"";
|
---|
199 |
|
---|
200 | if ($verbosity>2) {
|
---|
201 | &gsprintf($outhandle,"Convert command: $convert_cmd\n");
|
---|
202 | }
|
---|
203 |
|
---|
204 | my $print_info = { 'message_prefix' => "PDFBox Conversion",
|
---|
205 | 'message' => "Converting $source_file_no_path to: $target_file_type" };
|
---|
206 | # $print_info->{'cache_mode'} = $cache_mode if ($cache_mode ne "");
|
---|
207 |
|
---|
208 | my ($regenerated,$result,$had_error)
|
---|
209 | = $self->autorun_general_cmd($convert_cmd,$source_file_full_path, $target_file_path,$print_info);
|
---|
210 | if ($had_error) {
|
---|
211 | return (0, $result,$target_file_path);
|
---|
212 | }
|
---|
213 | return (1, $result,$target_file_path);
|
---|
214 | }
|
---|
215 |
|
---|
216 | sub convert_without_result {
|
---|
217 | my $self = shift(@_);
|
---|
218 |
|
---|
219 | my $source_file_path = shift(@_);
|
---|
220 | my $target_file_type = shift(@_);
|
---|
221 | my $convert_options = shift(@_) || "";
|
---|
222 | my $convert_id = shift(@_) || "";
|
---|
223 |
|
---|
224 | return $self->convert($source_file_path,$target_file_type,
|
---|
225 | $convert_options,$convert_id,"without_result");
|
---|
226 | }
|
---|
227 |
|
---|
228 | sub clean_up_temporary_files {
|
---|
229 | my $self = shift(@_);
|
---|
230 |
|
---|
231 | foreach my $pbtmp_file_path (@{$self->{'pbtmp_file_paths'}}) {
|
---|
232 | if (-e $pbtmp_file_path) {
|
---|
233 | &util::rm($pbtmp_file_path);
|
---|
234 | }
|
---|
235 | }
|
---|
236 |
|
---|
237 | $self->{'pbtmp_file_paths'} = ();
|
---|
238 | }
|
---|
239 |
|
---|
240 | sub get_java_command {
|
---|
241 | my $java = "java";
|
---|
242 | if(defined $ENV{'GSDLHOME'}) { # should be, as this script would be launched from the cmd line
|
---|
243 | # after running setup.bat or from GLI which also runs setup.bat
|
---|
244 | my $java_bin = &util::filename_cat($ENV{'GSDLHOME'},"packages","jre","bin");
|
---|
245 | if(-d $java_bin) {
|
---|
246 | $java = &util::filename_cat($java_bin,"java");
|
---|
247 | }
|
---|
248 | }
|
---|
249 | return $java;
|
---|
250 | }
|
---|
251 |
|
---|
252 |
|
---|
253 | 1;
|
---|