Save pdf to local server
Asked Answered
F

2

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");
Fullgrown answered 15/2, 2012 at 12:43 Comment(0)
P
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
Pluto answered 15/2, 2012 at 13:0 Comment(4)
where function generate_pdf()?Lefton
@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 "generate pdf using php" or something along those lines.Pluto
yes I meet many library for generate php content to pdfbut it's still can not acceptable because my language is not understanding with pdf :( ,, I crazy now();Lefton
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).Kano
G
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.

Greensickness answered 15/2, 2012 at 13:6 Comment(2)
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?Pluto
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".Yuyuan

© 2022 - 2024 — McMap. All rights reserved.