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 | ###########################################################################
|
---|
25 | use strict;
|
---|
26 | no strict 'refs'; # so we can use a var for filehandles (eg STDERR)
|
---|
27 |
|
---|
28 | package PDFPlug;
|
---|
29 |
|
---|
30 | use ConvertToPlug;
|
---|
31 | use unicode;
|
---|
32 |
|
---|
33 | sub BEGIN {
|
---|
34 | @PDFPlug::ISA = ('ConvertToPlug');
|
---|
35 | }
|
---|
36 |
|
---|
37 | my $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 |
|
---|
65 | my $options = { 'name' => "PDFPlug",
|
---|
66 | 'desc' => "{PDFPlug.desc}",
|
---|
67 | 'abstract' => "no",
|
---|
68 | 'inherits' => "yes",
|
---|
69 | 'args' => $arguments };
|
---|
70 |
|
---|
71 | sub 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 | # pdftohtml will always produce html files encoded as utf-8
|
---|
97 | if ($self->{'input_encoding'} eq "auto") {
|
---|
98 | $self->{'input_encoding'} = "utf8";
|
---|
99 | $self->{'extract_language'} = 1;
|
---|
100 | }
|
---|
101 |
|
---|
102 | return bless $self, $class;
|
---|
103 | }
|
---|
104 |
|
---|
105 | sub get_default_process_exp {
|
---|
106 | my $self = shift (@_);
|
---|
107 |
|
---|
108 | return q^(?i)\.pdf$^;
|
---|
109 | }
|
---|
110 |
|
---|
111 | # so we don't inherit HTMLPlug's block exp...
|
---|
112 | sub get_default_block_exp {
|
---|
113 | return "";
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 | # do plugin specific processing of doc_obj for HTML type
|
---|
118 | sub process {
|
---|
119 | my $self = shift (@_);
|
---|
120 | #my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;
|
---|
121 |
|
---|
122 | my $outhandle=$self->{'outhandle'};
|
---|
123 |
|
---|
124 | my $textref=$_[0];
|
---|
125 |
|
---|
126 | if ($self->{'use_sections'}
|
---|
127 | && $self->{'converted_to'} eq "HTML") {
|
---|
128 |
|
---|
129 | print $outhandle "PDFPlug: Calculating sections...\n";
|
---|
130 |
|
---|
131 | # we have "<a name=1></a>" etc for each page
|
---|
132 | # it may be <A name=
|
---|
133 | my @sections = split('<[Aa] name=', $$textref);
|
---|
134 |
|
---|
135 | if (scalar (@sections) == 1) { #only one section - no split!
|
---|
136 | print $outhandle "PDFPlug: warning - no sections found\n";
|
---|
137 | } else {
|
---|
138 | shift @sections; # don't need HTML header, etc
|
---|
139 | }
|
---|
140 |
|
---|
141 | # handle first section specially for title? Or all use first 100...
|
---|
142 |
|
---|
143 | my $title = $sections[0];
|
---|
144 | $title =~ s/^\"?\w+\"?>//; # specific for pdftohtml...
|
---|
145 | $title =~ s/<\/([^>]+)><\1>//g; # (eg) </b><b> - no space
|
---|
146 | $title =~ s/<[^>]*>/ /g;
|
---|
147 | $title =~ s/(?: |\xc2\xa0)/ /g; # utf-8 for nbsp...
|
---|
148 | $title =~ s/^\s+//s;
|
---|
149 | $title =~ s/\s+$//;
|
---|
150 | $title =~ s/\s+/ /gs;
|
---|
151 | $title =~ s/^$self->{'title_sub'}// if ($self->{'title_sub'});
|
---|
152 | $title =~ s/^\s+//s; # in case title_sub introduced any...
|
---|
153 | $title = substr ($title, 0, 100);
|
---|
154 | $title =~ s/\s\S*$/.../;
|
---|
155 |
|
---|
156 | my $top_section;
|
---|
157 | if (scalar (@sections) == 1) { # no sections found
|
---|
158 | $top_section=$sections[0];
|
---|
159 | @sections=();
|
---|
160 | } else {
|
---|
161 | $top_section = "<!--<Section>\n<Metadata name=\"Title\">$title</Metadata>\n-->\n <!--</Section>-->\n";
|
---|
162 | }
|
---|
163 |
|
---|
164 | # add metadata per section...
|
---|
165 | foreach my $section (@sections) {
|
---|
166 | # section names are not always just digits, may be like "outline"
|
---|
167 | $section =~ s@^\"?(\w+)\"?></a>@@; # leftover from split expression...
|
---|
168 |
|
---|
169 | $title = $1; # Greenstone does magic if sections are titled digits
|
---|
170 | if (! defined($title) ) {
|
---|
171 | print STDERR "no title: $section\n";
|
---|
172 | $title = " "; # get rid of the undefined warning in next line
|
---|
173 | }
|
---|
174 | my $newsection = "<!-- from PDFPlug -->\n<!-- <Section>\n";
|
---|
175 | $newsection .= "<Metadata name=\"Title\">" . $title
|
---|
176 | . "</Metadata>\n--><p>\n";
|
---|
177 | $newsection .= $section;
|
---|
178 | $newsection .= "<!--</Section>-->\n";
|
---|
179 | $section = $newsection;
|
---|
180 | }
|
---|
181 |
|
---|
182 | $$textref=join('', ($top_section, @sections));
|
---|
183 | }
|
---|
184 |
|
---|
185 | # turn any high bytes that aren't valid utf-8 into utf-8.
|
---|
186 | unicode::ensure_utf8($textref);
|
---|
187 |
|
---|
188 | print $outhandle "PDFPlug: passing $_[3] on to $self->{'converted_to'}Plug\n"
|
---|
189 | if $self->{'verbosity'} > 1;
|
---|
190 | print STDERR "<Processing n='$_[3]' p='PDFPlug'>\n" if ($_[6]);
|
---|
191 |
|
---|
192 | # tell htmlplug to extract these metadata fields from the HEAD META fields
|
---|
193 | $self->{'metadata_fields'} .= ",date,author<Creator>";
|
---|
194 |
|
---|
195 | my $result = ConvertToPlug::process_type($self,"pdf",@_);
|
---|
196 |
|
---|
197 | #my $doc_obj = pop(@_);
|
---|
198 | my $doc_obj = $_[5];
|
---|
199 | # fix up the extracted date metadata to be in Greenstone date format,
|
---|
200 | # and fix the capitalisation of 'date'
|
---|
201 | my $cursection = $doc_obj->get_top_section();
|
---|
202 |
|
---|
203 | foreach my $datemeta (@{$doc_obj->get_metadata($cursection, "date")}) {
|
---|
204 | $doc_obj->delete_metadata($cursection, "date", $datemeta);
|
---|
205 |
|
---|
206 | # We're just interested in the date bit, not the time
|
---|
207 | # some pdf creators (eg "Acrobat 5.0 Scan Plug-in for Windows")
|
---|
208 | # set a /CreationDate, and set /ModDate to 000000000. pdftohtml
|
---|
209 | # extracts the ModDate, so it is 0...
|
---|
210 | $datemeta =~ /(\d+)-(\d+)-(\d+)/;
|
---|
211 | my ($year, $month, $day) = ($1,$2,$3);
|
---|
212 | if (defined($year) && defined($month) && defined($day)) {
|
---|
213 | if ($year == 0) {next}
|
---|
214 | if ($year < 100) {$year += 1900} # just to be safe
|
---|
215 | if ($month =~ /^\d$/) {$month="0$month"} # single digit
|
---|
216 | if ($day =~ /^\d$/) {$day="0$day"} # single digit
|
---|
217 | my $date="$year$month$day";
|
---|
218 | $doc_obj->add_utf8_metadata($cursection, "Date", $date);
|
---|
219 | }
|
---|
220 | }
|
---|
221 |
|
---|
222 | # Add NumPages metadata (we have "<a name=1>" etc for each page)
|
---|
223 | my @pages = ($$textref =~ /\<[Aa] name=\"?\w+\"?>/ig);
|
---|
224 | $doc_obj->add_utf8_metadata($cursection, "NumPages", scalar(@pages));
|
---|
225 |
|
---|
226 | if ($self->{'use_sections'} && $self->{'converted_to'} eq "HTML") {
|
---|
227 | # we explicitly make it a paged document, cos greenstone won't get it
|
---|
228 | # right if any section has an empty title, or one with letters in it
|
---|
229 | $doc_obj->set_utf8_metadata_element ($cursection, "gsdlthistype", "Paged");
|
---|
230 | }
|
---|
231 | return $result;
|
---|
232 | }
|
---|
233 |
|
---|
234 | 1;
|
---|