4

I have been tearing my hair out with this for two days now and I cant help but think its something ridiculously simple.

When trying to use standard jpg images for the mpdf library I am getting the following image error...

<B>mPDF error: </B>IMAGE Error (https://www.example.net/myimage.jpg): Could not find image file
  1. I have tried both http and https
  2. I have tried the solutions here including editing the servers hosts file, checking permissions
  3. No matter what the url of the image is the problem occurs (an external image also gives the same problem)
  4. I have made sure that php gd and curl is installed on the server
  5. I have repeatedly checked to make sure the image paths are correct by directly copying and pasting the link from the error.

Things to note,

  • I had this working on another server (but it was shared hosting so everything was already set up, ive compared the two using phpinfo() and the only difference I can see is that ive a slightly newer php version.
  • I was also having another issue since moving the site which also involved reading urls where I was getting a 404 not found error even though the url was correct
  • I am using my hosts file on Windows to point the domain to this server so it's not live yet
Community
  • 1
  • 1
Adrian
  • 1,976
  • 5
  • 41
  • 105
  • I've recently been having the same problem with curl and mpdf and it was the problem with hosts - you have already mentioned. So if there is a way to try something else in that direction - I would recommend to do it. I had also found a similar answer here http://stackoverflow.com/a/9579996/1332837 – Pavel Bariev Apr 08 '15 at 08:01
  • I never really resolved the problem, but I set up a replica dev site on the exact same server with its own real live url and it worked perfectly so it was definitely related to the hosts file and not a server set up issue. However with all the changing and messing I tried to do with hosts files both on the server and locally I could not get it working on the "live" version. I took the risk and changed the A record and simply made it live for real (knowing that it wasnt a php or server set up issue) , and it worked. – Adrian Apr 08 '15 at 09:33
  • Possible duplicate of http://stackoverflow.com/questions/23514062/mpdf-not-rendering-images-mpdf-error-image-error-could-not-find-image-file – That Brazilian Guy Dec 14 '15 at 18:34

2 Answers2

1

I manage to solve this issue on local development

My set up is as the following:
XAMPP on windows 10
php version 5.6.35
ssl enabled
mpdf version 5.4

The issue was related with SSL/TLS version incompatibility. As for the solution I patched mpdf.php

Add the following function on mpdf.php before the end of class

private function _curl($url)
{
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_ENCODING,"");
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_TIMEOUT,10);
    curl_setopt($ch, CURLOPT_FAILONERROR,true);
    curl_setopt($ch, CURLOPT_VERBOSE, true);

    $data = curl_exec($ch);
    curl_close($ch);

    return $data;
}

Then add this line to call the above function inside the function _getImage (around line 9283 ~ something, I might have different line number than yours) before throwing error message

// patch
if (empty($data)) {
    $data = $this->_curl($file);
}
// end patch

if (!$data) { return $this->_imageError($file, $firsttime, 'Could not find image file'); }

This method is to prevent altering too much of the mpdf library itself.

Notes: This solution might not be working for all setup! You can refer to the link below to read more.
Ref: https://stackoverflow.com/a/42185532/209247

mdennisa
  • 177
  • 2
  • 13
  • I can confirm the patch works, but I'm still stumped why it fails to find the image which is right there, and weirdest part is it's not happening in my local but on my actual server instance! I have checked permissions and everything and still couldn't figure out!!! – Anupam Mar 12 '23 at 16:05
0

You could also try adding the following before the curl_exec to see what the error message is...

    curl_setopt($ch, CURLOPT_FAILONERROR,true);
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    curl_setopt($ch, CURLOPT_STDERR, fopen('php://output', 'w'));
user1432181
  • 918
  • 1
  • 9
  • 24