Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Opening PDF through CGI

by blazar (Canon)
on May 26, 2006 at 15:10 UTC ( [id://551885]=note: print w/replies, xml ) Need Help??


in reply to Opening PDF through CGI

At a first glance it should work.

open(FIN,"< $full_path") || print "File Not Found"; binmode FIN; $buffer = join '', <FIN>; close(FIN); print $buffer;

BTW: how 'bout:

open my $fin, '<:raw', $full_path or die WHATEVER; local \$; # or use File::Slurp print <$fin>;

Update:The following appears to work:

#!/usr/bin/perl -T use strict; use warnings; use CGI ':standard'; use CGI::Carp 'fatalsToBrowser'; print header('application/pdf'), do { open my $fh, '<:raw', 'minimal.pdf' or die "D'Oh! $!\n"; local $\; <$fh>; }; __END__

(it is temporarily available at http://blazar.perlmonk.org/tmp/minimal.pl)

Replies are listed 'Best First'.
Re^2: Opening PDF through CGI
by nobull (Friar) on May 26, 2006 at 16:58 UTC
    print header('application/pdf'), do { open my $fh, '<:raw', 'minimal.pdf' or die "D'Oh! $!\n"; local $\; <$fh>; };

    Due to the funny way CGI::header() operates under mod_perl it is advisable to always print the result of CGI::header immediately and never defer it. In this particular case it probably doesn't matter but one day it may.

    print header('application/pdf'); { open my $fh, '<:raw', 'minimal.pdf' or die "D'Oh! $!\n"; local $\; print <$fh>; };

    Note: the above code slurps the whole PDF into memory. I prefer to use File::Copy

    use File::Copy 'copy'; print header('application/pdf'); copy 'minimal.pdf', \*STDOUT or die "D'Oh! $!\n";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://551885]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-03-29 09:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found