Generate a pdf file using mPDF in PHP, create pdf using php, convert html to pdf using mpdf in php

Generate a pdf file using mPDF in PHP

In this article, we shall discuss the steps followed to generate a pdf file using mPDF in PHP.

A pdf file is considered as a document that is read-only meaning it cannot be edited or tampered with. It is a safe way of presenting or sending data to other users therefore the need to convert data to pdf files while exporting data.

mPDF is a library supported by PHP language which uses UTF-8 to encode HTML code.

mPDF allows one to create any size of pdf document depending on the measurements that are supported in the library.

There are various versions of the mPDF library that are supported by various versions of PHP. mPDF can be downloaded from GitHub which is the official channel that releases different versions of mPDF that are supported by different PHP versions

PHP 8 is supported as of mPDF version 8.0.13 while the other mPDF versions support the lower PHP versions as described in https://github.com/mpdf/mpdf

To create a pdf file using mPDF;

You can either clone or download as zip file depending on how you prefer. Also, you can install mPDF using composer. Follow the steps provided in the GitHub link above

After downloading mostly as zip, extract the zip file and load the mPDF folder in your working directory. If through the composer, you will need to use the auto load file

  • Create a file which will generate the pdf and give it your preferred name
  • Include the mPDF library

//include the mpdf library through the autoloader

include("mpdf/vendor/autoload.php");

  • Initialize the mPDF library as follows

//initialize mpdf function

$mpdf = new \\Mpdf\\Mpdf();

  • Define the code that will be displayed and converted to pdf file

You can either define the html code direct, as a variable or include it as an external file

//define the code that will be converted to pdf

$mpdf->WriteHTML('this is a test html');

  • Set the display mode, this is an optional function but you can as well include it

//define display mode

$mpdf->SetDisplayMode('real');

The options to use in the display mode are either real, fullpage, fullwidth, none, or default.

  • Use the output function to generate the pdf file as a preview in the browser and also set the name of the pdf file while you want to download the file and save it to the local directory

//output in browser using the output function and define the filename

$mpdf->Output("mpdf_file.pdf","I");

The full code to generate a pdf file using mPDF in PHP is as follows

<?php

//include the mpdf library through the autoloader

include("mpdf/vendor/autoload.php");

 

//initialize mpdf function

$mpdf = new \\Mpdf\\Mpdf();

 

//define the code that will be converted to pdf

$mpdf->WriteHTML('this is a test html');

 

//define display mode

$mpdf->SetDisplayMode('real');

 

//out put in browser using the output function and define the filename

$mpdf->Output("mpdf_file.pdf","I");

?>

Create a pdf file using mPDF by defining a variable

<?php

//include the mpdf library through the autoloader

include("mpdf/vendor/autoload.php");

 

//initialize mpdf function

$mpdf = new \\Mpdf\\Mpdf();


$htmlcode='this is a test html to be converted to pdf';

//define the code that will be converted to pdf

$mpdf->WriteHTML($htmlcode);

 

//define display mode

$mpdf->SetDisplayMode('real');

 

//out put in browser using the output function and define the filename

$mpdf->Output("mpdf_file.pdf","I");

?>

How to generate pdf file using mPDF by including an external file

You can use the file get contents to create a pdf file using a file that is defined as a different file

<?php

//include the mpdf library through the autoloader

include("mpdf/vendor/autoload.php");

 

//initialize mpdf function

$mpdf = new \\Mpdf\\Mpdf();

 

//define the code that will be converted to pdf

$mpdf->WriteHTML(file_get_contents('https://www.solutionspacenet.com/code_examples/pdf/pdf.html'));

 

//define display mode

$mpdf->SetDisplayMode('real');

 

//out put in browser using the output function and define the filename

$mpdf->Output("mpdf_file.pdf","I");

?>