print BASE64 coded image into a FPDF document

前端 未结 7 1871
我寻月下人不归
我寻月下人不归 2021-01-04 13:03

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

<         


        
7条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-04 13:39

    New solution with imagecreatefromstring() Summer 2019

    • delete temporary .png if exist
    • create temporary .png file
    • use $pdf->Image (fpdf)

    My Api returns me a Datamatrix of 1234 as Byte Array. The folowing code shows you how to use that Byte Array as Png in Fpdf.

    My $response = "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);
    

提交回复
热议问题