mPDF not rendering images (mPDF error: IMAGE Error

2020-02-11 04:10发布

问题:

We are getting plagued with this which started in April on working server. Everything was fine with our application until the customer reported that PDFs were no longer displaying the images.

Our PDF is generated via a HTML render first. When the HTML render is displayed the Image shows correctly. Also the image shows correctly if the image URL as noted in the mPDF is copied and pasted into a new tab.

However... If we load the image from a DIFFERENT DOMAIN then the image is rendered correctly. loading the image via absolute path, relative path or URL path all result in this error:

mPDF error: IMAGE Error (http://www.aibsonline.co.uk/logo.gif): Could not find image file

But, as you will see the logo url works when pasted. File permissions have been tested (which is why it is in the root) as standard and up to 777. Server is a Linux server in both cases we have seen so far.

HTML Code that renders the logo:

<div id="logo_wrapper" class="left">
<img width="107" height="76" src="<?php echo base_url('logo.gif'); ?>" />
</div>

At a real loss with this one and it is beginning to affect more and more clients.

Any help gratefully received.

UPDATE

The image renders if the rendering code and the image are in the same directory and we do NOT use an absolute path, eg.

<img width="107" height="76" src="logo.gif" />

回答1:

I ran across this issue today. My problem was that the domain name I was using didn't resolve back to the server when accessed from the server. I added an entry to the hosts file on the server, and images started showing.

Your equivalent hosts file entry of what fixed my problem would be:

127.0.0.1 www.aibsonline.co.uk

Or otherwise ensure that www.aibsonline.co.uk resolves to your server in the DNS that your server uses.

It appears that mPDF accesses images as a cURL web client, so DNS on the server needs to be configured correctly to refer back to itself.



回答2:

I had a similar issue and I solve by following:

1.Check if gd library is installed and enable in your php ini file. If not install gd library.

2.Turn on debug variable

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

3.Try inter-changing absolute/relative path for the image

<img src="http://someDomain/directory/image.jpg">
<img src="./directory/image.jpg">

Hope this helps.



回答3:

With same issue, I found that get_headers() returns:

"HTTP/1.1 412 Precondition Failed"

Provider says this was due to web firewall, because the request was badly formed and the user_agent not set.

An ini_set('user_agent', 'Mozilla/5.0'); solved the problem.

In a WordPress plugin using mpdf, mpdf doesn't use WordPress http classes and doesn't set user_agent. I solved this by adding in mu-plugins:

global $wp_version;

ini_set('user_agent',apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' ) ));


回答4:

Just make sure that you have image related functions in your PDF class file. Like parsejpg, parsepng, parsegif etc...



回答5:

I was running into the same issue. mPDF was running DOG SLOW and would end up just displaying a "Cannot find image file" error. After

  1. checking to make sure the image is actually there
  2. checking the permissions on the file
  3. checking all of the error logs on the server
  4. combing through the documentation on mPDF

I found that the image file in question was corrupted somehow. I downloaded the image to my local computer, opened it up with an image editor (Paint.NET in this case), re-saved it as a .gif file and uploaded it to the server again. That seemed to fix it. Your mileage my vary.

Also, for what its worth, I had to use images/image.gif instead of the file path /images/image.gif.



标签: php mpdf