source: main/tags/2.40/gsdl/bin/script/pdftohtml.pl

Last change on this file was 4103, checked in by sjboddie, 21 years ago

Added a -nohidden PDFPlug option and made it pass the -hidden option to pdftohtml
by default.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 7.6 KB
Line 
1#!/usr/bin/perl -w
2
3
4###########################################################################
5#
6# pdftohtml.pl -- convert PDF documents to HTML format
7#
8# A component of the Greenstone digital library software
9# from the New Zealand Digital Library Project at the
10# University of Waikato, New Zealand.
11#
12# Copyright (C) 2001 New Zealand Digital Library Project
13#
14# This program is free software; you can redistribute it and/or modify
15# it under the terms of the GNU General Public License as published by
16# the Free Software Foundation; either version 2 of the License, or
17# (at your option) any later version.
18#
19# This program is distributed in the hope that it will be useful,
20# but WITHOUT ANY WARRANTY; without even the implied warranty of
21# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22# GNU General Public License for more details.
23#
24# You should have received a copy of the GNU General Public License
25# along with this program; if not, write to the Free Software
26# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27#
28###########################################################################
29
30# pdftohtml.pl is a wrapper for running pdftohtml utility which converts
31# PDF documents to HTML, and converts images to PNG format for display in
32# the HTML pages generated
33
34BEGIN {
35 die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
36 unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
37}
38
39use parsargv;
40use util;
41use Cwd;
42use File::Basename;
43
44sub print_usage {
45# note - we don't actually ever use most of these options...
46print STDERR
47 ("pdftohtml.pl wrapper for pdftohtml.\n",
48 "Usage: pdftohtml [options] <PDF-file> <html-file>\n",
49 "Options:\n",
50 "\t-i\tignore images (don't extract)\n",
51 "\t-a\tallow images only (continue even if no text is present)\n",
52 "\t-c\tproduce complex output (requires ghostscript)\n",
53 "\t-hidden\tExtract hidden text\n",
54 "\t-zoom\tfactor by which to zoom the PDF (only useful if -c is set)\n"
55 );
56exit (1);
57}
58
59sub main {
60 my (@ARGV) = @_;
61 my ($allow_no_text, $ignore_images, $complex, $zoom, $hidden);
62
63 # read command-line arguments so that
64 # you can change the command in this script
65 if (!parsargv::parse(\@ARGV,
66 'a', \$allow_no_text,
67 'i', \$ignore_images,
68 'c', \$complex,
69 'hidden', \$hidden,
70 'zoom/\d+/2', \$zoom,
71 ))
72 {
73 print_usage();
74 }
75
76 # Make sure the input file exists and can be opened for reading
77 if (scalar(@ARGV) != 2) {
78 print_usage();
79 }
80
81 my $input_filename = $ARGV[0];
82 my $output_filestem = $ARGV[1];
83
84 $output_filestem =~ s/\.html$//i; # pdftohtml adds this suffix
85
86 # test that the directories exist to create the output file, or
87 # we should exit immediately. (File:: is included by util.pm)
88 my $output_dir = File::Basename::dirname($output_filestem);
89 if (! -d $output_dir || ! -w $output_dir) {
90 die "pdftohtml.pl: cannot write to directory $output_dir\n";
91 }
92
93 my @dir = split (/(\/|\\)/, $input_filename);
94 my $input_basename = pop(@dir);
95 $input_basename =~ s/\.pdf//i;
96 my $dir = join ("", @dir);
97
98 if (!-r $input_filename) {
99 print STDERR "Error: unable to open $input_filename for reading\n";
100 exit(1);
101 }
102
103 # Heuristical code removed due to pdftohtml being "fixed" to not
104 # create bitmaps for each char in some pdfs. However, this means we
105 # now create .html files even if we can't extract any text. We should
106 # check for that now instead someday...
107
108
109 # formulate the command
110 my $cmd = &util::filename_cat($ENV{'GSDLHOME'}, "bin", $ENV{'GSDLOS'}, "pdftohtml");
111
112 # don't include path on windows (to avoid having to play about
113 # with quoting when GSDLHOME might contain spaces) but assume
114 # that the PATH is set up correctly.
115 $cmd = "pdftohtml" if ($ENV{'GSDLOS'} =~ /^windows$/);
116
117 $cmd .= " -i" if ($ignore_images);
118 $cmd .= " -c" if ($complex);
119 $cmd .= " -hidden" if ($hidden);
120 $cmd .= " -zoom $zoom";
121 $cmd .= " -noframes -p -enc UTF-8 \"$input_filename\" \"$output_filestem.html\"";
122
123# system() returns -1 if it can't run, otherwise it's $cmds ret val.
124 # note we return 0 if the file is "encrypted"
125 $!=0;
126 if (system($cmd)!=0) {
127 print STDERR "pdftohtml error for $input_filename $!\n";
128 # leave these for gsConvert.pl...
129 #&util::rm("$output_filestem.text") if (-e "$output_filestem.text");
130 #&util::rm("$output_filestem.err") if (-e "$output_filestem.err");
131 return 1;
132 }
133
134 if (! -e "$output_filestem.html") {
135 return 1;
136 }
137
138# post-process to remove </b><b> and </i><i>, as these break up
139# words, screwing up indexing and searching.
140# At the same time, check that our .html file has some textual content.
141 &util::mv("$output_filestem.html","$output_filestem.html.tmp");
142 $!=0;
143 open INFILE, "$output_filestem.html.tmp" ||
144 die "Couldn't open file: $!";
145 open OUTFILE, ">$output_filestem.html" ||
146 die "Couldn't open file for writing: $!";
147 my $line;
148 my $seen_textual_content=$allow_no_text;
149 while ($line=<INFILE>) {
150 $line =~ s#</b><b>##g;
151 $line =~ s#</i><i>##g;
152 $line =~ s#\\#\\\\#g; # until macro language parsing is fixed...
153# check for any extracted text
154 if ($seen_textual_content == 0) {
155 my $tmp_line=$line;
156 $tmp_line =~ s/<[^>]*>//g;
157 $tmp_line =~ s/Page\s\d+//;
158 $tmp_line =~ s/\s*//g;
159 if ($tmp_line ne "") {
160 $seen_textual_content=1;
161 }
162 }
163
164 # relative hrefs to own document...
165 $line =~ s@href=\"$input_basename\.html\#@href=\"\#@go;
166# escape underscores, but not if they're inside tags (eg img/href names)
167 my $inatag = 0; # allow multi-line tags
168 if ($line =~ /_/) {
169 my @parts=split('_',$line);
170 my $lastpart=pop @parts;
171 foreach my $part (@parts) {
172 if ($part =~ /<[^>]*$/) { # if we're starting a tag...
173 $inatag=1;
174 } elsif ($part =~ />[^<]*$/) { # closing a tag
175 $inatag=0;
176 }
177 if ($inatag) {
178 $part.='_';
179 } else {
180 $part.="&#95;";
181 }
182 }
183 $line=join('',@parts,$lastpart);
184 }
185
186 print OUTFILE $line;
187 }
188 close INFILE;
189 close OUTFILE;
190 &util::rm("$output_filestem.html.tmp");
191
192 # Need to convert images from PPM format to PNG format
193 my @images;
194
195 my $directory=$output_filestem;
196 $directory =~ s@[^\/]*$@@; # assume filename has no embedded slashes...
197
198 if (open (IMAGES, "${directory}images.log") ||
199 open (IMAGES, "${directory}image.log")) {
200 while (<IMAGES>) {
201 push (@images, $_);
202 }
203 close IMAGES;
204 &util::rm("${directory}image.log") if (-e "${directory}image.log");
205
206 }
207
208 # no need to go any further if there is no text extracted from pdf.
209 if ($seen_textual_content == 0) {
210 print STDERR "Error: PDF contains no extractable text\n";
211 # remove images...
212 for $image (@images) {
213 chomp($image);
214 &util::rm("${directory}$image");
215 }
216 return 1;
217 }
218
219
220
221 for $image (@images) {
222 chomp($image);
223 my $cmd = "";
224 if ($ENV{'GSDLOS'} =~ /^windows/i) {
225 $cmd = "pnmtopng \"${directory}$image\"";
226 if (system($cmd)!=0) {
227 print STDERR "Error executing $cmd\n";
228 #return 1; # not sure about whether to leave this one in or take it out
229 next;
230 }
231 } else {
232 my @nameparts = split(/\./, $image);
233 my $image_base = shift(@nameparts);
234 $cmd = "pnmtopng \"${directory}$image\" > \"${directory}$image_base.png\" 2>/dev/null";
235 if (system($cmd)!=0) {
236 $cmd = "convert \"${directory}$image\" \"${directory}$image_base.png\" 2>/dev/null";
237 if (system($cmd)!=0) {
238 print STDERR "Cannot convert $image into PNG format (tried `pnmtopng' and `convert')...\n";
239 #return 1; # not sure about whether to leave this one in or take it out
240 next;
241 }
242 }
243 }
244 &util::rm($image);
245 }
246
247 return 0;
248}
249
250# indicate our error status, 0 = success
251exit (&main(@ARGV));
252
Note: See TracBrowser for help on using the repository browser.