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");
To save a PDF, choose File > Save or click the Save File icon in the Heads Up Display (HUD) toolbar at the bottom of the PDF. The Save As dialog box is displayed. Choose the location where you want to save the PDF and then click Save.
A: There are 2 ways you can do this: Save the whole PDF: You can export the modified PDF as an arrayBuffer using Instance#exportPDF . Then you can convert this arrayBuffer to blob and sent it to your server. You can see an example in the Sending the PDF to a server section.
You can simply read all the bytes of the pdf file and store them in the blob field of sqlite DB, but a better solution which is highly recommended is to store the files on the Internal Storage and in your DB just store the file's path. You have also to check if there is enough memory storage for you. Save this answer.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With