How to make PDF file downloadable in HTML link using PHP ? (original) (raw)

Last Updated : 12 Jul, 2025

In web development, it is common to provide users with downloadable resources, such as PDF files. If you want to create a downloadable PDF link using HTML and PHP, this article will guide you through the process of making a PDF file downloadable when the user clicks on a link.

Approach

Steps to Create the PDF file downloadable

The first step is to create an HTML link that will trigger the PDF download. You can use a simple anchor tag () to create this link. In this example, we create a link that, when clicked, will prompt the user to download a PDF file called gfgpdf.pdf.

html ``

</p><pre><code class="language-html">
<!DOCTYPE html>
<html>

<head>
    <title>Download PDF using PHP from HTML Link</title>
</head>

<body>
    <center>
        <h2 style="color:green;">Welcome To GFG</h2>
        <p><b>Click below to download PDF</b>
        </p>
        <a href="downloadpdf.php?file=gfgpdf">Download PDF Now</a>
    </center>
</body>

</html>
</code></pre><p></p><h3 style="text-align:left"><b><strong>Step 2: PHP Script to Handle the Download (downloadpdf.php)</strong></b></h3><p dir="ltr"><span>Next, you need to create the PHP script that will handle the download logic.</span><gfg-tabs data-mode="light" data-run-ide="false"><gfg-tab slot="tab">php

<?php

header("Content-Type: application/octet-stream");
 <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>f</mi><mi>i</mi><mi>l</mi><mi>e</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">file = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal">i</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">e</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>_GET["file"]  . ".pdf";

header("Content-Disposition: attachment; filename=" . urlencode($file));   
header("Content-Type: application/download");
header("Content-Description: File Transfer");            
header("Content-Length: " . filesize($file));

flush(); // This doesn't really matter.
 <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>f</mi><mi>p</mi><mo>=</mo><mi>f</mi><mi>o</mi><mi>p</mi><mi>e</mi><mi>n</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">fp = fopen(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal">p</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal">o</span><span class="mord mathnormal">p</span><span class="mord mathnormal">e</span><span class="mord mathnormal">n</span><span class="mopen">(</span></span></span></span>file, "r");
while (!feof($fp)) {
    echo fread($fp, 65536);
    flush(); // This is essential for large downloads
} 

fclose($fp); 
?>

**Output:

**In this example:

Conclusion

Making a PDF file downloadable in HTML using PHP is a straightforward process. By setting up a link in your HTML page that points to a PHP script, you can provide users with a seamless way to download PDF files. The key here is to use the serialize() and unserialize() functions for handling complex data storage efficiently, while also ensuring security and proper handling of files on the server.