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