Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TCPDF and insert an image base64 encoded

Tags:

php

tcpdf

I've this string from Mysql db:

$img_base64_encoded = 

'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+PCFET0NUWVBFIHN2ZyBQVUJMSUMgIi0vL1czQy8vRFREIFNWRyAxLjEvL0VOIiAiaHR0cDovL3d3dy53My5vcmcvR3JhcGhpY3MvU1ZHLzEuMS9EVEQvc3ZnMTEuZHRkIj48c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmVyc2lvbj0iMS4xIiB3aWR0aD0iMzgiIGhlaWdodD0iNTIiPjxwYXRoIHN0cm9rZS1saW5lam9pbj0icm91bmQiIHN0cm9rZS1saW5lY2FwPSJyb3VuZCIgc3Ryb2tlLXdpZHRoPSIyIiBzdHJva2U9InJnYigxMjEsIDEyMSwgMTIxKSIgZmlsbD0ibm9uZSIgZD0iTSA5IDggYyAtMC4wMyAwLjM1IC0xLjE5IDEzLjIzIC0yIDIwIGMgLTAuMjEgMS43MiAtMC40MSAzLjQ4IC0xIDUgYyAtMS4wMyAyLjY3IC0zLjI2IDUuMzMgLTQgOCBjIC0wLjgzIDMgLTEgMTAgLTEgMTAiLz48cGF0aCBzdHJva2UtbGluZWpvaW49InJvdW5kIiBzdHJva2UtbGluZWNhcD0icm91bmQiIHN0cm9rZS13aWR0aD0iMiIgc3Ryb2tlPSJyZ2IoMTIxLCAxMjEsIDEyMSkiIGZpbGw9Im5vbmUiIGQ9Ik0gOSAyOSBsIDExIC01Ii8+PHBhdGggc3Ryb2tlLWxpbmVqb2luPSJyb3VuZCIgc3Ryb2tlLWxpbmVjYXA9InJvdW5kIiBzdHJva2Utd2lkdGg9IjIiIHN0cm9rZT0icmdiKDEyMSwgMTIxLCAxMjEpIiBmaWxsPSJub25lIiBkPSJNIDIxIDYgYyAwIDAuNjEgMC41MiAyNS4wNiAwIDM1IGMgLTAuMDUgMS4wMiAtMiAzIC0yIDMiLz48cGF0aCBzdHJva2UtbGluZWpvaW49InJvdW5kIiBzdHJva2UtbGluZWNhcD0icm91bmQiIHN0cm9rZS13aWR0aD0iMiIgc3Ryb2tlPSJyZ2IoMTIxLCAxMjEsIDEyMSkiIGZpbGw9Im5vbmUiIGQ9Ik0gMzUgMSBjIDAuMDQgMC4wNSAxLjg5IDEuOTggMiAzIGMgMC40OCA0LjUzIDAuNzUgMTEuMTQgMCAxNiBjIC0wLjUgMy4yNyAtMy4zOCA2Ljc1IC00IDEwIGMgLTAuNjMgMy4zMiAwIDExIDAgMTEiLz48L3N2Zz4='

It show "HI" in a image. Now i want to embed this image encoded directly inside a PDF generated with TCPDF plugin but always get error:

TCPDF ERROR: [Image] Unable to get the size of the image: 

And this is how to insert it:

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

I've also tried with:

  $pdf->Image('@' . $img_base64_encoded);

with the same error.

like image 589
itajackass Avatar asked Dec 31 '17 15:12

itajackass


3 Answers

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, '');

Tested with the last version of TCPDF

like image 107
Gianluca Avatar answered Sep 27 '22 23:09

Gianluca


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, '');
like image 32
Vinay Avatar answered Sep 27 '22 23:09

Vinay


No need to write as HTML

    $img = base64_decode(preg_replace('#^data:image/[^;]+;base64,#', '', $img_base64_encoded));
    $this->tcpdf->Image("@".$img, 68, 208, 46, 46);
like image 43
sdespont Avatar answered Sep 27 '22 22:09

sdespont