How to Generate PDF using HTML Form in PHP using MPDF

  • Post author:
  • Post category:PHP
  • Post comments:0 Comments

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

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


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.

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

Leave a Reply