TCPDF and insert an image base64 encoded

后端 未结 4 1843
粉色の甜心
粉色の甜心 2021-01-05 23:38

I\'ve this string from Mysql db:

$img_base64_encoded = 

\'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+         


        
相关标签:
4条回答
  • 2021-01-06 00:15

    For those who generate an HTML variable for output, this is how I got it working: Note that I am generating a barcode using the tc-lib-barcode library found here

    $img_base64_encoded = 'data:image/png;base64,' . base64_encode($bobj->getPngData());
    $text.= '<img src="@' . preg_replace('#^data:image/[^;]+;base64,#', '', $img_base64_encoded) . '" width="200" height="30">';
    
    0 讨论(0)
  • 2021-01-06 00:17

    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);
    
    0 讨论(0)
  • 2021-01-06 00:21

    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, '');
    
    0 讨论(0)
  • 2021-01-06 00:36

    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

    0 讨论(0)
提交回复
热议问题