6
votes

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?

10

10 Answers

9
votes

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.

28
votes

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
2
votes

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

2
votes

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

1
votes

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">
0
votes

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

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

I had to 'resave' the images:

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

Then it worked.

0
votes

It is also posible add image data inline as base64:

<img src="@Base64encodedImageFile" />
0
votes

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.

0
votes

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.