source: trunk/gsdl/perllib/plugins/PDFPlug.pm@ 12834

Last change on this file since 12834 was 12834, checked in by kjdon, 18 years ago

these convertto plugins were all setting extract_language=1 to their secondary plugins. we don't want this - only pass to secondary plugin if user has asked for it. textcat can be very slow, so don't want to run it unless we have to

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