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.

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.

3 thoughts on “How to Create PDF file Using mpdf and PHP

  1. 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”

Leave a Reply

Your email address will not be published.