mPDF 5.7.1 - image displays as a broken [x]

Including images is kinda tricky in mPDF. I had some issues as well. I found more kind of problems.

At first you should turn on debug variable:

$mpdf = new mPDF();    
$mpdf->showImageErrors = true;

Usualy people don't have installed GD module for PHP. On linux machine, execute:

sudo apt-get install php5-gd
sudo service apache2 restart

On Windows servers, php_gd2.dll is included in a standard PHP installation, but is not enabled by default. To enable it, uncomment the extension=php_gd2.dll line in your php.ini file (remove the # from the beginning of that line) and restart the PHP extension. [1]

If you get this error you probably see [x] image:

mPDF error: IMAGE Error (http://www.domain.com/directory/image.jpg): Could not find image file

Check the url if your image exists and if the image is accessible. If yes then you can try to change absolute URL to relative. You can try both versions:

<img src="directory/image.jpg">
<img src="./directory/image.jpg">

Actually I also had a problem with PNG formats. Converted PNG image to JPG worked fine.

Linking in mPDF templates should be same like for whole your framework/system.


I have encountered the same problem after migrating the script.

The problem was, that the tmp directory inside the mpdf folder was not writable. I hope it helps someone.


I had the same problem with PNG images being displayed as [X] when to generate PDFs with mPDF.

I added: $mpdf->showImageErrors = true;

After: $mpdf = new Mpdf();

and got the error message:

GD library required for PNG image (alpha channel)#

So after running apt-get install php5-gd generating a PDF with a PNG worked like a charm!

Tags:

Html

Php

Image

Mpdf