5

I'm stuck in very awkward situation where Images are being shown in Local Environment while generating PDF. But, Not in Production. Images being displayed as [X] when to generate PDFs with mPDF.

After inserting $mpdf->showImageErrors = true; in Controller.

public function actionExportCasesPdf($id) {
  .
  .
  .
  .
  $mpdf = new \mPDF();
  $mpdf->showImageErrors = true;
  $mpdf->WriteHTML($output);
  $mpdf->Output($fileName, 'D');
}

Error

MpdfException

IMAGE Error (..17.jpg): Error parsing image file - image type not recognised, and not supported by GD imagecreate

Even, GD library is installed in the server using apt-get install php5-gd command. And, Image Path are also used correct.

I tried to keep image source as such. But, No Luck.

<img src="<?= \yii\helpers\Url::to('@web/images/logo.png', true) ?>" width="100" alt="logo" />

I searched and tried the solution given by these links. But, still no luck :

  1. Images not showing on production but they do on local environment - GitHub
  2. mPDF 5.7.1 - image displays as a broken [x]
  3. Generated picture in mpdf
  4. Error parsing image file

Any help/hint/suggestion is appreciable.

Community
  • 1
  • 1
Nana Partykar
  • 10,556
  • 10
  • 48
  • 77

5 Answers5

3

Most probably the picture is not available for MPDF via the generated URL. Let's try to debug it :) Run the following code on both local and prod environments

$imageUrl = \yii\helpers\Url::to('@web/images/logo.png', true);
$image = file_get_contents($imageUrl);
echo (new \finfo())->buffer($image);
// echo $image; 
// ^^ uncomment, if previous line echoed something not image-related. 
// Maybe, you've got 404 error
Nana Partykar
  • 10,556
  • 10
  • 48
  • 77
SilverFire
  • 1,582
  • 13
  • 22
3

It throws one new error

failed to open stream: HTTP request failed! HTTP/1.1 401 Unauthorized

So, problem was : server was secured with password. So, I searched for it to find the way. And, File-get-contents failed to open stream Unauthorized is having correct answer related to above error.

http://user_name:password@your_site.com/append_your_url 

After appending user name, password and site name as above, It worked correctly.

Related Search

  1. Images not showing on production but they do on local environment - GitHub
  2. mPDF 5.7.1 - image displays as a broken [x]
  3. Generated picture in mpdf
  4. Error parsing image file
Community
  • 1
  • 1
Nana Partykar
  • 10,556
  • 10
  • 48
  • 77
  • Yes. After keeping this code. All issues were fixed. – Nana Partykar Sep 08 '16 at 16:35
  • HI How did you solve this, I am also facing an issue with my code. its giving error: Error Here: net::ERR_NAME_NOT_RESOLVED at http://usernam:pwd@%@http://example.com/wiki135 – Ashish Pathak Jun 18 '21 at 08:19
  • 1
    For me, this problem showed up as an incompatible image error - when it was actually an access issue. I store images (apache environment) in `/assets/images`; I had to add that path to `php_admin_value open_basedir` - then everything worked and error messages vanished. MPDF v8, PHP v7.4 – cloudxix Jun 22 '22 at 20:46
2

mpdf is not fetching transparent image while generating pdf, so make sure your image is not transparent.

It's working on my localhost but issue with aws server. Apart from aws it's work fine. Gd library is not allow to parse transparent iamge.

Kaushal Roy
  • 159
  • 1
  • 6
0

if you use cpanel on your server go to Hotlink Protection and active Allow direct requests (for example, when you enter the URL of an image in a browser). but your Privacy Policy must allow it screenshot form this section

sadegh
  • 46
  • 5
0

Special case
In my case my SSL certificate got expire so I renewed it. My application is hosted on AWS ( classic load balancer).
After upgrading SSL certificate everything start working but MPDF start giving error for image files.
After 3-4 hours I just try with delete ssl certificate using AWS Certificate Manager and import again same certificate. Magic happen and now its working.

Prahlad
  • 716
  • 8
  • 17