source: gs2-extensions/pdf-box/perllib/plugins/PDFBoxConverter.pm@ 22669

Last change on this file since 22669 was 22669, checked in by sjm84, 12 years ago

Adding the PDFBox extension

File size: 6.4 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
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 if ($status != 0) {
72 print STDERR "Testing for java\n";
73 print STDERR "Failed to run: $cmd\n";
74 print STDERR "$!\n";
75 $pdfbox_conversion_available = 0;
76 $no_pdfbox_conversion_reason = "pdfboxnotinstalled";
77 }
78 }
79 }
80
81}
82
83my $arguments = [ ];
84
85my $options = { 'name' => "PDFBoxConverter",
86 'desc' => "{PDFBoxConverter.desc}",
87 'abstract' => "yes",
88 'inherits' => "yes",
89 'args' => $arguments };
90
91sub new {
92 my ($class) = shift (@_);
93 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
94 push(@$pluginlist, $class);
95
96 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
97 push(@{$hashArgOptLists->{"OptList"}},$options);
98
99 my $self = new BaseMediaConverter($pluginlist, $inputargs, $hashArgOptLists);
100
101 if ($self->{'info_only'}) {
102 # don't worry about any options etc
103 return bless $self, $class;
104 }
105 if ($pdfbox_conversion_available) {
106 my $gextpb_home = $ENV{'GEXT_PDFBOX'};
107 my $pbajar = &util::filename_cat($gextpb_home,"lib","java","pdfbox-app.jar");
108 my $launch_cmd = "java -cp $pbajar org.apache.pdfbox.ExtractText";
109
110 $self->{'pdfbox_launch_cmd'} = $launch_cmd;
111 }
112 else {
113 $self->{'no_pdfbox_conversion_reason'} = $no_pdfbox_conversion_reason;
114
115 my $outhandle = $self->{'outhandle'};
116 &gsprintf($outhandle, "PDFBoxConverter: {PDFBoxConverter.noconversionavailable} ({PDFBoxConverter.$no_pdfbox_conversion_reason})\n");
117 }
118
119 $self->{'pdfbox_conversion_available'} = $pdfbox_conversion_available;
120
121 return bless $self, $class;
122
123}
124
125sub init {
126 my $self = shift(@_);
127 my ($verbosity, $outhandle, $failhandle) = @_;
128
129 $self->{'pbtmp_file_paths'} = ();
130}
131
132sub deinit {
133 my $self = shift(@_);
134
135 $self->clean_up_temporary_files();
136}
137
138
139sub convert {
140 my $self = shift(@_);
141 my ($source_file_full_path, $target_file_type) = @_;
142
143 return 0 unless $pdfbox_conversion_available;
144 # check the filename
145 return 0 if ( !-f $source_file_full_path);
146
147 my $source_file_no_path = &File::Basename::basename($source_file_full_path);
148 # Determine the full name and path of the output file
149 my $target_file_path;
150 if ($self->{'enable_cache'}) {
151 $self->init_cache_for_file($source_file_full_path);
152 my $cache_dir = $self->{'cached_dir'};
153 my $file_root = $self->{'cached_file_root'};
154 #$file_root .= "_$convert_id" if ($convert_id ne "");
155 my $target_file = "$file_root.$target_file_type";
156 $target_file_path = &util::filename_cat($cache_dir,$target_file);
157 }
158 else {
159 # this is in gsdl/tmp. get a tmp filename in collection instead???
160 $target_file_path = &util::get_tmp_filename($target_file_type);
161 push(@{$self->{'pbtmp_file_paths'}}, $target_file_path);
162 }
163
164 # Generate and run the convert command
165 my $convert_cmd = $self->{'launch_cmd'};
166 $convert_cmd .= " -html" if ($target_file_type eq "html");
167 my $convert_command .= " \"$source_file_full_path\" \"$target_file_path\"";
168
169 print STDERR "**** convert_command = $convert_command\n";
170
171 my $print_info = { 'message_prefix' => "PDFBox Conversion",
172 'message' => "Converting $source_file_no_path to: $target_file_type" };
173 # $print_info->{'cache_mode'} = $cache_mode if ($cache_mode ne "");
174
175 my ($regenerated,$result,$had_error)
176 = $self->autorun_general_cmd($convert_command,$source_file_full_path, $target_file_path,$print_info);
177 if ($had_error) {
178 return (0, $result,$target_file_path);
179 }
180 return (1, $result,$target_file_path);
181}
182
183sub convert_without_result {
184 my $self = shift(@_);
185
186 my $source_file_path = shift(@_);
187 my $target_file_type = shift(@_);
188 my $convert_options = shift(@_) || "";
189 my $convert_id = shift(@_) || "";
190
191 return $self->convert($source_file_path,$target_file_type,
192 $convert_options,$convert_id,"without_result");
193}
194
195sub clean_up_temporary_files {
196 my $self = shift(@_);
197
198 foreach my $pbtmp_file_path (@{$self->{'pbtmp_file_paths'}}) {
199 if (-e $pbtmp_file_path) {
200 &util::rm($pbtmp_file_path);
201 }
202 }
203
204 $self->{'pbtmp_file_paths'} = ();
205}
206
207
208
2091;
Note: See TracBrowser for help on using the repository browser.