How to make PDF file downloadable in HTML link?
Instead of linking to the .PDF file, instead do something like
<a href="pdf_server.php?file=pdffilename">Download my eBook</a>
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:
header("Content-Type: application/octet-stream");
$file = $_GET["file"] .".pdf";
header("Content-Disposition: attachment; filename=" . urlencode($file));
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Description: File Transfer");
header("Content-Length: " . filesize($file));
flush(); // this doesn't really matter.
$fp = fopen($file, "r");
while (!feof($fp))
{
echo fread($fp, 65536);
flush(); // this is essential for large downloads
}
fclose($fp);
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
is it possible to make an hyperlink to only download a pdf file?
Let’s say you have a PDF that you want to let people download. The file will be like this:
<a href="/path/to/your/receipt.pdf">Download Receipt</a>
In most browsers, clicking on the link will open the file directly in the browser.
But, if you add the download attribute to the link, it will tell the browser to download the file instead.
<a href="/path/to/your/receipt.pdf" download>Download Receipt</a>
The download attribute works in all modern browsers, including MS Edge, but not Internet Explorer.
In the latest versions of Chrome, you cannot download cross-origin files (they have to be hosted on the same domain).
(HTML) Download a PDF file instead of opening them in browser when clicked
There is now the HTML 5 download
attribute that can handle this.
I agree, and think Sarim's answer is good (it probably should be the chosen answer if the OP ever returns). However, this answer is still the reliable way to handle it (as Yiğit Yener's answer points out and--oddly--people agree with). While the download attribute has gained support, it's still spotty:
http://caniuse.com/#feat=download
HTML: How to download a pdf file using a link
You could try adding the download
attribute to the a
, it's quite well supported.
Related Topics
What Kind of String Is This? How to Unserialize This String
Correct File Permissions For Wordpress
How to Set Http Header to Utf-8 Using PHP Which Is Valid in W3C Validator
What's the Difference Between Echo, Print, and Print_R in PHP
PHP: Convert Any String to Utf-8 Without Knowing the Original Character Set, or At Least Try
How to Find Out If You'Re Using Https Without $_Server['Https']
Performance of Foreach, Array_Map With Lambda and Array_Map With Static Function
Login to Remote Site With PHP Curl
How to Refresh Token With Google API Client
How to Send a Get Request from PHP
PHP - Get Bool to Echo False When False
How to Select and Upload Multiple Files With HTML and PHP, Using Http Post
How to Create an Array For Json Using PHP
Using Jquery Ajax to Retrieve Data from MySQL