Welcome Folks I am back with another blog post. In this post we will be building a simple web app in PHP Language using which we will be generating PDF Document using HTML Form using a PHP Library called as MPDF. A short demo of the application is shown below where user is presented with the interface to write the details such as first name, lastname , email , and message and when he click the generate button a pdf document is instantly download. So in the document all the details are written out. So let’s get started.

Now the User can enter can random data to make the form turn into pdf format something like below.

After entering the values when the user presses the create pdf button then a download file is generated something like below

Now when we open this pdf file which is generated by the library then all the values are shown to us in this file as shown below.

So you can see above this is the small project we will be building in this tutorial so let get started by building this app. First of all go and download the library which will be required for doing this i.e. MPDF . Download it by composer. If you already have downloaded composer then it is very easy . One command has to be written that’s it. The command is shown below. Go to your project active directory and issue this command

composer require mpdf/mpdf

In order to check if the library is successfully installed or not just check if you have got a vendor folder there after command is completed. If it is there then you have successfully installed the library.

Now create a index.php file. Only one file will be needed for this mini app. Write the html code necessary for generating the html form something like this

<!DOCTYPE html> <html> <head> <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css"/> <title>Create Pdf with HTML Form with Mpdf</title> </head> <body> <div class="container mt-5"> <h1>Create Pdf from Form</h1> <p>Fill out the Below details to generate the form</p> <form action="index.php" method="post"> <input type="text" name="fname" class="form-control" placeholder="First Name" required> <input type="text" name="lname" class="form-control" placeholder="Last Name" required> <input type="email" name="email" class="form-control" placeholder="Email" required> <textarea class="form-control" name="message" placeholder="Message"> </textarea> <button class="btn btn-success" type="submit">Create PDF</button> </form> </div> </body> </html>

So we are using some bootstrap here so that’s why just include bootstrap cdn after the title and also there is a form tag which has a action attribute equals to index.php same file and also the method is equals to post. We are posting data from the form to the server. Lastly we have input fields where the user will enter the data and a button to submit the form to the server.

Now we will be writing the PHP code in the same file index.php which is necessary after the user has submitted the form to the server. In these blocks of code we will be handling the data which is submitted by the user and generating the pdf from it from the library mpdf. First of all we will include the library and then we will be invoking it’s methods with the actual data which is passed and then we will allow the pdf file to be download as well. All the PHP code is given below.

<?php require_once __DIR__ . '/vendor/autoload.php'; // Grab variables if(isset($_POST['fname']) && isset($_POST['lname']) && isset($_POST['email']) && isset($_POST['message'])) { $fname = $_POST['fname']; $lname = $_POST['lname']; $email = $_POST['email']; $message = $_POST['message']; $mpdf = new \Mpdf\Mpdf(); $data = ""; $data .= "<h1>Your Details</h1>"; $data .='<strong>First Name</strong> '. $fname.'<br>'; $data .='<strong>Last Name</strong> '. $lname.'<br>'; $data .='<strong>Email</strong> '. $email .'<br>'; $data .='<strong>Message</strong> '. $message.'<br>'; $mpdf->WriteHtml($data); $mpdf->output("myfile.pdf",'D'); } ?>

Full code which includes the html and as well as PHP is given below.