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

My CGI form submits a whole load of values, I do stuff with the values and then send some output to the user - the catch is the output is in the form of a PDF file. So at the end of my script, after I've done stuff with the input, I have
use CGI qw/:standard/; open PDF, "test.pdf" or die "cd not open test.pdf"; my $buffer; read PDF, $buffer, -s(PDF); print header( -type => "application/pdf\n\n", ), $buffer;
I shd add that the "action" in the form is "my_file.pl?ext=.pdf" - that is, I think I've done all this good stuff to stop IE5 getting confused. And test.pdf is a real PDF file - if I link directly to it, I see what I expect to see. BUT the output from the above code is not a PDF file, but the "source code" of the PDF. I have a feeling I must be doing something very simple wrong... can any kind monk tell me what?

§ George Sherston

Replies are listed 'Best First'.
Re: Outputting PDF via CGI
by zengargoyle (Deacon) on Mar 23, 2002 at 00:19 UTC

    perldoc CGI says...

    print $q->header(-type=>'image/gif',-expires=>'+3d');

    So... try:

    print header(-type=>'application/pdf'),$buffer;

    If you're on a Winbloze box you might need to binmode the PDF file.

Whoop! Whoop! Whoop! Header Oversight Alert!
by George_Sherston (Vicar) on Mar 23, 2002 at 00:24 UTC
    Sorry chaps... I misthought. Further up the script I use some of my own modules, and in one of those modules I have a
    print header;
    for debugging purposes... which was overriding my nice application/pdf header further down, hence the garbage. So, for the record, my snippet above DOES do what it says on the can... if you have the right can opener. Thank you for listening.

    § George Sherston