Convert HTML form data to PDF file with PHP

by in PHP MySQL


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=”https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css”>

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

 

convert-HTML-form-data-to-PDF-file-with-PHP-input-element

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

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

 

convert-HTML-form-data-to-PDF-file-with-PHP-validate

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

$mpdf->Output(‘demo.pdf’,’I’);
or
$mpdf->Output();

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

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

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

$mpdf->Output(“mydata.pdf”,’F’);

 

The PHP code would look like this:

<?php
/**
* 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(); ?>

<?php
$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->SetDisplayMode(‘fullwidth’);

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

$mpdf->SetTitle($title);

$mpdf->WriteHTML($body);

// Open tab browser
$mpdf->Output(‘demo.pdf’,’I’);

// 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

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

 

convert-HTML-form-data-to-PDF-file-with-PHP-code-html

HTML code

 

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

convert-HTML-form-data-to-PDF-file-with-PHP-pdf-result

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;

$mpdf->DeletePages($blankpage);

$mpdf->Output();

 

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

 

 

convert-html-form-data-to-pdf-file-with-php-table-font-fize

 

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”

or

table autosize=”2.4″ …

or

table style=”autosize:2.4;” …

or

table autosize=”1″…

 

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

Tags: , , , , ,