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

Last change on this file since 10280 was 10273, checked in by chi, 19 years ago

A modification to allow a secondary-plugin setting through ConvertToPlug.

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