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

I have a simple Perl script that uses PDF::API2 to generate a PDF file from my data; with the help of CGI that PDF file is generated on the web server and sent to the user in their browser where it shows up as a downloaded file.
print CGI::header( -type => 'application/x-pdf', -attachment => 'Calendar.pdf' ); my $pdf = PDF::API2->new(); $pdf->saveas('-');
Life is good. But I have seen sites where the generated PDF shows up inside the browser and there are nifty "print" and "save" buttons. What is the recommended/best way to modify my script to do this?

Replies are listed 'Best First'.
Re: Creating inline PDF?
by thomas895 (Deacon) on May 12, 2015 at 01:14 UTC

    From the CGI documentation regarding the use of -attachment:

    Instead of displaying the page, some browsers will prompt the user to save it to disk
    Suggested fix: Remove the -attachment option.

    There will be browsers that don't have the PDF reader extension properly configured (or set to always download): they will still have it downloaded.

    I imagine you might want to suggest a name to the browser for if they choose to download it. I've always done it with path information, linking to the CGI script as: /cgi-bin/myscript.cgi/something.pdf, where myscript.cgi is of course the script itself. You can ignore the path info, no modifications are needed to your script.

    -Thomas
    "Excuse me for butting in, but I'm interrupt-driven..."

      Instead of the -attachment option, the OP should use the content-disposition header field with a value of inline -- this will generally be respected by browsers. I more often use this to have browsers download the file with the name I want, but this use is within spec.

      Those clients that don't respect content-disposition will, as you point out, guess what to do based upon the file extension. Rather than adding unnecessary misdirection of a fake path, why not just call your script myscript.pdf? The server should be interpreting the hash bang, and so who cares what it's called?


      #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

        The server should be interpreting the hash bang, and so who cares what it's called?
        Depends on the server configuration. It's also probably less confusing for others in the future. It works just fine in almost all cases, so OP has a choice.

        -Thomas
        "Excuse me for butting in, but I'm interrupt-driven..."