views:

511

answers:

3

I have a form button that submits the data to generic script (the same page that the form is loaded from, index.php) and if the right POST variables are submitted to that script, it returns a PDF for the user to print (with the relevant form info loaded, and other DB info, etc).

The problem is that no matter what I set the content-type and the content-disposition to in the header, Firefox prompts the user to either save or open the document.

This wouldn't be so bad if Firefox offered it's own Adobe Reader plugin as an "Open With" option, but it only offers the actual Adobe Reader App (which causes problems due to authentication and default browser settings being out of my control, etc).

So is there something server side I need to change (either in the .htaccess file or in the actual header) to have Firefox get the message that response is both a PDF with the application/pdf MIME Type and thus should treat the file as though it were a hard link to a PDF, not a server-generated stream?

One last thing...

If I set the Content-Disposition to "inline" Firefox opens the file in Firefox, but only before loading the page as "index.php" first. Once the user gets to the PDF, if they try clicking the back button, they hit the index.php that loaded the PDF, not the one that they submitted the data. On the Mac side of things, Firefox doesn't load the PDF but instead prompts the user to save or open "index.php" as though it were an attachment.

Thanks!


Currently the code I've tried (in this order):

This one gets treated as a file attachment, thus the Mime-Type rule for Firefox is ignored for the file extension rule:

    header("Content-Type: application/pdf");
    header('Content-Disposition:attachment; filename="testing.pdf"');

This one works but loads index.php first and then loads the file, which is unattractive, confusing, and puts an extra request (and thus an extra page) between the pdf and the original page:

    header("Content-Type: application/pdf");
    header('Content-Disposition:inline; filename="testing.pdf"');

This one gets treated the same as attachment (in Firefox) and thus only prompts to save or open in Adobe Reader rather than defaulting to Adobe Reader plugin:

    header("Content-Type: application/pdf");
    header('Content-Disposition:pdf; filename="testing.pdf"');

Also notice that in the last example I have tried both pdf and application\pdf as the content disposition. No difference.

The only thing I haven't tried yet is setting the type as ocet/stream. I'll cross my fingers but won't hold my breath.

A: 

I'm thinking that Firefox is having a hard time handling the PDF as a response to a POST, could you try generating a link to the PDF and having them click on the link, the link could contain GET variables like blah.php?VAR=123 so you can still dynamically generate the PDF.

MindStalker
A: 

The mime-type application/octet-stream should force the browser to download it. I can't tell if that's what you want from your post.

R. Bemrose
Sorry. I'm having the oposite problem (I think). I want it to treat it as it would a link to a static PDF, meaning that if the user has it set to open with the PDF Reader Plugin (rather than offering the save dialog), the PDF gets opened. Right now it's treating it like it's download only.
Anthony
+1  A: 

You could try having the link end in ".pdf", and use something like mod_rewrite to map it to your PHP script.

Frank Schmitt
I just tried this in my sandbox and it definitely seems promising. I'm really hesitant since this opens up a security vulnerability and because it adds yet another layer of (for lack of a better word) sneakiness on how the file is actually getting generated. But it definitely worked, thanks! +1 for the workaround. I'll mark this the answer if a much simpiler solution isn't suggested.
Anthony