8

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?

tampe125
  • 8,231
  • 7
  • 30
  • 45

10 Answers10

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
u32i64
  • 2,384
  • 3
  • 22
  • 36
DACrosby
  • 11,116
  • 3
  • 39
  • 51
  • 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. –  Oct 31 '16 at 02:41
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.

000
  • 3,976
  • 4
  • 25
  • 39
  • 2
    it can be a transparency issue..i am not sure..[here's a similar looking post](http://www.dolibarr.org/forum/527-bugs-on-a-stable-version/19407-tcpdf-error), which you may also go through where someone's suggesting a change in _tcpdf\_config.php_..check if that works for your _png_ logo.. – 000 Dec 06 '12 at 04:55
  • 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 working – Vishnu Bhadoriya Dec 05 '19 at 13:52
2

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

FreshFish
  • 21
  • 1
2

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

Barrett
  • 81
  • 1
  • 3
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">
  • 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. – Roland Nov 17 '17 at 13:04
1

It is also posible add image data inline as base64:

<img src="@Base64encodedImageFile" />
Murdej Ukrutný
  • 329
  • 1
  • 4
  • 10
0

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

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

I had to 'resave' the images:

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

Then it worked.

carla
  • 1,970
  • 1
  • 31
  • 44
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.

Peter
  • 1,247
  • 19
  • 33
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.

Erik
  • 303
  • 1
  • 3
  • 12