mPDF 5.7.1 - image displays as a broken [x]

2020-02-23 06:40发布

I have a small issue with mPDF (version 5.7.1).

This code should generate PDF with image file:

 $mpdf = new mPDF();
 $html = '<img src="https://www.google.pl/images/srpr/logo11w.png"/>';
 $mpdf->WriteHTML($html);
 $mpdf->debug = true; 
 $output = $mpdf->Output(); 
 exit();

Well there is no image but an [x] instead.

I've googled enough to get to the conclusion that it has to be done this way but I also tried realpath to the file. Still nothing.

The only thing I haven't tried is <img src="logo11w.png"> and copying the image into the folder because I don't know into which folder I should copy file logo11w.png.

Any suggestions?

10条回答
Root(大扎)
2楼-- · 2020-02-23 07:20

You can try this:

$mpdf->imageVars['myvariable'] = file_get_contents('alpha.png');

or

$html = '<img src="var:myvariable"/>';
$mpdf->WriteHTML($html);

after there, you should do:

$mpdf->Image('var:myvariable', 0, 0);

read more about this in documentation: mPDF Load Image

查看更多
仙女界的扛把子
3楼-- · 2020-02-23 07:24

In my project i fix problem and solution is:

Set src absolute path on the server example: src="/var/www/myproject/images/logo.png" if image is on the same server. If image is from external server src is absolute path example: src="https://www.google.bg/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png". Hope this will help someone.

查看更多
放我归山
4楼-- · 2020-02-23 07:25

For me, it is working as of now. Hope this will help someone.

Solution : Try relative path of image instead of URL. Image must be hosted on the same server.

Ex: /var/www/mysite/image/xyz.jpg

查看更多
欢心
5楼-- · 2020-02-23 07:26

I had the same problem with PNG images being displayed as [X] when to generate PDFs with mPDF.

I added: $mpdf->showImageErrors = true;

After: $mpdf = new Mpdf();

and got the error message:

GD library required for PNG image (alpha channel)#

So after running apt-get install php5-gd generating a PDF with a PNG worked like a charm!

查看更多
登录 后发表回答