TCPDF 和 base64 图像 [英] TCPDF and base64 image

查看:117
本文介绍了TCPDF 和 base64 图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下午好,我在使用 TCPDF lib 在 PDF 文档中插入图像时遇到问题.图像以这种方式从 JSignature 中捕获:

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);

在另一个 PHP 页面中,我可以通过这种方式正确查看图像:

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 ?>" />

但是当我想打印一个 pdf 文件时,签名不起作用:

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

  1. 这样:

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

返回这个错误:TCPD ERROR: [Image] Unable to get the size of the image: @ ** * and Corrected image visualized!!

  1. 这样:

$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.

推荐答案

在我的项目中,我这样做是为了使用 JSignature + TCPDF 完美工作:

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

签名:
- HTML

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

-JS(使用按钮 ID 和输入 ID 更改 ID #btnSave)

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

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

TCPD 代码:

$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 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;
}

希望能帮到你!巧;)

这篇关于TCPDF 和 base64 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆