How to create a PDF from a form using PHP
In this post, you will learn how to generate a PDF from a form using the PHP programming language. This application contains a form to get user information. When the user clicks on the submit button, a PDF document is instantly generated. In the generated document, all the details are written out.
The PDF is a read-only document that cannot be altered by users until they have the right electronic impression. So, PDF is mostly used for sharing information over the web. There is also a demand for producing PDF dynamically increased by the associations. This article helps you with generating such types of applications, like- generating an invoice, salary receipt, visiting card, etc. with a single click.
There are several PHP libraries available to generate PDF. In this article, we are using 'mPDF' to generate a PDF. This PHP library is used to generate PDF from UTF-8 encoded HTML. The mPDF library contains a number of enhancements over FPDF and HTML2FPDF. This library is written by Lan Back and released under the GNU GPL v2 License. This also provides features for generating a PDF with a watermark.
These are the steps to generate PDF from a form using PHP programming language.
Install mPDF with Composer
First, we need to install the mPDF library to generate a PDF file. You can either download this from Github or install it using Composer. If your system does not have composer installed, then first download the latest version of composer from its official website.
https://getcomposer.org/download/You can check for a successful installation using the following command on cmd.
c:\>composer
Now, go to your project directory on the command prompt and run the following command to install mPDF library.
E:\wamp\www\mpdfproj> composer require mpdf/mpdf
After this, you have probably noticed that Composer has downloaded all libraries under the 'vendor' directory of your project root.
Now, let's create a main PHP file 'index.php' that we will call on the browser. At the top of this page, include the mPDF library file.
require_once __DIR__ . '/vendor/autoload.php';
$mpdf = new \Mpdf\Mpdf();
Complete Code: Generate PDF from PHP
Here is the code to generate PDF dynamically from an HTML form using the mPDF library of PHP. You can replace the HTML content and PHP variables assigned in '$body' with whatever you want to print.
index.php<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<?php
if(isset($_POST['submit'])){
//collect form data
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
//check for a valid email address
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
$error = 'Please enter a valid email address';
echo $error;
}
//if no errors carry on
if(!isset($error)){
//create HTML of the post data
$body = '<h1>Data from form</h1>'
.'<p>Name: '.$name.'</p>'
.'<p>Email: '.$email.'</p>'
.'<p>Phone: '.$phone.'</p>';
require_once __DIR__ . '/vendor/autoload.php';
$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML($body);
$mpdf->SetDisplayMode('fullpage');
$mpdf->list_indent_first_level = 0;
//call watermark content and image
$mpdf->SetWatermarkText('etutorialspoint');
$mpdf->showWatermarkText = true;
$mpdf->watermarkTextAlpha = 0.1;
//output in browser
$mpdf->Output();
}
}
?>
</head>
<body>
<form action="" method="post">
<div class="form-group">
<label for="name">Name:
<input type="text" class="form-control" id="name" placeholder="Enter name" name="name" required>
</div>
<div class="form-group">
<label for="email">Email:
<input type="text" class="form-control" id="email" placeholder="Enter email" name="email" required>
</div>
<div class="form-group">
<label for="phone">Phone:
<input type="text" class="form-control" id="phone" placeholder="Enter phone" name="phone" required>
</div>
<button type="submit" class="btn btn-primary" name="submit">Submit</button>
</form>
</body>
</html>
In the above code, the WriteHTML() method writes the HTML content to PDF file. With the help of the SetDisplayMode() function, we have defined the way the document is to be displayed by the viewer. Like, we can set 'fullpage', 'fullwidth', 'real', 'default', 'none' or specify the magnification(zoom) level of the display. We have also set the watermark on the PDF using the SetWatermarkText() function and shown it on the PDF using the showWatermarkText() function.
When the user clicks on the submit button, the generated PDF looks something like this -
Related Articles
PHP reverse a string without predefined functionPHP random quote generator
PHP convert string into an array
PHP remove HTML and PHP tags from string
Import Excel File into MySQL using PHP
PHP array length
Import Excel File into MySQL Database using PHP
PHP String Contains
PHP remove last character from string
PHP calculate percentage of total
PHP qr code generator
How to read CSV file in PHP and store in MySQL
Create And Download Word Document in PHP
PHP SplFileObject Standard Library
Simple File Upload Script in PHP
Sending form data to an email using PHP
Recover forgot password using PHP and MySQL
Php file based authentication
Simple PHP File Cache
How to get current directory, filename and code line number in PHP