How to make PDF file downloadable in HTML link - NESTED CODE || TECH FLOAT

Breaking

Post Top Ad

Post Top Ad

Sunday, 28 October 2012

How to make PDF file downloadable in HTML link

The HTML code for download option

<html>
<head></head>
<body>

<a href="pdf_server.php?file=new sample paper">Download my eBook</a>

</body>

</html>


Now the Corresponding php code for "pdf_server.php"

<?php

header("Content-Type: application/octet-stream");

$file = $_GET["file"] .".pdf";
header("Content-Disposition: attachment; filename=" . urlencode($file));  
header("Content-Type: application/force-download");
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);

?>


Note: The pdf file should be in the folder, as it is described in the php-code (i.e the pdf_server.php file).

No comments:

Post a Comment

Post Bottom Ad