source: main/trunk/greenstone2/perllib/plugins/PDFPlugin.pm@ 22675

Last change on this file since 22675 was 22675, checked in by sjm84, 14 years ago

Modified PDFPlugin to use PDFBox if it is available

  • Property svn:keywords set to Author Date Id Revision
File size: 12.3 KB
Line 
1###########################################################################
2#
3# PDFPlugin.pm -- reasonably with-it pdf plugin
4# A component of the Greenstone digital library software
5# from the New Zealand Digital Library Project at the
6# University of Waikato, New Zealand.
7#
8# Copyright (C) 1999-2001 New Zealand Digital Library Project
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23#
24###########################################################################
25package PDFPlugin;
26
27use PBConvertBinaryFile;
28use ReadTextFile;
29use unicode;
30use strict;
31no strict 'refs'; # so we can use a var for filehandles (eg STDERR)
32
33sub BEGIN {
34 @PDFPlugin::ISA = ('PBConvertBinaryFile', 'ReadTextFile');
35}
36
37my $convert_to_list =
38 [ { 'name' => "auto",
39 'desc' => "{ConvertBinaryFile.convert_to.auto}" },
40 { 'name' => "html",
41 'desc' => "{ConvertBinaryFile.convert_to.html}" },
42 { 'name' => "text",
43 'desc' => "{ConvertBinaryFile.convert_to.text}" },
44 { 'name' => "pagedimg_jpg",
45 'desc' => "{ConvertBinaryFile.convert_to.pagedimg_jpg}"},
46 { 'name' => "pagedimg_gif",
47 'desc' => "{ConvertBinaryFile.convert_to.pagedimg_gif}"},
48 { 'name' => "pagedimg_png",
49 'desc' => "{ConvertBinaryFile.convert_to.pagedimg_png}"},
50 ];
51
52
53my $arguments =
54 [
55 { 'name' => "convert_to",
56 'desc' => "{ConvertBinaryFile.convert_to}",
57 'type' => "enum",
58 'reqd' => "yes",
59 'list' => $convert_to_list,
60 'deft' => "html" },
61 { 'name' => "process_exp",
62 'desc' => "{BasePlugin.process_exp}",
63 'type' => "regexp",
64 'deft' => &get_default_process_exp(),
65 'reqd' => "no" },
66 { 'name' => "block_exp",
67 'desc' => "{BasePlugin.block_exp}",
68 'type' => "regexp",
69 'deft' => &get_default_block_exp() },
70 { 'name' => "metadata_fields",
71 'desc' => "{HTMLPlugin.metadata_fields}",
72 'type' => "string",
73 'deft' => "" },
74 { 'name' => "metadata_field_separator",
75 'desc' => "{HTMLPlugin.metadata_field_separator}",
76 'type' => "string",
77 'deft' => "" },
78 { 'name' => "noimages",
79 'desc' => "{PDFPlugin.noimages}",
80 'type' => "flag" },
81 { 'name' => "allowimagesonly",
82 'desc' => "{PDFPlugin.allowimagesonly}",
83 'type' => "flag" },
84 { 'name' => "complex",
85 'desc' => "{PDFPlugin.complex}",
86 'type' => "flag" },
87 { 'name' => "nohidden",
88 'desc' => "{PDFPlugin.nohidden}",
89 'type' => "flag" },
90 { 'name' => "zoom",
91 'desc' => "{PDFPlugin.zoom}",
92 'deft' => "2",
93 'range' => "1,3", # actually the range is 0.5-3
94 'type' => "int" },
95 { 'name' => "use_sections",
96 'desc' => "{PDFPlugin.use_sections}",
97 'type' => "flag" },
98 { 'name' => "description_tags",
99 'desc' => "{HTMLPlugin.description_tags}",
100 'type' => "flag" }
101 ];
102
103my $options = { 'name' => "PDFPlugin",
104 'desc' => "{PDFPlugin.desc}",
105 'abstract' => "no",
106 'inherits' => "yes",
107 'srcreplaceable' => "yes", # Source docs in PDF can be replaced with GS-generated html
108 'args' => $arguments };
109
110sub new {
111 my ($class) = shift (@_);
112 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
113 push(@$pluginlist, $class);
114
115 push(@$inputargs,"-title_sub");
116 push(@$inputargs,'^(Page\s+\d+)?(\s*1\s+)?');
117
118 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
119 push(@{$hashArgOptLists->{"OptList"}},$options);
120
121 my $self = new PBConvertBinaryFile($pluginlist, $inputargs, $hashArgOptLists);
122
123 if ($self->{'info_only'}) {
124 # don't worry about any options etc
125 return bless $self, $class;
126 }
127
128 $self->{'filename_extension'} = "pdf";
129 $self->{'file_type'} = "PDF";
130
131 # these are passed through to gsConvert.pl by ConvertBinaryFile.pm
132 my $zoom = $self->{"zoom"};
133 $self->{'convert_options'} = "-pdf_zoom $zoom";
134 $self->{'convert_options'} .= " -pdf_complex" if $self->{"complex"};
135 $self->{'convert_options'} .= " -pdf_nohidden" if $self->{"nohidden"};
136 $self->{'convert_options'} .= " -pdf_ignore_images" if $self->{"noimages"};
137 $self->{'convert_options'} .= " -pdf_allow_images_only" if $self->{"allowimagesonly"};
138
139 # check convert_to
140 if ($self->{'convert_to'} eq "text" && $ENV{'GSDLOS'} =~ /^windows$/i) {
141 print STDERR "Windows does not support pdf to text. PDFs will be converted to HTML instead\n";
142 $self->{'convert_to'} = "html";
143 }
144 elsif ($self->{'convert_to'} eq "auto") {
145 # choose html ?? is this the best option
146 $self->{'convert_to'} = "html";
147 }
148 # set convert_to_plugin and convert_to_ext
149 $self->PBConvertBinaryFile::set_standard_convert_settings();
150
151 my $secondary_plugin_name = $self->{'convert_to_plugin'};
152 my $secondary_plugin_options = $self->{'secondary_plugin_options'};
153
154 if (!defined $secondary_plugin_options->{$secondary_plugin_name}) {
155 $secondary_plugin_options->{$secondary_plugin_name} = [];
156 }
157 my $specific_options = $secondary_plugin_options->{$secondary_plugin_name};
158
159 # following title_sub removes "Page 1" added by pdftohtml, and a leading
160 # "1", which is often the page number at the top of the page. Bad Luck
161 # if your document title actually starts with "1 " - is there a better way?
162 push(@$specific_options , "-title_sub", '^(Page\s+\d+)?(\s*1\s+)?');
163 my $associate_tail_re = $self->{'associate_tail_re'};
164 if ((defined $associate_tail_re) && ($associate_tail_re ne "")) {
165 push(@$specific_options, "-associate_tail_re", $associate_tail_re);
166 }
167 push(@$specific_options, "-file_rename_method", "none");
168
169 if ($secondary_plugin_name eq "HTMLPlugin") {
170 # pdftohtml always produces utf8
171 push(@$specific_options, "-input_encoding", "utf8");
172 push(@$specific_options, "-extract_language") if $self->{'extract_language'};
173 push(@$specific_options, "-processing_tmp_files");
174 # Instruct HTMLPlug (when eventually accessed through read_into_doc_obj)
175 # to extract these metadata fields from the HEAD META fields
176 if (defined $self->{'metadata_fields'} && $self->{'metadata_fields'} =~ /\S/) {
177 push(@$specific_options,"-metadata_fields",$self->{'metadata_fields'});
178 } else {
179 push(@$specific_options,"-metadata_fields","Title,GENERATOR,date,author<Creator>");
180 }
181 if (defined $self->{'metadata_field_separator'} && $self->{'metadata_field_separator'} =~ /\S/) {
182 push(@$specific_options,"-metadata_field_separator",$self->{'metadata_field_separator'});
183 }
184 if ($self->{'use_sections'} || $self->{'description_tags'}) {
185 $self->{'description_tags'} = 1;
186 push(@$specific_options, "-description_tags");
187 }
188 }
189 elsif ($secondary_plugin_name eq "PagedImagePlugin") {
190 push(@$specific_options, "-screenviewsize", "1000");
191 push(@$specific_options, "-enable_cache");
192 push(@$specific_options, "-processing_tmp_files");
193 }
194
195 $self = bless $self, $class;
196 $self->load_secondary_plugins($class,$secondary_plugin_options,$hashArgOptLists);
197 return $self;
198}
199
200sub get_default_process_exp {
201 my $self = shift (@_);
202
203 return q^(?i)\.pdf$^;
204}
205
206# so we don't inherit HTMLPlug's block exp...
207sub get_default_block_exp {
208 return "";
209}
210
211sub convert_post_process
212{
213 my $self = shift (@_);
214 my ($conv_filename) = @_;
215
216 my $outhandle=$self->{'outhandle'};
217
218 #$self->{'input_encoding'} = "utf8"; # The output is always in utf8 (is it?? it is for html, but what about other types?)
219 #my ($language, $encoding) = $self->textcat_get_language_encoding ($conv_filename);
220
221 # read in file ($text will be in utf8)
222 my $text = "";
223 # encoding will be utf8 for html files - what about other types? will we do this step for them anyway?
224 $self->read_file ($conv_filename, "utf8", "", \$text);
225
226 # Calculate number of pages based on <a ...> tags (we have a <a name=1> etc
227 # for each page). Metadata based on this calculation not set until process()
228 #
229 # Note: this is done even if we are not breaking to document into pages as it might
230 # be useful to give an indication of document length in browser through setting
231 # num_pages as metadata.
232 my @pages = ($text =~ /\<[Aa] name=\"?\w+\"?>/ig);
233 my $num_pages = scalar(@pages);
234 $self->{'num_pages'} = $num_pages;
235
236 if ($self->{'use_sections'}
237 && $self->{'converted_to'} eq "HTML") {
238
239 print $outhandle "PDFPlugin: Calculating sections...\n";
240
241 # we have "<a name=1></a>" etc for each page
242 # it may be <A name=
243 my @sections = split('<[Aa] name=', $text);
244
245 my $top_section = "";
246
247 if (scalar (@sections) == 1) { #only one section - no split!
248 print $outhandle "PDFPlugin: warning - no sections found\n";
249 } else {
250 $top_section .= shift @sections; # keep HTML header etc as top_section
251 }
252
253 # handle first section specially for title? Or all use first 100...
254
255 my $title = $sections[0];
256 $title =~ s/^\"?\w+\"?>//; # specific for pdftohtml...
257 $title =~ s/<\/([^>]+)><\1>//g; # (eg) </b><b> - no space
258 $title =~ s/<[^>]*>/ /g;
259 $title =~ s/(?:&nbsp;|\xc2\xa0)/ /g; # utf-8 for nbsp...
260 $title =~ s/^\s+//s;
261 $title =~ s/\s+$//;
262 $title =~ s/\s+/ /gs;
263 $title =~ s/^$self->{'title_sub'}// if ($self->{'title_sub'});
264 $title =~ s/^\s+//s; # in case title_sub introduced any...
265 $title = substr ($title, 0, 100);
266 $title =~ s/\s\S*$/.../;
267
268
269 if (scalar (@sections) == 1) { # no sections found
270 $top_section .= $sections[0];
271 @sections=();
272 } else {
273 $top_section .= "<!--<Section>\n<Metadata name=\"Title\">$title</Metadata>\n-->\n <!--</Section>-->\n";
274 }
275
276 # add metadata per section...
277 foreach my $section (@sections) {
278 # section names are not always just digits, may be like "outline"
279 $section =~ s@^\"?(\w+)\"?></a>@@; # leftover from split expression...
280
281 $title = $1; # Greenstone does magic if sections are titled digits
282 if (! defined($title) ) {
283 print STDERR "no title: $section\n";
284 $title = " "; # get rid of the undefined warning in next line
285 }
286 my $newsection = "<!-- from PDFPlugin -->\n<!-- <Section>\n";
287 $newsection .= "<Metadata name=\"Title\">" . $title
288 . "</Metadata>\n--><p>\n";
289 $newsection .= $section;
290 $newsection .= "<!--</Section>-->\n";
291 $section = $newsection;
292 }
293
294 $text=join('', ($top_section, @sections));
295 }
296
297 # turn any high bytes that aren't valid utf-8 into utf-8.
298 unicode::ensure_utf8(\$text);
299
300 # Write it out again!
301 $self->utf8_write_file (\$text, $conv_filename);
302}
303
304
305# do plugin specific processing of doc_obj for HTML type
306sub process {
307 my $self = shift (@_);
308 my ($pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;
309
310 my $result = $self->process_type($base_dir,$file,$doc_obj);
311
312 # fix up the extracted date metadata to be in Greenstone date format,
313 # and fix the capitalisation of 'date'
314 my $cursection = $doc_obj->get_top_section();
315 foreach my $datemeta (@{$doc_obj->get_metadata($cursection, "date")}) {
316 $doc_obj->delete_metadata($cursection, "date", $datemeta);
317
318 # We're just interested in the date bit, not the time
319 # some pdf creators (eg "Acrobat 5.0 Scan Plug-in for Windows")
320 # set a /CreationDate, and set /ModDate to 000000000. pdftohtml
321 # extracts the ModDate, so it is 0...
322 $datemeta =~ /(\d+)-(\d+)-(\d+)/;
323 my ($year, $month, $day) = ($1,$2,$3);
324 if (defined($year) && defined($month) && defined($day)) {
325 if ($year == 0) {next}
326 if ($year < 100) {$year += 1900} # just to be safe
327 if ($month =~ /^\d$/) {$month="0$month"} # single digit
328 if ($day =~ /^\d$/) {$day="0$day"} # single digit
329 my $date="$year$month$day";
330 $doc_obj->add_utf8_metadata($cursection, "Date", $date);
331 }
332 }
333
334 $doc_obj->add_utf8_metadata($cursection, "NumPages", $self->{'num_pages'});
335
336 if ($self->{'use_sections'} && $self->{'converted_to'} eq "HTML") {
337 # we explicitly make it a paged document, cos greenstone won't get it
338 # right if any section has an empty title, or one with letters in it
339 $doc_obj->set_utf8_metadata_element ($cursection, "gsdlthistype", "Paged");
340 }
341
342 return $result;
343}
344
3451;
Note: See TracBrowser for help on using the repository browser.