source: gs2-extensions/pdf-box/trunk/java/perllib/plugins/PDFBoxConverter.pm@ 25513

Last change on this file since 25513 was 25513, checked in by ak19, 12 years ago

Correction to previous commit: could not call a subroutine from a BEGIN block (in this case, could not call the new get_java_command() subroutine). But as subroutines in other files that are included can be called from BEGIN, I've added get_java_command as a utility function in GS' perllib/util.pm and this now gets called instead.

File size: 8.0 KB
Line 
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###########################################################################
26package PDFBoxConverter;
27
28use BaseMediaConverter;
29
30use strict;
31no strict 'refs'; # allow filehandles to be variables and viceversa
32no strict 'subs'; # allow barewords (eg STDERR) as function arguments
33
34use gsprintf 'gsprintf';
35
36# these two variables mustn't be initialised here or they will get stuck
37# at those values.
38our $pdfbox_conversion_available;
39our $no_pdfbox_conversion_reason;
40
41BEGIN {
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 = &util::get_java_command();
69
70 my $cmd = "$java -version";
71 if ($ENV{'GSDLOS'} =~ /^windows/i) {
72 $cmd .= " >nul 2>&1"; # java 2>&1 >null or java >null 2>&1 both work (%ERRORLEVEL% is 0)
73 }
74 else {
75 # On Ubuntu, java >/dev/null 2>&1 works,
76 # but java 2>&1 >/dev/null doesn't work: output goes to screen anyway
77 $cmd .= " >/dev/null 2>&1"; # " >/dev/null 2>&1 &" - don't need & at end for Linux Centos anymore (Ubuntu was already fine without it)
78 }
79
80 my $status = system($cmd);
81
82 if ($status != 0) {
83
84 my $error_message = "**** Testing for java\n";
85 $error_message .= "Failed to run: $cmd\n";
86 $error_message .= "Error variable: |$!| and status: $status\n";
87
88 &gsprintf(STDERR, "PDFBoxConverter: $error_message");
89
90 $pdfbox_conversion_available = 0;
91 $no_pdfbox_conversion_reason = "couldnotrunjava";
92 }
93 }
94 }
95
96}
97
98my $arguments = [ ];
99
100my $options = { 'name' => "PDFBoxConverter",
101 'desc' => "{PDFBoxConverter.desc}",
102 'abstract' => "yes",
103 'inherits' => "yes",
104 'args' => $arguments };
105
106sub new {
107 my ($class) = shift (@_);
108 my ($pluginlist,$inputargs,$hashArgOptLists,$auxilary) = @_;
109 push(@$pluginlist, $class);
110
111 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
112 push(@{$hashArgOptLists->{"OptList"}},$options);
113
114
115 my $self = new BaseMediaConverter($pluginlist, $inputargs,
116 $hashArgOptLists, $auxilary);
117
118 if ($self->{'info_only'}) {
119 # don't worry about any options etc
120 return bless $self, $class;
121 }
122 if ($pdfbox_conversion_available) {
123 my $gextpb_home = $ENV{'GEXT_PDFBOX'};
124 my $pbajar = &util::filename_cat($gextpb_home,"lib","java","pdfbox-app.jar");
125 my $java = &util::get_java_command();
126 my $launch_cmd = "$java -cp \"$pbajar\" -Dline.separator=\"<br />\" org.apache.pdfbox.ExtractText";
127
128 $self->{'pdfbox_launch_cmd'} = $launch_cmd;
129 }
130 else {
131 $self->{'no_pdfbox_conversion_reason'} = $no_pdfbox_conversion_reason;
132
133 my $outhandle = $self->{'outhandle'};
134 &gsprintf($outhandle, "PDFBoxConverter: {PDFBoxConverter.noconversionavailable} ({PDFBoxConverter.$no_pdfbox_conversion_reason})\n");
135 }
136
137 $self->{'pdfbox_conversion_available'} = $pdfbox_conversion_available;
138
139 return bless $self, $class;
140
141}
142
143sub init {
144 my $self = shift(@_);
145 my ($verbosity, $outhandle, $failhandle) = @_;
146
147 $self->{'pbtmp_file_paths'} = ();
148}
149
150sub deinit {
151 my $self = shift(@_);
152
153 $self->clean_up_temporary_files();
154}
155
156
157sub convert {
158 my $self = shift(@_);
159 my ($source_file_full_path, $target_file_type) = @_;
160
161 return 0 unless $pdfbox_conversion_available;
162 # check the filename
163 return 0 if ( !-f $source_file_full_path);
164
165 # the following line is necessary to avoid 'uninitialised variable' error
166 # messages concerning the converted_to member variable when PDFPlugin's
167 # use_sections option is checked.
168 # PDFBox plugin now processes use_sections option, when working with v1.5.0
169 # of the PDFBox jar file (which embeds each page in special <div> tags).
170 if ($target_file_type eq "html") {
171 $self->{'converted_to'} = "HTML";
172 } else {
173 $self->{'converted_to'} = "text";
174 }
175
176 my $outhandle = $self->{'outhandle'};
177 my $verbosity = $self->{'verbosity'};
178
179 my $source_file_no_path = &File::Basename::basename($source_file_full_path);
180 # Determine the full name and path of the output file
181 my $target_file_path;
182 if ($self->{'enable_cache'}) {
183 $self->init_cache_for_file($source_file_full_path);
184 my $cache_dir = $self->{'cached_dir'};
185 my $file_root = $self->{'cached_file_root'};
186 #$file_root .= "_$convert_id" if ($convert_id ne "");
187 my $target_file = "$file_root.$target_file_type";
188 $target_file_path = &util::filename_cat($cache_dir,$target_file);
189 }
190 else {
191 # this is in gsdl/tmp. get a tmp filename in collection instead???
192 $target_file_path = &util::get_tmp_filename($target_file_type);
193 push(@{$self->{'pbtmp_file_paths'}}, $target_file_path);
194 }
195
196 # Generate and run the convert command
197 my $convert_cmd = $self->{'pdfbox_launch_cmd'};
198 $convert_cmd .= " -html" if ($target_file_type eq "html");
199 $convert_cmd .= " \"$source_file_full_path\" \"$target_file_path\"";
200
201 if ($verbosity>2) {
202 &gsprintf($outhandle,"Convert command: $convert_cmd\n");
203 }
204
205 my $print_info = { 'message_prefix' => "PDFBox Conversion",
206 'message' => "Converting $source_file_no_path to: $target_file_type" };
207 # $print_info->{'cache_mode'} = $cache_mode if ($cache_mode ne "");
208
209 my ($regenerated,$result,$had_error)
210 = $self->autorun_general_cmd($convert_cmd,$source_file_full_path, $target_file_path,$print_info);
211 if ($had_error) {
212 return (0, $result,$target_file_path);
213 }
214 return (1, $result,$target_file_path);
215}
216
217sub convert_without_result {
218 my $self = shift(@_);
219
220 my $source_file_path = shift(@_);
221 my $target_file_type = shift(@_);
222 my $convert_options = shift(@_) || "";
223 my $convert_id = shift(@_) || "";
224
225 return $self->convert($source_file_path,$target_file_type,
226 $convert_options,$convert_id,"without_result");
227}
228
229sub clean_up_temporary_files {
230 my $self = shift(@_);
231
232 foreach my $pbtmp_file_path (@{$self->{'pbtmp_file_paths'}}) {
233 if (-e $pbtmp_file_path) {
234 &util::rm($pbtmp_file_path);
235 }
236 }
237
238 $self->{'pbtmp_file_paths'} = ();
239}
240
241
2421;
Note: See TracBrowser for help on using the repository browser.