35

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="https://www.google.pl/images/srpr/logo11w.png"/>';
 $mpdf->WriteHTML($html);
 $mpdf->debug = true; 
 $output = $mpdf->Output(); 
 exit();

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?

Simon East
  • 55,742
  • 17
  • 139
  • 133
Mr.TK
  • 1,743
  • 2
  • 17
  • 22
  • 1
    mPDF will automatically download the resources which are external. Maybe try writing the PDF to disk: $mpdf->Output(__DIR__ . DIRECTORY_SEPARATOR . 'test.pdf', 'F'); – KoalaBear Feb 28 '14 at 11:53
  • Good idea, but not good enough... :( Still nothing. – Mr.TK Feb 28 '14 at 13:27
  • Try Relative URLS. It worked for me – Gireesh Jun 25 '19 at 06:23
  • My images were served that required the user to be logged on; curl wasn't and I had forgotten it also needed to be, wondering why the script got 404s while my browser showed the images just fine. – Herbert Van-Vliet Aug 14 '23 at 13:42

16 Answers16

97

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!

ddn
  • 1,360
  • 11
  • 8
  • Have to accept Your question. I won't be able to check the results but still helped me on a very similar issue on mpdf v6.0. That's weird that debug = true doesn't show what showImageErrors = true does. Thanks for Your help. – Mr.TK Nov 09 '15 at 08:16
  • 1
    This was really helpful. I ran `yum install php56w-gd` and restarted my web service and that did the trick. If you use yum, it might be `yum install php-gd` as well. – David Oct 17 '16 at 15:58
  • 2
    This solution worked for me. For PHP 7.0 the command is `sudo apt-get install php7.0-gd` – Nebster Oct 06 '17 at 08:03
  • I can still see any errors after adding `$mpdf->showImageErrors = true;`. Any help ? I have GD library installed too – Sudharshan Nair Jul 13 '18 at 10:58
  • This is how someone should response to questions on stackoverflow – Jagadish Meghval Oct 25 '21 at 14:30
25

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.

pevac
  • 591
  • 4
  • 8
15

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.

Dimitri L.
  • 4,499
  • 1
  • 15
  • 19
5

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

Krupal Patel
  • 512
  • 8
  • 12
2

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: src="https://www.google.bg/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png". Hope this will help someone.

2

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:

file_get_contents_by_curl

and

_get_file

Osmaan Shah
  • 345
  • 2
  • 8
1

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?

Edited

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)

KoalaBear
  • 2,755
  • 2
  • 25
  • 29
  • I was trying to do so on a local files (my own virtual host domain) so those are not firewall issues for sure. Tried to downgrade to 5.4 - didn't work as well. Tried changing owner and file mod - nothing. – Mr.TK Feb 28 '14 at 14:09
  • Did had time earlier. Made a full working example. Just place it in directory on your server and go. – KoalaBear Feb 28 '14 at 21:35
  • * Didn't had time earlier – KoalaBear Feb 28 '14 at 22:00
  • I am aware of this fact that it would work from script folder but i am working on a Zend Framework project so i need to have an order in app. Maybe some symbolic link will do the trick - i'm gonna check this but not now. Thanks for your help. If it will work don't be suprised that your reputation will grow. :) :P – Mr.TK Mar 01 '14 at 06:00
  • Haha. Good luck! Let us know :) – KoalaBear Mar 01 '14 at 07:42
1

It's enough The solve is :

$mpdf->curlAllowUnsafeSslRequests = true;

It's all

1

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.

JuliSmz
  • 996
  • 1
  • 12
  • 26
0

You can try this:

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

or

$html = '<img src="var:myvariable"/>';
$mpdf->WriteHTML($html);

after there, you should do:

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

read more about this in documentation: mPDF Load Image

Santos L. Victor
  • 628
  • 8
  • 13
0

I am using Code Ignitor and mpdf. After installing php-gd5 library, I had to re-install the php with configure command --with-gd to resolve the issue

Soumya Rajiv
  • 87
  • 1
  • 4
0

I encountered a similar problem using MPDF 8.1.x where I could not use URL paths for my images.

Eventually I determined it was caused because the "curlUserAgent" setting was using a very old user agent.

The setting was found here: /mpdf/src/Config/ConfigVariables.php

The curlUserAgent was set to:

'curlUserAgent' => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:13.0) Gecko/20100101 Firefox/13.0.1',

But I updated this to:

'curlUserAgent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:108.0) Gecko/20100101 Firefox/108.0',

and the images successfully loaded from URL source.

James
  • 51
  • 1
0

I faced the same issue after we moved our code to AWS Server.

After some debugging, I found the solution. We need to give write permissions to the mpdf/tmp directory.

After giving 0777 permission to mpdf/tmp directory, its working for me.

We don't need to give write permission to the whole mpdf directory. It may cause security issues.

Refer here

  • Giving any directory in production 0777 is a bad idea. I would suggest checking what user is accessing that directory via PHP or your web server before giving it public write. – Dave Jul 12 '23 at 01:18
  • Yes, but for the store temporary images, we should give write permission of the folder. We can create index.html in the directory for preventing unwanted access of the directory – Sumer Singh Solanki Jul 13 '23 at 02:17
-1

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='https://www.google.pl/images/srpr/logo11w.png' alt=''>";
$mpdf->WriteHTML($html);
$mpdf->debug = true; 
$output = $mpdf->Output(); 
exit();
user2706194
  • 199
  • 8
-1

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.

networks:
  app-network:
    aliases:
      - 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.

Adrian Lynch
  • 8,237
  • 2
  • 32
  • 40
-1

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.

copy("holdpath.bmp","samepath.bmp");
$writer->save("someotherpath.pdf");
unlink("samepath.bmp");
Zach Jensz
  • 3,650
  • 5
  • 15
  • 30