Convert HTML form data to PDF file with PHP


PHP is still a very popular language and it is very easy to retrieve data and display lists, so if you need to print the list to A4 paper, you will have to do it.

The following content shows how to use the Mpdf library to print the data to a pdf file, from here you can backup files, print at will.

In your source folder, create a composer.json file with the following content:


“require”: {
“mpdf/mpdf”: “^8.0”
// or “mpdf/mpdf”: “*”

Make sure you have composer installed on your computer, then run the command to install the smalot and pdfparser libraries:

composer update mpdf

We are not going to query from the database but will always retrieve the data as soon as the form is submitted, assuming our data entry form simply has username and email.

Convert HTML form data to PDF file with PHP

The main functions will perform:

– The article uses Bootstrap 4 library:

<link rel=”stylesheet” href=””>

– Form html includes 2 fields: username and email, button submit



Form html includes 2 fields: username and email, button submit


– Use PHP to validate input data, email format

– If there are errors, they will be notified via the for loop

// if errors issset, show it
if (isset($errors)) {
foreach ($errors as $error) { ?>
<?php echo $error; ?>
<?php }
} ?>



Validate input data, email format


– Mpdf library, with lots of options to match the appropriate output file:
+ Font selection

+ Specifying the paper size

+ Specify the orientation of the paper: L or P

+ Margin layout


– With output file option:

The file will be opened as soon as you click submit, open tab browser


Download file: the file will be downloaded right after clicking submit


The file will be saved to the server as soon as you click submit



The PHP code would look like this:

* Require
require_once __DIR__.’/vendor/autoload.php’;

if (isset($_POST[‘viewPdf’])) {
// Get element form data
$username = $_POST[‘username’];
$email = $_POST[’email’];

// Validation username is set
if ($username == ”) {
$errors[] = ‘Username is required’;

// Check for a valid email address
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = ‘Please enter a valid email address’;

//if no errors carry on
if (!isset($errors)) {
//create html of the data
ob_start(); ?>

$mpdf = new \Mpdf\Mpdf([
‘fontdata’ => [
‘ipa’ => [
‘R’ => ‘Sun-ExtA.ttf’
‘mode’ => ‘en’, // Specify the language to use as English(en), Japanese(ja),…
‘format’ => ‘A4’, // Specifying the paper size
‘orientation’ => ‘L’, // Specify the orientation of the paper: L, P
‘default_font_size’ => 7,
‘margin_left’ => 20, // Left margin
‘margin_right’ => 20, // Right margin
‘margin_top’ => 10, // Top margin
‘margin_bottom’ => 10, // Bottom margin
‘margin_header’ => 10, // Header margin
‘margin_footer’ => 10 // Footer margin

header(‘content-type: text/html; charset=utf-8’);
$body = ob_get_clean();


$mpdf->setFooter(‘Page {PAGENO} / {nb}’);



// Open tab browser

// Open tab browser
// $mpdf->Output();

// Download file
// $mpdf->Output(‘demo.pdf’,’D’);

// Save to server
// $mpdf->Output(“mydata.pdf”,’F’);
} ?>


– HTML code, enter the form content and show errors if any

// if errors issset, show it
if (isset($errors)) {
foreach ($errors as $error) { ?>
<?php echo $error; ?>
<?php }



HTML code


As soon as you click submit button, you will receive the following PDF file’s content:


PDF file’s content


Important tips:

1. If your data needs to be exported in bulk, an error may occur:

“Fatal error Exception: The HTML code size is larger than pcre.backtrack_limit = 100000”

=> You need to edit it in the php.ini file, for example the following parameters:

“pcre.backtrack_limit = 10000000”


2. If you want to delete blank page at the end of the document:

$blankpage = $mpdf->page + 1;




3. MPDF: Long text in table td to small size:





If it is turned off e.g. $mpdf->shrink_tables_to_fit = 0; it is overridden by a specific declaration for a table e.g.:

style=”overflow: wrap”


table autosize=”2.4″ …


table style=”autosize:2.4;” …


table autosize=”1″…


 You can refer to How to Convert PDF to text file using PHP here

Tags: , , , , ,
1 Comment
  • Marie - 19/10/2022 - 11:11 am

    Thanks your post

  • Your comment

    Please rate

    Your comment is approved before being displayed.
    Your email address will not be published. Required fields are marked *