How to make PDF file downloadable in HTML link?

Posted by Rohit Kumar | Updated on

Want to make PDF file downloadable in HTML link in PHP? Instead of linking to the .PDF file, instead do something like:

Which outputs a custom header, opens the PDF (binary safe) and prints the data to the user’s browser, then they can choose to save the PDF despite their browser settings. The pdf_server.php should look like this:

PS: and obviously run some sanity checks on the “file” variable to prevent people from stealing your files such as don’t accept file extensions, deny slashes, add .pdf to the value.


If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.

Related Questions & Answers