1

I am getting the same problem as TCPDF don't display image with writeHTML. Everything works just fine in my localhost server, but the images is not showing up when deployed to the live server.

I have tried the following steps:

  1. The images are in base64, so it's nothing to do with the path or file permission. I printed out the base64 string and pretty sure it's normal.
  2. Both png and jpeg format don't work
  3. Both TCPDF Image() and writeHTML() with don't work
  4. The quote mark is '<img src="..." />' instead of "<img src='...' />"
  5. Adding width and height doesn't work.

Here is the code:

// $data is the base64 string       
$data = base64_decode($scoreImg);
$pdf->Image('@'.$data);  // this doesn't work
$html = '<img src="data:image/jpeg;base64,' . $scoreImg . '"  width="50" height="50">';
$pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true); // this doesn't work as well

Anyone please save my day.

Jia Yin Foo
  • 41
  • 1
  • 5

3 Answers3

0

Get the image data from somewhere, then encode in base64, and set the image with the @ symbol:

$data = base64_encode($data);
$pdf->Image('@'.$data);
James Young
  • 316
  • 2
  • 6
  • Hi James, thanks for your response. This is the way how I do it in the beginning, but it only works on my local server. – Jia Yin Foo Jul 06 '21 at 09:49
0

With a properly base64 encoded image string, you should be able to do it thru Image('@') command and the writeHTML statement

Please try this (please replace the tcpdf.php file PATH by your own one) - it will generate and download a pdf with a red dot image:

<?php
require_once('./tcpdf/tcpdf.php');

$pdf = new TCPDF("P", PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

$pdf->AddPage();

$data='iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==';

$imgdata = base64_decode($data);
$pdf->Image('@'.$imgdata);
$pdf->writeHTML($html, true, false, true, false, '');
$pdf->Output('ken1.pdf', 'D');
?>

The above $data string is a base64_encoded image file string. For example like the following

$data = base64_encode(file_get_contents("reddot.png"));
Ken Lee
  • 6,985
  • 3
  • 10
  • 29
0

It's very late but I found the answer here: https://programmierfrage.com/items/does-tcpdf-have-limits-on-base64-images

It is to redefine the tmp variable:

define('K_PATH_CACHE', '/tmp/');

With this it works fine for me.

guenter47
  • 457
  • 5
  • 13