1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
|
package pdf2html;
use strict;
=pod
=head1 NAME
pdf2html - swish-e sample module to convert pdf to html
=head1 SYNOPSIS
use pdf2html;
my $html_record_ref = pdf2html( $pdf_file_name, 'title' );
# or by passing content in a scalar reference
my $html_text_ref = pdf2html( \$pdf_content, 'title' );
=head1 DESCRIPTION
Sample module for use with other swish-e 'prog' document source programs.
Pass either a file name, or a scalar reference.
The differece is when you pass a reference to a scalar
only the content is returned. When you pass a file name
an entire record is returned ready to be fed to swish -- this
includes the headers required by swish for indexing.
The second optional parameter is the extracted PDF info tag to use as the HTML title.
The plan is to find a library that will do this to avoid forking an external
program.
=head1 REQUIREMENTS
Uses the xpdf package that includes the pdftotext conversion program.
This is available from http://www.foolabs.com/xpdf/xpdf.html.
You will also need the module File::Temp (and its dependencies)
available from CPAN if passing content to this module (instead of a file name).
=head1 AUTHOR
Bill Moseley
=cut
use Symbol;
use vars qw(
@ISA
@EXPORT
$VERSION
);
# $Id: pdf2html.pm,v 1.8 2003/06/12 04:00:45 whmoseley Exp $
$VERSION = sprintf '%d.%02d', q$Revision: 1.8 $ =~ /: (\d+)\.(\d+)/;
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(pdf2html);
if ( $0 eq 'pdf2html.pm' ) {
my $file = shift || die "Usage: perl pdf2html.pm file.pdf [title tag]\n";
my $title = shift;
print ${pdf2html( $file, $title )};
}
sub pdf2html {
my $file_or_content = shift;
my $title_tag = shift;
my $file = ref $file_or_content
? create_temp_file( $file_or_content )
: $file_or_content;
my $metadata = get_pdf_headers( $file );
my $headers = format_metadata( $metadata );
if ( $title_tag && exists $metadata->{ $title_tag } ) {
my $title = escapeXML( $metadata->{ $title_tag } );
$headers = "<title>$title</title>\n" . $headers
}
# Check for encrypted content
my $content_ref;
# patch provided by Martial Chartoire
if ( $metadata->{encrypted} && $metadata->{encrypted} =~ /yes\.*\scopy:no\s\.*/i ) {
$content_ref = \'';
} else {
$content_ref = get_pdf_content_ref( $file );
}
my $txt = <<EOF;
<html>
<head>
$headers
</head>
<body>
<pre>
$$content_ref
</pre>
</body>
</html>
EOF
if ( ref $file_or_content ) {
# unlink $file;
return \$txt;
}
my $mtime = (stat $file )[9];
my $size = length $txt;
my $ret = <<EOF;
Content-Length: $size
Last-Mtime: $mtime
Path-Name: $file
EOF
$ret .= $txt;
return \$ret;
}
sub get_pdf_headers {
my $file = shift;
my $sym = gensym;
# This doesn't work
my $path = $file;
for ( $path ) {
s/"/\\"/g;
$path = qq["$path"];
}
open $sym, "pdfinfo $path |" || die "$0: Failed to open $file $!";
my %metadata;
while (<$sym>) {
if ( /^\s*([^:]+):\s+(.+)$/ ) {
my ( $metaname, $value ) = ( lc( $1 ), $2 );
$metaname =~ tr/ /_/;
$metadata{$metaname} = $value;
}
}
close $sym or warn "$0: Failed close on pipe to pdfinfo for $file: $?";
return \%metadata;
}
sub format_metadata {
my $metadata = shift;
my $metas = join "\n", map {
qq[<meta name="$_" content="] . escapeXML( $metadata->{$_} ) . '">';
} sort keys %$metadata;
return $metas;
}
sub get_pdf_content_ref {
my $file = shift;
# This doesn't work
my $path = $file;
for ( $path ) {
s/"/\\"/g;
$path = qq["$path"];
}
my $sym = gensym;
open $sym, "pdftotext $path - |" or die "$0: failed to run pdftotext: $!";
local $/ = undef;
my $content = escapeXML(<$sym>);
close $sym or warn "$0: Failed close on pipe to pdftotext for $file: $?";
return \$content;
}
# How are URLs printed with pdftotext?
sub escapeXML {
my $str = shift;
for ( $str ) {
s/&/&/go;
s/"/"/go;
s/</</go;
s/>/>/go;
}
return $str;
}
# This is the portable way to do this, I suppose.
# Otherwise, just create a file in the local directory.
sub create_temp_file {
my $scalar_ref = shift;
require "File/Temp.pm";
my ( $fh, $file_name ) = File::Temp::tempfile();
print $fh $$scalar_ref or die $!;
close $fh or die "Failed to close '$file_name' $!";
return $file_name;
}
1;
|