Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Opening PDF through CGI

by sanPerl (Friar)
on May 26, 2006 at 14:51 UTC ( #551879=perlquestion: print w/replies, xml ) Need Help??

sanPerl has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,
I am new to CGI. I want to write an application which will open PDF file in a browser. I have written following code.
use strict; my $image_dir="D:\\my"; display_pdf("in.pdf"); sub display_pdf{ my $file = shift; my $full_path = "$image_dir\\$file"; my $buffer=""; print "Content-type: application/pdf\n\n"; open(FIN,"< $full_path") || print "File Not Found"; binmode FIN; $buffer = join '', <FIN>; close(FIN); print $buffer; }
I am Not able to open PDF-reader and PDF document when I use this code. I would be glad if someone could provide me some hints/clues to get my job done.
Sandeep
Update: Thanks to all for suggestions. blazer's code is working fine here. I am checking with other suggestions.

Replies are listed 'Best First'.
Re: Opening PDF through CGI
by blazar (Canon) on May 26, 2006 at 15:10 UTC

    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)

      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";
Re: Opening PDF through CGI
by ForgotPasswordAgain (Priest) on May 26, 2006 at 15:35 UTC
    You might also need a Content-Length header, since otherwise the browser wouldn't know when to stop reading. So try throwing in a length($buffer).
Re: Opening PDF through CGI
by nobull (Friar) on May 26, 2006 at 16:50 UTC
    I agree that at first glance that should work.

    Perhaps ot would help if you told us in what way it fails.

    Note, rather than passing the string "File Not Found" as the content of the PDF entity body if open() fails for any reason perhaps you should log an error containing the actual reason.

    open(FIN,'<',$full_path") or die $!;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://551879]
Approved by idsfa
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2021-05-14 17:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (150 votes). Check out past polls.

    Notices?