HTML form to PDF file using PHP and MPDF!

html-pdf

This tutorial show you how to convert a HTML form to dynamically generated PDF file using MPDF library.  Basically you are going to see how easy to generate a PDF file in PHP.

There is a form which is a normal PHP form after you fill the details and submit, immediately the values you entered are going to be created as a PDF file. If you are looking for the same kind of script then you are at the right place.

So, what all need for you?

Here is the MPDF download page, go to the link and download the full installation file which is around (12.8MB):

Download mPDF Version 5.7

Here is the example code which helps you to understand what is it:

HTML CODE:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="simpleValidation.js"></script>
</head>
<style>
	input[type="text"]{border:none; border: 1px solid black; width: 182px;}
	.requiredfield{color:red;}
	.error{color:red;}
</style>
<body>

<center><h2>
Demo of HTML form to Dynamic PDF Generator - blog.theonlytutorials.com<br>
Script can be download from this link <a href="https://blog.theonlytutorials.com/simple-jquery-validation-custom-error-message/">Go to the Download page</a></h2>
</center>
<form id="myform" method="post" action="generatePDF.php">	
	<table border="0" align="center">
	  <tr><td>Name:</td><td><input type="text" name="name" id="name"  /></td></tr>
	  <tr><td>Email:</td><td><input type="text" name="email" id="email"  /></td></tr>
	  <tr><td>Message:</td><td><textarea name="message" id="message"></textarea></td></tr>
	  <tr><td></td><td><input type="submit" id="submitbtn" /></td></tr>
	</table>
</form>
</body>
</html>

In the above HTML I have used a Simple jQuery Validation script which you can also use it in your code if you wish.


PHP PART:

<?php

include('MPDF57/mpdf.php');

$name 	= $_POST['name'];
$email 	= $_POST['email'];
$msg 	= $_POST['message'];

$html .= "
<html>
<head>
<style>
body {font-family: sans-serif;
    font-size: 10pt;
}
td { vertical-align: top; 
    border-left: 0.6mm solid #000000;
    border-right: 0.6mm solid #000000;
	align: center;
}
table thead td { background-color: #EEEEEE;
    text-align: center;
    border: 0.6mm solid #000000;
}
td.lastrow {
    background-color: #FFFFFF;
    border: 0mm none #000000;
    border-bottom: 0.6mm solid #000000;
    border-left: 0.6mm solid #000000;
	border-right: 0.6mm solid #000000;
}

</style>
</head>
<body>

<!--mpdf
<htmlpagefooter name='myfooter'>
<div style='border-top: 1px solid #000000; font-size: 9pt; text-align: center; padding-top: 3mm; '>
Page {PAGENO} of {nb}
</div>
</htmlpagefooter>

<sethtmlpageheader name='myheader' value='on' show-this-page='1' />
<sethtmlpagefooter name='myfooter' value='on' />
mpdf-->

<div style='text-align:center;'>HTML Form to PDF - Blog.theonlytutorials.com</div><br>
<table class='items' width='100%' style='font-size: 9pt; border-collapse: collapse;' cellpadding='8'>
<thead>
<tr>
<td width='15%'>FIELDS</td>
<td width='15%'>VALUES</td>
</tr>
</thead>
<tbody>
<tr><td>Name</td><td>$name</td></tr>
<tr><td>Email</td><td>$email</td></tr>
<tr><td class='lastrow'>Your Message</td><td class='lastrow'>$msg</td></tr>
</tbody>
</table>
</body>
</html>
";

$mpdf=new mPDF();
$mpdf->WriteHTML($html);
$mpdf->SetDisplayMode('fullpage');

$mpdf->Output();

?>

So basically it is the same way like capturing the form values using $_POST method.

After capturing and generated the html code we need to pass the $html variable to the MDPF. The above code shows everything in one shot.

The below code shows how to call MPDF functions

<?php
include('mpdf.php');

$html = "your HTML code";

$mpdf=new mPDF();
$mpdf->WriteHTML($html);
$mpdf->SetDisplayMode('fullpage');

$mpdf->Output();

?>

To make it very easier to you, I have given here the download and demo options

Click here to download the above example with MPDF 

download

If already downloaded MPFD just download the Example script by clicking the below button:

demo download

 

7 thoughts on “HTML form to PDF file using PHP and MPDF!

  1. hi
    My inedx1.html and pdf form formate are same.
    i want to add css bootstrap and style on html code of generated.php.how can i do that .. please reply as soon as possible

      1. It can’t load the pdf, it says. No errors or what so ever. Have tried your older mPDF version and the newer 60 one. Do you have any idea why?

  2. Am I to late to ask?? How to redirect mpdf to a page after print process?? And what is ” exit(); ” means??Tq

Leave a Reply

Theme: Overlay by Kaira
Agurchand