mPDF 5.7.1 – image displays as a broken [x]

Posted on

mPDF 5.7.1 – image displays as a broken [x] – Here in this article, we will share some of the most common and frequently asked about PHP problem in programming with detailed answers and code samples. There’s nothing quite so frustrating as being faced with PHP errors and being unable to figure out what is preventing your website from functioning as it should like php and html . If you have an existing PHP-based website or application that is experiencing performance issues, let’s get thinking about mPDF 5.7.1 – image displays as a broken [x].

I have a small issue with mPDF (version 5.7.1).

This code should generate PDF with image file:

 $mpdf = new mPDF();
 $html = '<img src=""/>';
 $mpdf->debug = true; 
 $output = $mpdf->Output(); 

Well there is no image but an [x] instead.

I’ve googled enough to get to the conclusion that it has to be done this way but I also tried realpath to the file. Still nothing.

The only thing I haven’t tried is <img src="logo11w.png"> and copying the image into the folder because I don’t know into which folder I should copy file logo11w.png.

Any suggestions?

Solution :

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!

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 ( 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.

For me, it is working as of now. Hope this will help someone.

Solution : Try relative path of image instead of URL. Image must be hosted on the same server.

Ex: /var/www/mysite/image/xyz.jpg

Just experienced this issue today with an old website.

If you have mPDF <= 6.0 and you are now experiencing this issue when it was previously working. The reason is that there is an SSL error (expired certificate, invalid certificate, etc). The solution is the fix the SSL certificate installation; however, if you want a quick fix you can edit the mpdf.php file to ignore SSL errors during CURL requests. Simply add:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

Inside the functions:




Ok. After the last comment on question (14:30). Here the exact same code that IS working with mPDF 5.4. And it saves it’s output on scripts directory as test.pdf. Firewall issues?


I have the following directories / files:

  • images
    • wallpaper01.jpg
    • wallpaper02.jpg
  • index.php (source code)

So these image files are relatively starting from the script directory as it looks like.

require_once __DIR__ . DIRECTORY_SEPARATOR . 'MPDF/mpdf.php';

try {
    $mpdf = new mPDF(); 
    $mpdf->WriteHTML('<img src="images/wallpaper01.jpg" alt="" width="480">');
    $mpdf->WriteHTML('<img src="images/wallpaper02.jpg" alt="" width="480">');
    $mpdf->Output(__DIR__ . DIRECTORY_SEPARATOR . 'test.pdf', 'F');
} catch(Exception $e) {
    echo $e;

Fully working example (download)

In my project i fix problem and solution is:

Set src absolute path on the server example:
src="/var/www/myproject/images/logo.png" if image is on the same server.
If image is from external server src is absolute path example:
Hope this will help someone.

It’s enough
The solve is :

$mpdf->curlAllowUnsafeSslRequests = true;

It’s all

In my case, the problem was just the usage of transparents PNG using the 6.0 version. This bug was fixed on the mpdf 6.1 version.

I successfully solved the problem downloading the 6.1 version from here and overriding the mpdf files on my project.

You can try this:

$mpdf->imageVars['myvariable'] = file_get_contents('alpha.png');


$html = '<img src="var:myvariable"/>';

after there, you should do:

$mpdf->Image('var:myvariable', 0, 0);

read more about this in documentation:
mPDF Load Image

change to
remove the second line before initializing $html
its undefined at that time.
new code will be as

$mpdf = new mPDF();
$html= "<img src='' alt=''>";
$mpdf->debug = true; 
$output = $mpdf->Output(); 

Another reason might be you’re running your application in Docker (or another container) and you’re using a domain that can’t be resolved.

Adding an alias in my docker-compose file resolved the issue for me.

      - your.local.domain

To find out if this is the reason, log into the running container and attempt to request the file via curl/wget/etc.

.htaccess with password also can blocks access

An exception can be added:

AuthType Basic
AuthUserFile "/home/"
require valid-user
AuthName "set password"
Order allow,deny
# payu
Allow from
Allow from
Allow from
Allow from
Allow from
Allow from 
# serwer localhost
Allow from
satisfy any

Had the same issue today. Problem was that the file was in another folder on the same server of the file calling the “save pdf” function.


Leave a Reply

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