Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
66
rated 0 times [  71] [ 5]  / answers: 1 / hits: 22906  / 12 Years ago, tue, june 5, 2012, 12:00:00

I have a form button, when clicked it submits the form.



I'd like that at the same time, the browser starts downloading a PDF file. I wonder how can I do this? Please keep in mind that PDF are usually opened by the browser by default, but I'd like the browser to Save As the file and not open the file. Is this do-able using html only or do I need javascript?


More From » html

 Answers
9

If you are using PHP in the server side try this. This is tested code which is running in one of my websites.



<?php
ob_start();
$file = 'YOUR-FILENAME-HERE.pdf';

if (file_exists($file))
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit();
}
?>


Browser will prompt to download without displaying it in the browser.


[#85134] Monday, June 4, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
sandra

Total Points: 708
Total Questions: 100
Total Answers: 84

Location: Bosnia and Herzegovina
Member since Thu, Jun 24, 2021
3 Years ago
sandra questions
Tue, Jun 30, 20, 00:00, 4 Years ago
Sun, May 31, 20, 00:00, 4 Years ago
Wed, May 20, 20, 00:00, 4 Years ago
Fri, May 31, 19, 00:00, 5 Years ago
;