Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

print BASE64 coded image into a FPDF document

Tags:

php

base64

fpdf

I've some as base64 stored images in a database. Is it possible to print those data directly in a PDF document, using FPDF?

Data sctructure of the image

data:image/png;base64,iVBORw0KGgoAAAANSUhEUg7AAAfQAAACWCAYAAAAonXpvAAAfgUlEQVR4Ae2dC9BuVVnHuSN3QaBENA9h5A2IcJxMj8hUkymoQDfClIkmrQZr1EydMqapnKRMc8JmHLBBHadJJaV0lKQDZCmCQiqQ2KERUFARDhcFDpx /33W tznPe/ 3ttaa9/ a2Z9/32Xvt5nvVbl2evtdfee9dtS27bt4mACJmACJm8ACJtBvArv123xbbwImYAImYAImIAJ26K4HJmACJmACJjAAAnboAyhEZ8EETMAETMAE7NBdB0zABEzABExgAATs0AdQiM6CCZiACZiACdihuw6YgAmYgAmYwAAI2KEPoBCdBRMwARMwAROwQ3cdMAETMAETMIEBELBDH0AhOgsmYAImYAImYIfuOmACJm

I think $pdf->imagepng() should be the right function in FPDF.

like image 327
Thomas1703 Avatar asked Aug 28 '13 09:08

Thomas1703


1 Answers

I know this is an old topic, but there is an easier way of solving this problem. Just try this simple steps:

  1. Strip data:image/png;base64 from URI using explode
  2. Concatenate data://text/plain;base64, with what is left from the stripped URI
  3. Use the result string(base64) as argument of FPDF Image()

Example:

$dataURI = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAPCAMAAADarb8dAAAABlBMVEUAAADtHCTeKUOwAAAAF0lEQVR4AWOgAWBE4zISkMbDZQRyaQkABl4ADHmgWUYAAAAASUVORK5CYII=";

$img = explode(',',$dataURI,2)[1];
$pic = 'data://text/plain;base64,'. $img;

$pdf = new FPDF();
$pdf->AddPage();
$pdf->Image($pic, 10,30,0,0,'png');
$pdf->Output();

Remember to choose the correct image type for Image() method call.

like image 87
Pedro Soares Avatar answered Sep 21 '22 03:09

Pedro Soares