7

I am creating a PDF file from raw binary data and it's working perfectly but because of the headers that I define in my PHP file it prompts the user either to "save" the file or "open with". Is there any way that I can save the file on local server somewhere here http://localhost/pdf?

Below are the headers I have defined in my page

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: application/pdf");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Transfer-Encoding: binary");
Linus Kleen
  • 33,871
  • 11
  • 91
  • 99
Salman
  • 145
  • 2
  • 2
  • 9

2 Answers2

12

If you would like to save the file on the server rather than have the visitor download it, you won't need the headers. Headers are for telling the client what you are sending them, which is in this case nothing (although you are likely displaying a page linking to you newly created PDF or something).

So, instead just use a function such as file_put_contents to store the file locally, eventually letting your web server handle file transfer and HTTP headers.

// Let's say you have a function `generate_pdf()` which creates the PDF,
// and a variable $pdf_data where the file contents are stored upon creation
$pdf_data = generate_pdf();

// And a path where the file will be created
$path = '/path/to/your/www/root/public_html/newly_created_file.pdf';

// Then just save it like this
file_put_contents( $path, $pdf_data );

// Proceed in whatever way suitable, giving the user feedback if needed 
// Eg. providing a download link to http://localhost/newly_created_file.pdf
Simon
  • 3,667
  • 1
  • 35
  • 49
  • 1
    where function generate_pdf()? – sk juli kaka Jun 05 '15 at 04:14
  • 1
    @skjulikaka: As the question is not about creating PDF data using PHP, but rather about how to save such data that is out of scope. The first line in my code example also clearly states "***Let's say** you have a function `generate_pdf()` which creates the PDF*". But I'm sure you can find a suitable answer by [searching for](http://stackoverflow.com/search?q=generate+%5Bpdf%5D+using+%5Bphp%5D) "generate [tag:pdf] using [tag:php]" or something along those lines. – Simon Jun 05 '15 at 07:20
  • yes I meet many library for `generate php content to pdf`but it's still can not acceptable because my language is not understanding with pdf :( ,, I crazy now(); – sk juli kaka Jun 05 '15 at 09:35
  • This worked for me, using the fpdf library, $pdf_data = $pdf->Output(); These 3 lines allow me to save the .pdf so I can attach it to an email (in subsequent script). – Jason Oct 09 '18 at 14:51
0

You can use output control functions. Place ob_start() at beginning of your script. At the end use ob_get_contents() and save the content to a local file.

After that you can use ob_end_clean() or ob_end_flush() depending on whether you want to output PDF to browser as well, or you would redirect user to some other page. If you use ob_end_flush() make sure you set the headers before flushing the data.

Milan Babuškov
  • 59,775
  • 49
  • 126
  • 179
  • If you already have the PDF data in memory, wouldn't output buffering be a bit like printing a file only to scan it in again? Even if you wanted to save it to disk at the same time as letting the user download it, couldn't you just 1. save it, 2. send headers, 3. print out the PDF data? – Simon Feb 15 '12 at 15:33
  • It depends which library are you using to create PDF file. Some libraries are only able to output to the browser directly, so you cannot "just save it". – Milan Babuškov Feb 17 '12 at 19:42