kartik-v / yii2-mpdf

A Yii2 wrapper component for the mPDF library which generates PDF files from UTF-8 encoded HTML.
http://demos.krajee.com/mpdf
Other
161 stars 150 forks source link

Images not showing on production but they do on local environment #19

Closed evilito closed 7 years ago

evilito commented 9 years ago

Hello, I have a couple of simple PNG images in a view print.php, like this:

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

In the local environment, the images shows fine in the generated PDF and even if I just print it without PDF (controller code):

        return $this->renderPartial('print', [
            'model' => $this->findModel($id),
        ]);

However, on the production server, the images are not showing. They show only if I print the view without PDF (like in the previous code), so for me this means that the images are fine, no broken links (because they show on renderPartial) and leads me to think that there might be some issue with the mpdf?

The generated PDF in the production server shows a red "X" where the image should be loaded. How can I troubleshoot this? Can the PDF be examined? If it was an HTML page I would check where the link in "src" is leading, but in the PDF I'm not sure how to proceed.

Local environment is PHP 5.5 and production is PHP 5.6. Does this has something to do?

Any help will be really welcomed and appreciated!

evilito commented 9 years ago

The problem was that mpdf doesn't understand well relative paths for images, even if the web browser is fine with them and display the images correctly.

The fix was to generate a path with an absolute base URL, in this case, by setting the second parameter of the "Url::to" function to true, as per the documentation.

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

I hope this helps others having the same issue. Thank you.

ivanlemeshev commented 8 years ago

The absolute URL didn't solve this problem to me. But using base64 image instead solve it.

shakalvv commented 8 years ago

I had the same problem. The absolute URL and base64 -- didn't work. You can set $this->_mpdf->showImageErrors = true; in file ... /vendor/kartik-v/yii2-mpdf/Pdf.php in method setApi(). Then you can see the error. In my case, I had issues with gd (Error parsing image file - image type not recognised, and not supported by GD imagecreate). I changed picture and it helped.

sunilsoftkochi commented 8 years ago

My problem was a different one and i had problem in local server as well as in the hosting server. I solved problem with a shortcut that I copied the images to web folder instead of database. After the change the application worked without issues.

spirit-of-wars commented 7 years ago

The absolute URL and base64 -- didn't work me too. I install php5-gd and this solved my problem.

bdionis commented 7 years ago

HI, just try to install php7.0-gd http://stackoverflow.com/a/22575654/6547455

parambursys commented 7 years ago

Hi, just clean browser history or update browser.

josmanaba commented 6 years ago

Inside the MPDF directory there is a subdirectory "tmp", you need to give write permissions to it.

MloneThakuri commented 6 years ago

i have problem in uploading the img in yii2, my yii2 framework is in latest version. And i have uploaded some img in home page using code (src="img/product.jpg") and it's working but when i click on another page and come back to the home page , the img is not displaying and it needs code (src="../img/product.jpg") to display it on home page. so why is that? please help me solve this problem.

aBahrami13 commented 2 years ago

I had a same problem because of SSL error! If peer verification fails in ssl hand shake, it will not show the image. I 'm using 8.0.15 version of mpdf, in getImage() function of vendor/mpdf/mpdf/src/Image/ImageProcessor.php the file_get_contents is used and there is no way to pass context options.