TCPDF and base64 image

夙愿已清 提交于 2021-02-11 15:24:57

问题


Good Afternoon, I have a problem with insert an image in a PDF document with TCPDF lib. The image is caught from JSignature in this manner:

$uri=base64_decode($_POST['firma']);
$filename=SIGNATURE_IMAGE_PATH.ucfirst(strtolower(($_POST['cognome']))).ucfirst(strtolower(($_POST['nome'])))."-".$birthday.".png";
$ret=file_put_contents($filename, $uri);

In another PHP page I can view image correctly in this manner:

<?php
    $string=base64_encode(file_get_contents(str_replace(" ", "", SIGNATURE_IMAGE_PATH.$member->firma)))
?>
<img src="data:image/svg+xml;base64,<?php echo $string ?>" />

But when if I want to print a pdf file the signature not work:

  1. This way:

    $string=file_get_contents(str_replace(" ", "", SIGNATURE_IMAGE_PATH.$member->firma));
    $pdf->Image('@'.$string);
    

return this error: TCPDF ERROR: [Image] Unable to get the size of the image: @ ** *and corrected image visualized!!

  1. This way:

    $string=file_get_contents(str_replace(" ", "", SIGNATURE_IMAGE_PATH.$member->firma));
    $img = '<img src=data:image/svg+xml;base64,'.$string.'>';
    $pdf->writeHTML($img, true, false, true, false, '');
    

not return errors but image do not appear. I have read other threads for this problem, but any solution proposed seems to not work for me.


回答1:


In my project i did that for use JSignature + TCPDF work perfectly:

Sign:
- HTML

<div id="signature"></div><br>
<input type="hidden" id="hiddenSigData" name="hiddenSigData" />

-JS(change ID #btnSave with you button id and input id)

$('#btnSave').click(function(){
var sigData = $('#signature').jSignature('getData','base30');
$('#hiddenSigData').val(sigData);       
 });

TCPDF CODE:

$firmacode=$_POST['hiddenSigData'];
$firmatradotta=base30_to_jpeg($firmacode,'nameofimage.png');
$firmadefinitiva='path/to/image/'.$firmatradotta;
$content='

<table border="0">
    <tr>
        <td style="border-bottom:1pt solid black;"><img src="'.$firmadefinitiva.'"></td>
    </tr>

</table>
';
$obj_pdf->writeHTMLCell(120, 20, 80, 244, $content);

Function for decode image:

function base30_to_jpeg($base30_string, $output_file) {
require_once ('./jSignature/jSignature_Tools_Base30.php');
$data = str_replace ( 'image/jsignature;base30,', '', $base30_string );
$converter = new jSignature_Tools_Base30 ();
$raw = $converter->Base64ToNative ( $data );
// Calculate dimensions
$width = 0;
$height = 0;
foreach ( $raw as $line ) {
    if (max ( $line ['x'] ) > $width)
        $width = max ( $line ['x'] );
    if (max ( $line ['y'] ) > $height)
        $height = max ( $line ['y'] );
}

// Create an image
$im = imagecreatetruecolor ( $width + 20, $height + 20 );

// Save transparency for PNG
imagesavealpha ( $im, true );
// Fill background with transparency
$trans_colour = imagecolorallocatealpha ( $im, 255, 255, 255, 127 );
imagefill ( $im, 0, 0, $trans_colour );
// Set pen thickness
imagesetthickness ( $im, 2 );
// Set pen color to black
$black = imagecolorallocate ( $im, 0, 0, 0 );
// Loop through array pairs from each signature word
for($i = 0; $i < count ( $raw ); $i ++) {
    // Loop through each pair in a word
    for($j = 0; $j < count ( $raw [$i] ['x'] ); $j ++) {
        // Make sure we are not on the last coordinate in the array
        if (! isset ( $raw [$i] ['x'] [$j] ))
            break;
        if (! isset ( $raw [$i] ['x'] [$j + 1] ))
            // Draw the dot for the coordinate
            imagesetpixel ( $im, $raw [$i] ['x'] [$j], $raw [$i] ['y'] [$j], $black );
        else
            // Draw the line for the coordinate pair
            imageline ( $im, $raw [$i] ['x'] [$j], $raw [$i] ['y'] [$j], $raw [$i] ['x'] [$j + 1], $raw [$i] ['y'] [$j + 1], $black );
    }
}

// Check if the image exists
if (! file_exists ( dirname ( $output_file ) )) {
    mkdir(dirname($output_file));
}

// Create Image
$ifp = fopen ( $output_file, "wb" );
imagepng ( $im, $output_file );
fclose ( $ifp );
imagedestroy ( $im );
copy($output_file, 'path/to/image/'.$output_file);
unlink( $output_file );
return $output_file;
}

Hope this can help you! Ciao ;)



来源:https://stackoverflow.com/questions/59502688/tcpdf-and-base64-image

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