1

I am generating PDF file by using FPDF library with PHP. PDF file getting generated and showing in browser perfectly.

However instead of it showing in browser I want forcefully save option instead of showing it.

user1806296
  • 41
  • 2
  • 11
  • May help http://stackoverflow.com/questions/5289610/server-downloading-file-instead-of-showing-it and http://stackoverflow.com/questions/2316610/php-files-are-downloaded-by-browser-instead-of-processed-by-local-dev-server-ma – PiLHA Jul 29 '13 at 14:01
  • @PiLHA, those SO links are for problems with the server not showing scripts correctly, he wants to force a download of a file instead of letting a browser plugin show it. – Patrick Evans Jul 29 '13 at 14:04

3 Answers3

1

FPDF has a download function build in, use the D argument:

// At the end of your PDF generation code:

$pdf->Output('file.pdf', 'D');
seymar
  • 3,993
  • 6
  • 25
  • 30
  • Change that line to: `$pdf->Output('file.pdf', 'D');` The `file.pdf` is just how the file will be called when downloaded by the user. – seymar Jul 29 '13 at 14:10
0

To do this, you need to send the appropriate content header:

<?php
header('Content-Type: application/pdf');
header('Content-disposition: attachment;filename=MyFile.pdf');
readfile('MyFile.pdf');

To implement this, simply cause the user's browser to open a page with the above headers, where MyFile.pdf refers to your pdf file's location (you can use cookies, sessions, form submissions, or whatever to figure out that file location). In this example I left off the closing ?> tag; this may or may not be neccessary.

IanPudney
  • 5,941
  • 1
  • 24
  • 39
0

Yeah, you can do this with .htaccess as well.

Just add the following line: AddType application/octet-stream .pdf

TN888
  • 7,659
  • 9
  • 48
  • 84