TCPDF don't display image with writeHTML

橙三吉。 提交于 2019-12-10 15:11:57

问题


this question seems an evergreen on TCPDF...
I have an issue that's driving me crazy.

I have an html code that I use as "template" for my PDF, here I have my company logo.
Everything works fine on localhost (Windows), but when I move online, the image is not shown.
Pay attention: I don't get any error (ie the Unable to get image error) on my PDF the image is simple blank!
Infact if I click on the PDF on the position where the images it's supposed to be, I can select it, and Adobe enables the option "Copy image".

Obviously the image exists, is here, and permission are correct.
If I try to surf there, or view the generated HTML page, everything is fine.

This is the PHP code:

$pdf->SetMargins($params->get('pdfMarginLeft', 15), $params->get('pdfMarginTop', 27), $params->get('pdfMarginRight', 15));
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->SetFont('helvetica', '', 8);
$pdf->AddPage();

$pdf->writeHTML($html, true, false, true, false, '');

$pdf->lastPage();

Then this is my HTML code (I just skipped everything except the image):

<img alt="logo black" src="../images/logo_black.png" height="60" width="210" />

I've tried with the url (relative and absolute) and with the path (relative and absolute), the problem still occurs. Any ideas?


回答1:


As things are working locally so you may try changing the image type from png to jpg and check after modifying your code accordingly and uploading the jpg on the server.




回答2:


This wasn't your problem, but it's a possible solution for people with a similar issue in the future. Please make sure the HTML attributes have double quotes.

$html = "<img src='...' />"; // This will not work

$html = '<img src="..." />'; // This will work



回答3:


You can convert image type "jpg/png" to base64. <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgA...."/> this may help you !




回答4:


In my case IMG tag not working until I write full path to file

Not working

<img src="/pdfrender/XXX.jpg" width="50" height="50">

Working (localhost example)

<img src="http://site.local/pdfrender/XXX.jpg" width="50" height="50">



回答5:


I implemented a str_replace for the image src, and that works ok now.

$html = str_replace("../images", $_SERVER["DOCUMENT_ROOT"] . '/images', $html);



回答6:


I had to 'resave' the images:

$image = 'images/logo_example.png';    
imagepng(imagecreatefrompng($image),$image);

Then it worked.




回答7:


In my case, I tried every solution above to no avail. It turned out I was missing width and height attributes. Adding those in, and using a root path ended up working for me:

<img src="/images/image.png" width="50" height="50"/>



来源:https://stackoverflow.com/questions/13702891/tcpdf-dont-display-image-with-writehtml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!