how to force pdf download
Here is something new for today. If you want to force your pdf to be downloaded rather than opened in the browsers PDF Reader, this is how to do it:
The php code
It's really simple. Just create a php file and paste this code in to:
<?php
$file = $_GET['file'];
header ("Content-type: octet/stream");
header ("Content-disposition: attachment; filename=".$file.";");
header("Content-Length: ".filesize($file));
readfile($file);
exit;
?>
Save your file as you want. I am going to save it as straight-pdf-download.php.
After this all you have to do is putting the link in to your page.
The Hyperlink
Click <a href="straight-pdf-download.php?file=yourfile.pdf">HERE</a> to download the PDF version.
Be careful with the paths. If you store your pdf in a different folder, or want to save the php somewhere else, use absolute path instead of relative.
This method will work as well:
Click <a href="http://yourdomain.co.uk/yourfolder/straight-pdf-download.php?file=http://yourdomain.co.uk/yourfolder/yourfile.pdf">HERE</a> to download the PDF version.
You can check the method on my about page. If you click on the "Click HERE to download the PDF version", just wait... You probably think, that will be opened in your browser. That will never happen :)
Have fun.
This web development tutorial brought you by Attila Hajdu, expert web developer, multimedia specialist in Portsmouth.
back to css tips



