Php

How to Create PDF file Using mpdf and PHP

PDF is a popular file format for sharing and reading data on the internet. We’ve already given a tutorial on convert html data into pdf.

Now I’m making a basic PHP application that uses the mpdf library to generate PDF files. The mpdf is a fantastic PHP utility that allows you to transform the text into a rich UI PDF format. The mPDF library aids in the creation of PDF files from HTML that has been encoded in UTF-8.

There are following dependencies are:

  • PHP 5.6 || 7.0.0 || 7.1.0 || 7.2.0 is required by mPDF 7.0.
  • PHP mbstring and gd extensions must be enabled.
  • Requires the PHP zlib module for output compression and embedded resources like fonts, as well as bcmath for barcode generation and xml for character set conversion and SVG handling..

Create PDF File with Watermark Using PHP

We will add watermark text within a pdf file. The mPDF providing method to add watermark text and images. There is a two-step process to create a pdf using PHP.

Step 1. Download the library from GitHub then extract it and paste it into (xampp/htdoc/projectname/mpdf) or use the composer command composer require mpdf/mpdf.

Related Post

Step 2. Create an HTML document and save it as a php variable. You can make it dynamic, but I’m going with a static HTML document. The following content will be added to the index.php file.

$html = '

<table width="60%" cellspacing="0">
 <thead>
  <tr>
   <th>Empid</th>

   <th>Name</th>

   <th>Salary</th>

   <th>Age</th>
  </tr>
 </thead>

 <tbody>
  <tr>
   <td>1</td>

   <td>Tiger Nixon</td>

   <td>320800</td>

   <td>61</td>
  </tr>

  <tr>
   <td>2</td>

   <td>Garrett Winters</td>

   <td>434343</td>

   <td>63</td>
  </tr>

  <tr>
   <td>3</td>

   <td>Ashton Cox</td>

   <td>86000</td>

   <td>66</td>
  </tr>

  <tr>
   <td>4</td>

   <td>Cedric Kelly</td>

   <td>433060</td>

   <td>22</td>
  </tr>

  <tr>
   <td>5</td>

   <td>Airi Satou</td>

   <td>162700</td>

   <td>33</td>
  </tr>

  <tr>
   <td>6</td>

   <td>Brielle Williamson</td>

   <td>372000</td>

   <td>61</td>
  </tr>

  <tr>
   <td>7</td>

   <td>Herrod Chandler</td>

   <td>137500</td>

   <td>59</td>
  </tr>

  <tr>
   <td>8</td>

   <td>Rhona Davidson</td>

   <td>327900</td>

   <td>55</td>
  </tr>

  <tr>
   <td>9</td>

   <td>Colleen Hurst</td>

   <td>205500</td>

   <td>39</td>
  </tr>

  <tr>
   <td>10</td>

   <td>Sonya Frost</td>

   <td>103600</td>

   <td>23</td>
  </tr>
 </tbody>
</table>';

Output:

EmpidNameSalaryAge
1Tiger Nixon32080061
2Garrett Winters43434363
3Ashton Cox8600066
4Cedric Kelly43306022
5Airi Satou16270033
6Brielle Williamson37200061
7Herrod Chandler13750059
8Rhona Davidson32790055
9Colleen Hurst20550039
10Sonya Frost10360023

Step 3. We will include the library class mPDF at the end of the index.php file.

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

//call watermark content aand image
$mpdf->SetWatermarkText('phpflow.COM');
$mpdf->showWatermarkText = true;
$mpdf->watermarkTextAlpha = 0.1;


//save the file put which location you need folder/filname
$mpdf->Output("phpflow.pdf", 'F');


//out put in browser below output function
$mpdf->Output();

Using PHP and static HTML content, I built a simple PDF document. You can change the pdf creation method to meet your requirements.

You can download the source code and see the Demo from the below link.

View Comments

  • I have used composer to for mpdf library. I would getting error message

    "Uncaught Error: Class 'mPDF' not found in /opt/lampp/htdocs/working/pdf-example-using-mpdf-code/generate_pdf.php:76 Stack trace: #0 /opt/lampp/htdocs/working/pdf-example-using-mpdf-code/generate_pdf.php(92): generate_pdf() #1 {main} thrown in /opt/lampp/htdocs/working/pdf-example-using-mpdf-code/generate_pdf.php on line 76"

Recent Posts

Configure and Retrieve S3 Information Using Laravel PHP-AWS-SDK

This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More

3 months ago

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

9 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

9 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

9 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

10 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

10 months ago

Categories