How to Create PDF file Using mpdf and PHP

PDF is the common format of the file to percentage or study records on web.we’ve already shared a tutorial to convert HTML facts into pdf and excellent pdf viewer.

Now here I am developing a simple Hypertext Preprocessor application which is generating PDF record using the mpdf library. The pdf is wonderful personal homepage library that assists to convert textual content into wealthy UI pdf format using PHP. The mPDF library assist to generates PDF files from UTF-8 encoded HTML.

There are following dependencies are:

  • mPDF 7.0 requires PHP ^5.6 || ~7.0.0 || ~7.1.0 || ~7.2.0
  • PHP mbstring and gd extensions must be enabled.
  • Require PHP zlib module for compression of output and embedded resources such as fonts, bcmath for generating barcodes or XML for character set conversion and SVG handling.

Create PDF File with Watermark Using PHP

We can add watermark textual content within pdf record.The mPDF presenting approach to add watermark text and photo.There is steps method to create pdf using PHP.

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

Step 2. Create an HTML content and assigned into PHP variable, You can generate dynamic but I am taking as a static HTML content.We will add below content into the fileindex.php.

 


$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>';

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

 


$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();

I have created simple PDF file using php and static HTML content.You can modify pdf generation method as per your need.

Please follow and like us:



Leave a Comment

Your email address will not be published. Required fields are marked *