Print BASE64 encoded image in FPDF document

I have some as base64 saved images in a database. Can I print this data directly into a PDF using FPDF?

Image Data Structure

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

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

+9
source share
6 answers

Although TCPDF was suggested in the comments, I wanted to say that there is a way to do this completely using FPDF. The thought process is as follows:

Always remember the mistake. If the image cannot be saved to the server, you obviously do not want to continue execution. Be sure to strictly check if the functions return false!

Here is my solution. In this example, I used a tiny image for a small URI.

 const TEMPIMGLOC = 'tempimg.png'; $dataURI = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAPCAMAAADarb8dAAAABlBMVEUAAADtHCTeKUOwAAAAF0lEQVR4AWOgAWBE4zISkMbDZQRyaQkABl4ADHmgWUYAAAAASUVORK5CYII="; $dataPieces = explode(',',$dataURI); $encodedImg = $dataPieces[1]; $decodedImg = base64_decode($encodedImg); // Check if image was properly decoded if( $decodedImg!==false ) { // Save image to a temporary location if( file_put_contents(TEMPIMGLOC,$decodedImg)!==false ) { // Open new PDF document and print image $pdf = new FPDF(); $pdf->AddPage(); $pdf->Image(TEMPIMGLOC); $pdf->Output(); // Delete image from server unlink(TEMPIMGLOC); } } 
+20
source

I know this is an old topic, but there is an easier way to solve this problem. Just try the following simple steps:

  1. Crop data : image / png; base64 from URI using explode
  2. The combined data://text/plain;base64, so that data://text/plain;base64, what remains of the remote URI
  3. Use the resulting string (base64) as the argument to FPDF Image()

Example:

 $dataURI= "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAPCAMAAADarb8dAAAABlBMVEUAAADtHCTeKUOwAAAAF0lEQVR4AWOgAWBE4zISkMbDZQRyaQkABl4ADHmgWUYAAAAASUVORK5CYII="; $img = explode(',',$logo,2); $pic = 'data://text/plain;base64,'. $img; $pdf = new FPDF(); $pdf->AddPage(); $pdf->Image($pic, 10,30,0,0,'png'); $pdf->Output(); 

Remember to select the correct Image() type of the Image() function.

+5
source

To make images work with FPDF and to avoid errors, here is a modified version of @ pedro-soares code:

 function getImage($dataURI){ $img = explode(',',$dataURI,2); $pic = 'data://text/plain;base64,'.$img[1]; $type = explode("/", explode(':', substr($dataURI, 0, strpos($dataURI, ';')))[1])[1]; // get the image type if ($type=="png"||$type=="jpeg"||$type=="gif") return array($pic, $type); return false; } 

Because FPDF only allows these three types of files, it is important to verify that it is valid. Use:

 $pic = getImage($Base64StringGoesHere); if ($pic!==false) $pdf->Image($pic[0], 10,30,0,0, $pic[1]); 
+3
source

To add a little more Mari M answer, I added some lines to work with any type of image. (as for me FPDF complains if the image is jpeg and I try to load tempimg.png)

 $this->tempImgLoc = 'tempimg'; // Here I just remove the file extension $dataPieces = explode(',',$dataURI); $encodedImg = $dataPieces[1]; $decodedImg = base64_decode($encodedImg); // extract the image type from data:image/png;base64 $extension = explode('/', $dataPieces[0]); $extension = explode(';', $extension[1]); $this->tempImgLoc .= '.' . $extension[0]; 

I'm sure there is a better way to do this with regex, but to be honest, regexp is not my cup of tea ...

+1
source

Well, I spent half a day testing on this issue. I have to say that this is a solution for printing images from a database row directly, without saving them in temporary files.

I am going to write a few points to consider:

  • I use the type of blobs in the database to store the human signature for printing in a PDF document.
  • I send a signature to be saved to the database using the post method from the canvas node.
  • When I print a line from the db blob, it does not contain the "+" character, which does not allow me to print the signature correctly. To solve this problem, I am doing str_replace to remove spaces and insert a plus symbol.

Then the only thing you need to do is:

 $pdf->Image(str_replace(' ','+',$dbRow['blobImage']),160,150,20,10,'png'); 

If you, like me, need to print several images from the database, you should see if they came or not, because if the image is empty, you will have an error.

I just add this to the last code:

 if( $dbRow['blobImage'] ){ $pdf->Image(str_replace(' ','+',$dbRow['blobImage']),160,150,20,10,'png'); } 

NOTE:

  • 160 β†’ X image position
  • 150 β†’ Y image position
  • 20 β†’ X Image Scale
  • 10 β†’ Y Image Scale
  • 'png' β†’ The base format of my Base64 image type.
+1
source

New solution with imagecreatefromstring () summer 2019

  • remove temporary .png if exists
  • create a temporary .png file
  • use $ pdf-> image (fpdf)

My Api returns me a Datamatrix 1234 as a byte array. The following code shows how to use this byte array as Png in Fpdf.

My answer is $ = "iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs + 9AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAABJSURBVChTjYwBCgAgCAP9 / 6dNreVUgg7Gps3EUOfD5Q4Au6YdAGcH8ygyVKrFnhmbc9FL7qRxWRxEkXWW4Q691UsGpZPzt7dEFyY47RMW + x2LAAAAAElFTkSuQmCC"

 $filename = '/lk_Reptool/printing/tmp/tmppng.png'; if (file_exists($filename)) { unlink($filename); } $response = file_get_contents('https://myapi.com/TwoDCode/GetDatamatrixAsPngByteArray?datamatrixText=' . $RepID . '&pixelSize=100'); $response = base64_decode($response); $img = imagecreatefromstring($response); imagepng($img, $filename); 

code for pdf

 $pdf = new PDF('P', 'mm', 'A4'); $pdf->AddPage(); $pdf->Image('/lk_Reptool/printing/tmp/tmppng.png', 180, 20, 10, 10); 
+1
source

Source: https://habr.com/ru/post/952648/


All Articles