PHP 7 TCPDF Example to Insert Base64 Encoded Image inside PDF Document in Browser Full Project For Beginners

  • Post author:
  • Post category:PHP
  • Post comments:0 Comments

 

 

Well, you can actually, looking at the code you just neet to add a ‘@’ before the base64 encoded string:

 

 

$img_base64_encoded = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA0gA...';

$img = '<img src="@' . preg_replace('#^data:image/[^;]+;base64,#', '', $img_base64_encoded) . '">';

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

 

 

You cannot use base64 stream in src rather first save the stream to a file then use it

 

 

$img_base64_encoded = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA0gA...';
$imageContent = file_get_contents($img_base64_encoded);
$path = tempnam(sys_get_temp_dir(), 'prefix');

file_put_contents ($path, $imageContent);

$img = '<img src="' . $path . '">';
$pdf->writeHTML($img, true, false, true, false, '');

Leave a Reply