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

Last change on this file since 10549 was 10514, checked in by kjdon, 19 years ago

added in description_tags option, as it wasn't valid cos no longer inherit from HTMLPlug.

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