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

I have a strange problem that seems to revolve around the headers I am sending to the browser in order to properly send a PDF file. The script creates an Excel object, fills in a sheet with data, and then prints it out to PDF. From there, the PDF is opened, binmode'd and read into a scalar.

The following snippet is what I am currently using:

my ($offset,$bytes,$buffer,$contents); open(PDF, "<$print_pdf") || print_debug("Couldn't open file $print +_pdf");; $offset = 0; $contents = ''; binmode PDF; until(eof(PDF)) { $bytes += read(PDF,$buffer, 1048576, $offset); $offset += 1048576; $contents .= $buffer; } close PDF; print header( -type => 'application/pdf', -Content_Disposition => "inline; filename=charges.pdf", -Content_Length => "$bytes" ); #print "Content-Disposition: inline; filename=charges.pdf\n"; #print "Content-Length: $bytes\n"; #print "Content-Type: application/pdf\n\n"; #binmode STDOUT; print $contents;

The problem I'm facing, is that the browser (IE) "loads" the script three times (one URL click executes the script three times). Netscape 6 does it twice. So the script pops open three Excel objects, creates three PDF files, and ultimately sends back one PDF to the browser.

I have tried with/without the binmode STDOUT; line with no difference. I tried Using the lines that are commented out instead of the print header function (CGI.pm), and nothing I've tried seems to resolve the problem.

Have any fellow monks had any similar experiences?

ryddler

Replies are listed 'Best First'.
Re: Content headers for PDF?
by shotgunefx (Parson) on Jun 09, 2001 at 02:48 UTC
    What type of server is it?

    -Lee

    "To be civilized is to deny one's nature."
      Oops! I mentioned that it was IIS in CB. Guess you weren't there, huh? ;)

      ryddler
        I'm using Linux/Apache but I think I know what your problem is. I think it has to do with additional headers being added. I believe IIS determines automatically whether or not to add headers. Removing all the additional headers besides type and putting CGI.pm in nph mode fixed it for me. Opens in one window on IE 5.0 on a win32 machine.

        #!/usr/bin/perl use CGI qw(:all -nph ); $|++; my ($offset,$bytes,$buffer,$contents); open(PDF, "</tmp/pdftest.pdf") || die ("Couldn't open file $pr +int_pdf");; $offset = 0; $contents = ''; binmode PDF; until(eof(PDF)) { $bytes += read(PDF,$buffer, 1048576, $offset); $offset += 1048576; $contents .= $buffer; } close PDF; print header( -type => 'applicatio +n/pdf' ); print $contents;


        -Lee

        "To be civilized is to deny one's nature."