root/gs2-extensions/pdf-box/trunk/java/perllib/plugins/PDFBoxConverter.pm @ 24301

Revision 24301, 7.1 KB (checked in by ak19, 9 years ago)

1. Putting the pdfbox-app.jar executable file back to the official 1.5.0 release (without Greenstone-specific changes regarding the line-separator). 2. Instead, Dr Bainbridge's suggestions have been followed to set the line.separator as a property when launching the pdfbox-app.jar, since it is a Java System property that needed to be adjusted for GS' customisation of PDFBox. Tested, works.

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
32
33use gsprintf 'gsprintf';
34
35# these two variables mustn't be initialised here or they will get stuck
36# at those values.
37our $pdfbox_conversion_available;
38our $no_pdfbox_conversion_reason;
39
40BEGIN {
41    @PDFBoxConverter::ISA = ('BaseMediaConverter');
42
43    # Check that PDFBox is installed and available on the path
44    $pdfbox_conversion_available = 1;
45    $no_pdfbox_conversion_reason = "";
46   
47    if (!defined $ENV{'GEXT_PDFBOX'}) {
48    $pdfbox_conversion_available = 0;
49    $no_pdfbox_conversion_reason = "gextpdfboxnotinstalled";
50    }
51    else {
52    my $gextpb_home = $ENV{'GEXT_PDFBOX'};
53    my $pbajar = &util::filename_cat($gextpb_home,"lib","java","pdfbox-app.jar");
54
55    if (!-e $pbajar) {
56        print STDERR "Failed to find $pbajar\n";
57        $pdfbox_conversion_available = 0;
58        $no_pdfbox_conversion_reason = "gextpdfboxjarnotinstalled";
59    }
60    else {
61        # test to see if java is in path     
62        my $cmd = "java 2>&1";
63        if ($ENV{'GSDLOS'} =~ /^windows/i) {
64        $cmd .= " >nul";
65        }
66        else {
67        $cmd .= " >/dev/null &";
68        }
69
70        my $status = system($cmd);
71
72        if ($status != 0) {
73        print STDERR "Testing for java\n";
74        print STDERR "Failed to run: $cmd\n";
75        print STDERR "$!\n";
76        $pdfbox_conversion_available = 0;
77        $no_pdfbox_conversion_reason = "couldnotrunjava";
78        }
79    }
80    }
81
82}
83
84my $arguments = [ ];
85
86my $options = { 'name' => "PDFBoxConverter",
87        'desc' => "{PDFBoxConverter.desc}",
88        'abstract' => "yes",
89        'inherits' => "yes",
90        'args' => $arguments };
91
92sub new {
93    my ($class) = shift (@_);
94    my ($pluginlist,$inputargs,$hashArgOptLists,$auxilary) = @_;
95    push(@$pluginlist, $class);
96
97    push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
98    push(@{$hashArgOptLists->{"OptList"}},$options);
99
100
101    my $self = new BaseMediaConverter($pluginlist, $inputargs,
102                      $hashArgOptLists, $auxilary);
103
104    if ($self->{'info_only'}) {
105    # don't worry about any options etc
106    return bless $self, $class;
107    }
108    if ($pdfbox_conversion_available) {
109    my $gextpb_home = $ENV{'GEXT_PDFBOX'};
110    my $pbajar = &util::filename_cat($gextpb_home,"lib","java","pdfbox-app.jar");   
111    my $launch_cmd = "java -cp \"$pbajar\" -Dline.separator=\"<br />\" org.apache.pdfbox.ExtractText";
112   
113    $self->{'pdfbox_launch_cmd'} = $launch_cmd;
114    }
115    else {       
116    $self->{'no_pdfbox_conversion_reason'} = $no_pdfbox_conversion_reason;
117
118    my $outhandle = $self->{'outhandle'};
119    &gsprintf($outhandle, "PDFBoxConverter: {PDFBoxConverter.noconversionavailable} ({PDFBoxConverter.$no_pdfbox_conversion_reason})\n");
120    } 
121
122    $self->{'pdfbox_conversion_available'} = $pdfbox_conversion_available;
123   
124    return bless $self, $class;
125
126}
127
128sub init {
129    my $self = shift(@_);
130    my ($verbosity, $outhandle, $failhandle) = @_;
131
132    $self->{'pbtmp_file_paths'} = ();
133}
134
135sub deinit {
136    my $self = shift(@_);
137
138    $self->clean_up_temporary_files();
139}
140
141
142sub convert {
143    my $self = shift(@_);
144    my ($source_file_full_path, $target_file_type) = @_;
145
146    return 0 unless $pdfbox_conversion_available;
147    # check the filename
148    return 0 if ( !-f $source_file_full_path);
149
150    # the following line is necessary to avoid 'uninitialised variable' error
151    # messages concerning the converted_to member variable when PDFPlugin's
152    # use_sections option is checked.
153    # PDFBox plugin now processes use_sections option, when working with v1.5.0
154    # of the PDFBox jar file (which embeds each page in special <div> tags).
155    if ($target_file_type eq "html") {
156    $self->{'converted_to'} = "HTML";
157    } else {
158    $self->{'converted_to'} = "text";
159    }
160
161    my $outhandle = $self->{'outhandle'};
162    my $verbosity = $self->{'verbosity'};
163
164    my $source_file_no_path = &File::Basename::basename($source_file_full_path);
165    # Determine the full name and path of the output file
166    my $target_file_path;
167    if ($self->{'enable_cache'}) {
168    $self->init_cache_for_file($source_file_full_path);
169    my $cache_dir = $self->{'cached_dir'};
170    my $file_root = $self->{'cached_file_root'};
171    #$file_root .= "_$convert_id" if ($convert_id ne "");
172    my $target_file = "$file_root.$target_file_type";
173    $target_file_path = &util::filename_cat($cache_dir,$target_file);
174    }
175    else {
176    # this is in gsdl/tmp. get a tmp filename in collection instead???
177    $target_file_path = &util::get_tmp_filename($target_file_type);
178    push(@{$self->{'pbtmp_file_paths'}}, $target_file_path);
179    }
180
181    # Generate and run the convert command
182    my $convert_cmd = $self->{'pdfbox_launch_cmd'};
183    $convert_cmd .= " -html" if ($target_file_type eq "html");
184    $convert_cmd .= " \"$source_file_full_path\" \"$target_file_path\"";
185
186    if ($verbosity>2) {
187    print $outhandle "Convert command: $convert_cmd\n";
188    }
189
190    my $print_info = { 'message_prefix' => "PDFBox Conversion",
191               'message' => "Converting $source_file_no_path to: $target_file_type" };
192    # $print_info->{'cache_mode'} = $cache_mode if ($cache_mode ne "");
193
194    my ($regenerated,$result,$had_error)
195    = $self->autorun_general_cmd($convert_cmd,$source_file_full_path, $target_file_path,$print_info);
196    if ($had_error) {
197    return (0, $result,$target_file_path);
198    }
199    return (1, $result,$target_file_path);
200}
201
202sub convert_without_result {
203    my $self = shift(@_);
204
205    my $source_file_path = shift(@_);
206    my $target_file_type = shift(@_);
207    my $convert_options  = shift(@_) || "";
208    my $convert_id       = shift(@_) || "";
209
210    return $self->convert($source_file_path,$target_file_type,
211              $convert_options,$convert_id,"without_result");
212}
213
214sub clean_up_temporary_files {
215    my $self = shift(@_);
216
217    foreach my $pbtmp_file_path (@{$self->{'pbtmp_file_paths'}}) {
218    if (-e $pbtmp_file_path) {
219        &util::rm($pbtmp_file_path);
220    }
221    }
222
223    $self->{'pbtmp_file_paths'} = ();
224}
225
226
227
2281; 
Note: See TracBrowser for help on using the browser.