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

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

made the -input_encoding=utf8 always be set for htmlplug secondary plugin

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