Nana Partykar
Nana Partykar

Reputation: 10548

MpdfException IMAGE Error () : Error parsing image file - Yii2

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.

Upvotes: 5

Views: 12680

Answers (5)

Prahlad
Prahlad

Reputation: 796

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.

Upvotes: 0

sadegh
sadegh

Reputation: 56

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

Upvotes: 0

Kaushal Roy
Kaushal Roy

Reputation: 189

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.

Upvotes: 3

Nana Partykar
Nana Partykar

Reputation: 10548

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

Upvotes: 4

SilverFire
SilverFire

Reputation: 1592

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

Upvotes: 3

Related Questions