TCPDF don't display image with writeHTML
Asked Answered
R

10

9

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?

Relative answered 4/12, 2012 at 12:27 Comment(0)
T
9

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.

Telega answered 5/12, 2012 at 6:38 Comment(2)
it can be a transparency issue..i am not sure..here's a similar looking post, which you may also go through where someone's suggesting a change in tcpdf_config.php..check if that works for your png logo..Telega
i have an logo which is displayed multiple times in pdf but it only appearing once then i converted to png file to jpg and its works like charm.Dont know the reason behind this but anyhow it is workingSpeck
T
30

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
Trellas answered 11/12, 2014 at 22:53 Comment(1)
+1, disregard Rishi Kalia's answer because both PNG and JPEG worked for me. It's the outer quotes for src the solved the problem for me as well.Xeroderma
T
9

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.

Telega answered 5/12, 2012 at 6:38 Comment(2)
it can be a transparency issue..i am not sure..here's a similar looking post, which you may also go through where someone's suggesting a change in tcpdf_config.php..check if that works for your png logo..Telega
i have an logo which is displayed multiple times in pdf but it only appearing once then i converted to png file to jpg and its works like charm.Dont know the reason behind this but anyhow it is workingSpeck
A
3

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"/>

Apivorous answered 21/8, 2019 at 20:40 Comment(0)
L
2

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

Lugger answered 23/2, 2017 at 9:42 Comment(1)
this doesn't work... but the <img src="@Base64bin" doesFrugivorous
B
1

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">
Bombycid answered 5/5, 2016 at 12:43 Comment(1)
Please note that this way, your web statistics will contain TCPDF as user agent and it increases traffic on your server (you have to pay it twice, first the server's own request to itself + PDF output) which is you also don't want.Nikolia
D
1

It is also posible add image data inline as base64:

<img src="@Base64encodedImageFile" />
Dib answered 19/8, 2020 at 12:42 Comment(0)
V
0

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

$html = str_replace("../images", $_SERVER["DOCUMENT_ROOT"] . '/images', $html);
Vaasa answered 13/7, 2015 at 8:34 Comment(0)
G
0

I had to 'resave' the images:

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

Then it worked.

Gaitan answered 14/9, 2017 at 19:52 Comment(0)
F
0

Not really an answer but as everybody is trying to add a "brick" here is a very strange detail I'm facing:

I have small "strips" of HTML that I put, one after other on a page. If I put the first one, which has 2 png and a small text, on the file, the png are visibles on the PDF. If I put the second one, which has 2 png and a small text, on the file, the png are visible on the PDF. Now, if I put the two, ONLY the first one as the images. The second one as blank picture. If I change the picture of the second strip, images became visible.

Detail: 1st and 2nd strips are using the same images!

Notice this seems to happened ONLY with PNG image. So it seems there is a bug and you can't have two time the same PNG image on a page. Using JPG solve the problem.

Facer answered 5/12, 2020 at 13:21 Comment(0)
T
0

I had the same problem, locally it was working and on server blank image. I found out that htaccess password was the problem. I've put .htaccess file with the row Satisfy any to the folder with pictures and now it is working.

These answered 8/12, 2020 at 14:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.