Generating PDF Reports From HTML form With DOMPDF Library is a valuable and widespread tool that is applied to a few special uses like invoices, receipts, applications, custom information, and so forth.
Moreover, here in this particular section, we would be learning how to make an HTML Form, storing the data in the form inside PHP, generating a PDF with DOMPDF, and finally getting the PDF either to download or to display the PDF in our web Browser.
We are able to refer to a few of the practices that we have already adopted, such as introducing reCAPTCHA to the HTML Form, WordPress website into pure PHP, and so forth.
However, the creation of PDF Reports based on HTML forms through the use of PHP and the DOM PDF Library is a handy and popular functionality, taking special uses and applications examples like creating invoices, receipts, applications, individual data, etc.
Here, in this particular section, we will be learning how to create an HTML form, how to capture the form data in PHP, how to generate a PDF using DOMPDF, and how we can download or view the PDF in our web Browser.

There are a number of practices which we have already applied, such as adding reCAPTCHA to HTML Form, WordPress to Pure PHP, etc.
Folder Structure for Generating PDF Reports from HTML Forms
The Folder structure to produce PDF reports using HTML Forms should be kept simple since we have to simply keep all the files in a single root folder.
We need to create a base folder called pdf-generator and need to create various files as given below –
| Files | Usage |
| index.html | To write code for HTML Form. |
| generate_pdf.php | PHP Login to Generate PDF. |
| dompdf/ | Folder for DOMPDF library to be installed via Composer or manually. |
| To output/ | Where generated PDFs can be saved. It is optional. |
Therefore, the Folder structure of the project can be seen in the image below –
Installing DOMPDF for Generating PDF Reports from HTML Forms
In order to use the DOMPDF Library for generating PDF Reports from HTML Forms, we must download it. We cannot do it via CDN; we simply need to download.
To sum up, this will require the Composer, which is typically a dependency manager for PHP, and needs to author a command of the following nature in order to download the same.
Composer requires dompdf/dompdf

Or, we can download the DOMPDF manually by accessing the link: https://github.com/dompdf/dompdf.
Next, we can copy and download the zipped file to the dompdf folder and unzip it there too to use it accordingly.
Creating HTML Form for Report
We have to generate an HTML File that will have the form for the fields that will be required to be saved in the PDF. The code is as follows –
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Generate PDF Report</title> </head> <body> <h2>Report Form</h2> <form action="generate_pdf.php" method="POST"> <label>Name:</label><br> <input type="text" name="name" required><br><br> <label>Email:</label><br> <input type="email" name="email" required><br><br> <label>Message:</label><br> <textarea name="message" rows="5" required></textarea><br><br> <input type="submit" value="Generate PDF"> </form> </body> </html>
Moreover, the code contains three fields in the cited code: Name, Message, and Message that will translate these form fields into the PDF format.
Creating a PHP Backend for Generating PDF Reports from HTML Forms
In this section, we need to create a folder named generate_pdf.php that would be used in the action attribute of the HTML Form.
Therefore, the code to display the backend functionality as follows –
<?php
require 'vendor/autoload.php'; // If using Composer
// require_once 'dompdf/autoload.inc.php'; // If manually added
use Dompdf\Dompdf;
use Dompdf\Options;
// Collect form data
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$message = nl2br(htmlspecialchars($_POST['message']));
// PDF content as HTML
$html = "
<h1>User Report</h1>
<p><strong>Name:</strong> $name</p>
<p><strong>Email:</strong> $email</p>
<p><strong>Message:</strong><br>$message</p>
";
// Setup DOMPDF
$options = new Options();
$options->set('isHtml5ParserEnabled', true);
$options->set('defaultFont', 'Helvetica');
$dompdf = new Dompdf($options);
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
// Output the PDF to browser
$dompdf->stream("user_report.pdf", array("Attachment" => false)); // true to force download
exit;There are several predefined functions included in the form whose workings and functionalities are mentioned in the table below –
| Functions | Description |
| htmlspecialchars() | It is use to prevents XSS attacks by escaping special characters. |
| nl2br() | It is used to convert the line breals in textarea to <br>. |
| Options() | It will allow the customization of DOMPDF settings. |
| LoadHTML() | It will loads the HTML Content to Convert to the PDF. |
| render() | It will renders the HTML into a PDF Document. |
| stream() | It is use to Streams the PDF to the browser. Attachment => false means inline view. |

There are some practices that can make the project more robust, such as – Always sanitize the user Input, never include the raw data directly, and use proper file permissions.
Conclusion
In conclusion, it is quite a critical practice, whose name is Generating PDF Reports From HTML Forms, which immediately transforms HTML Form inputs to a PDF.
It is significant in that sense that it is a good habit in E-commerce that it will be translated into an invoice and furthermore sent dynamically to the particular emails.
Use the button to access our blogs that address big challenges with PHP and JavaScript.
Privacy is guaranteed, and you can get your own personalized site at a low price of Rs. 5,499 with a domain name and hosting validity of 1 year. Contact us now.




