Portable Document Format (PDF), standardized as ISO 32000, is a file format developed by Adobe. PDF is used to download data or text content in the web application. The PDF file format is the perfect choice for downloading text or HTML content in a file. While downloading web page content as a PDF file, it requires converting HTML to PDF.

Here, we will see how to convert HTML to PDF or generate PDF files using PHP with dynamic values. For this example, we will use Dompdf

The Dompdf is a PHP library that provides a simple way to convert HTML to PDF documents using HTML layouts and its rendering engine written in PHP. This example will help you to implement PDF generation functionality in the web application with Dompdf.

Before starting,  download the stable release of the Dompdf library and include it in the root directory.

Creating Data Input Functionality

For this example, we will take user few details from the user as input and generate a pdf of that data using Dompdf. However, you can use a database for fetching data or pass some static data too.

Let's create an HTML form that will take some basic information about the user like name, email, and address. Create user.html file:

<!DOCTYPE html>
<head>
  <title>User Details</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css" integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous">
  <style>
    label {
      font-weight: 600;
      color: #666;
    }

    body {
      background: #f1f1f1;
    }

    .box8 {
      box-shadow: 0px 0px 5px 1px #999;
    }

    .mx-t3 {
      margin-top: -3rem;
    }
  </style>
</head>
  <body>
    <div class="container mt-3">
      <form action="generate_pdf.php" method="post">
        <div class="row jumbotron box8">
          <div class="col-sm-12 mx-t3 mb-4">
            <h2 class="text-center text-info">User Details</h2>
          </div>
          <div class="col-sm-6 form-group">
            <label for="name-f">First Name</label>
            <input type="text" class="form-control" name="fname" placeholder="First name." required>
          </div>
          <div class="col-sm-6 form-group">
            <label for="name-l">Last name</label>
            <input type="text" class="form-control" name="lname" placeholder="Last name." required>
          </div>
          <div class="col-sm-6 form-group">
            <label for="email">Email</label>
            <input type="email" class="form-control" name="email"  placeholder="Email." required>
          </div>
          <div class="col-sm-6 form-group">
            <label for="address-1">Address</label>
            <input type="address" class="form-control" name="address" placeholder="Address" required>
          </div>
          <div class="col-sm-12 form-group mb-0">
            <button class="btn btn-primary float-right">Download PDF</button>
          </div>
        </div>
      </form>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.min.js" integrity="sha384-+sLIOodYLS7CIrQpBjl+C7nPvqq+FbNUBDunl/OZv93DB7Ln/533i8e/mZXLi/P+" crossorigin="anonymous"></script>
  </body>
</html>

Generate PDF Using PHP

After creating the user detail page, once the user fills in and submits the form we will generate PDF using user-inputted data. Let's create a PHP file for handling the PDF generation process.

Create a new file in the same directory with generate_pdf.php name as we already mentioned file name in the form action.

<?php
namespace Dompdf;
require_once 'dompdf/autoload.inc.php';

if(isset($_POST['submit_val']))
{
    $dompdf = new Dompdf(); 
    $dompdf->loadHtml('
    <table border=1 align=center width=400>
    <tr><td>Name : </td><td>' . $_POST['fname'] . ' '. $_POST['lname'] . '</td></tr>
    <tr><td>Email : </td><td>' . $_POST['email'] . '</td></tr>
    <tr><td>Address : </td><td>' . $_POST['address'] . '</td></tr>
    </table>
    ');
    $dompdf->setPaper('A4', 'landscape');
    $dompdf->render();
    $dompdf->stream("",array("Attachment" => false));
    exit(0);
}
?>

Here, on top of the code we have included autoload file for Dompdf. which will handle all necessary processes for generating PDFs.

First of all, we will create a new object of Dompdf and call loadHtml() method to load HTML into PDF object. Then we will configure some other options like paper size, orientation, etc.

At last, we will render the PDF file to browser using render() and stream() methods.  There are many other options available like filename for generated PDF, attachment, and all.

Conclusion

Here, we have taken a simple example to generate a PDF file from HTML using Dompdf. In this example, we asked user to fill in some information and generated PDF based on that data. However, we can use any other source of data as per requirement.

There are plenty of other packages available for generating PDF files in PHP like Mpdf, and TCPDF. With Dompdf you can also load HTML from files too.