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

后端 未结 2 790
无人及你
无人及你 2020-12-19 23:42

I\'m storing signatures (using signaturepad in my database Coldfusion/MySQL 5.0.88 and would like to output the signature I\'m taking onto a pdf which I\'m gene

相关标签:
2条回答
  • 2020-12-20 00:25

    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.

    0 讨论(0)
  • 2020-12-20 00:38

    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);
    }
    
    0 讨论(0)
提交回复
热议问题