How to make PDF file downloadable in HTML link using PHP ?

01 Aug, 2021
To Download PDF from HTML link using PHP with the help of header() function in php. The header()function is used to send a raw HTTP header. Sometimes it wants the user to be prompted to save the data such as generated PDF.
Syntax:
http response headers to download any applicationheader("Content-Type: application/octet-stream");
header("Content-Type: application/octet-stream");
http response headers to set composition and file to downloadheader('Content-Disposition: attachment; filename="downloaded.pdf"');
header('Content-Disposition: attachment; filename="downloaded.pdf"');
The length of the requested file need to downloadheader("Content-Length: " . filesize("download.pdf"));
header("Content-Length: " . filesize("download.pdf"));
Reads a file and writes it to the output buffer.readfile('original.pdf');
readfile('original.pdf');
.
Note: Remember that HTTP header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file or from PHP.
Example 1: Save below HTML file as htmllinkpdf.html and save PHP file as downloadpdf.php
Below example to illustrate concept of downloading PDF file using HTML link.
Here downloading file appears to be PDF format but without any content which shows error on opening in any application
HTML code:<!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="gfgpdf.php?file=gfgpdf">Download PDF Now</a></center></body>  </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="gfgpdf.php?file=gfgpdf">Download PDF Now</a></center></body>  </html>
PHP code:<?php  $file = $_GET["file"] .".pdf";  // We will be outputting a PDFheader('Content-Type: application/pdf');  // It will be called downloaded.pdfheader('Content-Disposition: attachment; filename="gfgpdf.pdf"');  $imagpdf = file_put_contents($image, file_get_contents($file));   echo $imagepdf;?>
<?php  $file = $_GET["file"] .".pdf";  // We will be outputting a PDFheader('Content-Type: application/pdf');  // It will be called downloaded.pdfheader('Content-Disposition: attachment; filename="gfgpdf.pdf"');  $imagpdf = file_put_contents($image, file_get_contents($file));   echo $imagepdf;?>
Output:
Below example illustrates the concept of downloading PDF file locally (i.e. read gfgpdf.pdf file from local ) using HTML link.
Example 2: Save HTML file as htmllinkpdf.html and save PHP file as downloadpdf.php
HTML code:<!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>
<!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>
PHP code:<?php  header("Content-Type: application/octet-stream");  $file = $_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.  $fp = fopen($file, "r");while (!feof($fp)) {    echo fread($fp, 65536);    flush(); // This is essential for large downloads}   fclose($fp); ?>
<?php  header("Content-Type: application/octet-stream");  $file = $_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.  $fp = fopen($file, "r");while (!feof($fp)) {    echo fread($fp, 65536);    flush(); // This is essential for large downloads}   fclose($fp); ?>
Output:
PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.
Picked
PHP
PHP Programs
Web Technologies
Web technologies Questions
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Insert Form Data into Database using PHP ?
How to convert array to string in PHP ?
How to Upload Image into Database and Display it using PHP ?
PHP | Converting string to Date and DateTime
Comparing two dates in PHP
How to Insert Form Data into Database using PHP ?
How to convert array to string in PHP ?
How to Upload Image into Database and Display it using PHP ?
How to call PHP function on the click of a Button ?
Comparing two dates in PHP