Install MPDF 7.x on

| By Webner

How to install MPDF 7.x for PHP version > 5.6 on and use in PHP Project?

Since from January 2019, has been resisting the use of PHP version 5.6, it is supporting PHP versions >= 7.1. Currently, PHP version is 7.2 is highly recommended by is charging extra money for working with PHP version 5.6.

We have mpdf 5.7 on one of our PHP projects hosted on . MPDF 5.7 works for PHP <= 5.6. When we upgrade PHP version from 5.6 to 7.2 as recommended by, then we face a problem that pdf is not generated with MPDF 5.7 for PHP version 7.2. So, we need to upgrade MPDF library too with upgradation of PHP version.

MPDF 7 has different procedure/approach for installation than MPDF 5.7. The MPDF 5.7 setup method was simple, just download the zip/tar.gz file and extract it in your project and include the mpdf in your file.

To install the MPDF 7.x, we need to install composer and its packagist package mpdf/mpdf.

Let us see how we can install composer and then MPDF 7.x package on PHP project hosted on

If a composer is already installed in PHP project hosted on the server, then you can skip step 1, otherwise, go to step 2.

Step 1: Install composer in PHP project hosted on

1. Connect to your PHP project via ssh connection credentials on the terminal with following command
a. ssh user_name@server_name
b. Above command asks for ssh password. Enter the password and you will be connected to your hosted server.
2. Open your project directory with the command: cd project-name.
3. Now, complete installation of the composer with the following actions/commands
a. Download composer installer with the following command
curl -sS| /usr/bin/php7.1-cli
b. Create composer json file with following command: vi composer.json
c. Edit mode will be opened for composer.json file, type the following:

    			"require": {
        				"cocur/slugify": "^1.3"

d. Now, save the changes in composer.json file.
4. Now, with following command composer will install slugify as mentioned in the composer.json file.
php7.1 composer.phar install

Note: It is not necessary to install slugify, you can try with phpmailer or any other package.

Step 2: This is the main step to install MPDF 7.x for php version >= 7.1 & <= 7.3 1. Just type the following command to install MPDF 7.x : /usr/bin/php7.1 composer.phar require mpdf/mpdf

2. You can see that in your php project having name, for example, project-name, a vendor folder is created and in vendor folder, mpdf library and other dependencies have been created, composer.json and composer.lock files have also been created.

Step 3: We are now in the final step, that is, how to use mpdf 7.x in php project to create a PDF file.
1. Create a php file, for example, index.php file.
2. Write the following code:

$html = '<h1>Hello world</h1>';
require_once __DIR__ . '/vendor/autoload.php';
$mpdf = new \Mpdf\Mpdf();
3. Now, open on browser and check. If following error is displayed
   Fatal error: Uncaught Mpdf\MpdfException: Temporary files directory"path/project-name/vendor/mpdf/mpdf/src/Config/../../tmp" is not writable in path/project-name/vendor/mpdf/mpdf/src/Cache.php:17

then it can be rectified by using this changed code statement:

   $mpdf = new \Mpdf\Mpdf(['tempDir' => sys_get_temp_dir().DIRECTORY_SEPARATOR.'mpdf']);
Sometimes, browser specific issues also occur. In Firefox, with this code, everything works fine but in chrome, PDF does not open and the error message is displayed: Failed to load the document. 
With just adding the following line above require_once statement in above PHP code this issue was resolved on my system: 

Leave a Reply

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