How to Download PDF Files in HTML With PHP

John Wachira Feb 02, 2024
  1. Syntax of the header() Function in PHP
  2. Download PDF With HTML Link Using PHP
How to Download PDF Files in HTML With PHP

This tutorial will discuss the steps to make your PDF files downloadable in HTML links with PHP. We will use the PHP header() function to prompt users to save our PDF file.

Syntax of the header() Function in PHP

  1. The header below will download any application.

    header("Content-Type: application/octet-stream");
    
  2. The header below will set the composition and the downloadable file.

    header('Content-Disposition: attachment; filename="Delft.pdf"');
    
  3. The header below shows the file’s size.

    header("Content-Length: " . filesize("Delft.pdf"));
    

We will attempt to download a PDF(Delft.pdf) in HTML with a PHP script in the following example. The Delft.pdf file is empty and will show an error upon opening, and we’ll give you a picture of how we go about the process.

Example Code - HTML script:

<!DOCTYPE html>
<html>
   <head>
      <title>Download PDF using PHP from HTML Link</title>
   </head>
   <body>
      <center>
         <h2 style="color:red;">Welcome To DELFT</h2>
         <p><b>Click below to download PDF</b></p>
         <a href="downloadpdf.php?file=Delft">Download PDF Now</a>
      </center>
   </body>
</html>

Example Code - PHP script:

<?php
$file = $_GET["file"] .".pdf";
// To Output a PDF file
header('Content-Type: application/pdf');
// PDF will be called Delft.pdf
header('Content-Disposition: attachment; filename="Delft.pdf"');
$imagpdf = file_put_contents($image, file_get_contents($file));
echo $imagepdf;
?>

Output;

Download PDF in HTML Link with a PHP script

Download PDF in HTML Link with a PHP script output

The link will download a Delft.pdf file, but since the file is empty, we will get an error when trying to open it. That is the basic concept of downloading PDF files in HTML links with PHP.

Let’s try downloading and reading the Delft.pdf file from our local machine. In the next example, we will attempt to download a PDF file locally with an HTML link.

Example Code - HTML Script:

<!DOCTYPE html>
<html>
   <head>
      <title>Download PDF using PHP from HTML Link</title>
   </head>
   <body>
      <center>
         <h2 style="color:blue;">Welcome To DELFTSTACK</h2>
         <p><b>Click below to download PDF</b></p>
         <a href="downloadpdf.php?file=Delft">Download PDF Now</a>
      </center>
   </body>
</html>

Example Code - PHP Script:

<?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(); // Not a must.
$fp = fopen($file, "r");
while (!feof($fp)) {
echo fread($fp, 65536);
flush(); // This is essential for large downloads
}
fclose($fp);
?>

Output;

download a PDF file locally with a HTML link

download a PDF file locally with a HTML link output

The link will download the Delft.pdf file, and we successfully open the file. Always call headers before sending output.

Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - PHP Download