How to display a JSON/base64 encoded image in FPDF?

大憨熊 提交于 2019-11-29 11:57:47

The issue is here

$pdf->imagepng($img);
                 ^-------------- This should be an image path (String)

Solution

$file = 'signature.png' ;
imagepng($img, $file);  
                 ^----------- Save Image to File Instead 

Then

if ($imgProceed == "true") {
    $pdf->imagepng($file);
} else {
    $pdf->Cell(50, 4, '', 0, 1);
}

Originally, I was going to convert to PNG and the more I thought about it, it was more overhead then necessary. After all, Signature Pad give us everything we need to re-draw the signature in FPDF.

I did the following:

  // decode the signature into an array
    $sig = json_decode($signatureInJSON);

    // for each line segment
    foreach ($sig as $line) {
        $lx = $line->lx;
        $ly = $line->ly;
        $mx = $line->mx;
        $my = $line->my;
        // draw the line
        $pdf->Line($lx, $ly, $mx, $my);
    }

Obviously, I also added some functions to scale the signature and offset it to where I wanted in the PDF.

Hope that helps someone else.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!