在TCPDF中使用转换后的证书签名

时间:2019-03-25 09:17:33

标签: php openssl tcpdf

我正在使用TCPDF,目的是使用setSignature签署pdf。生成输出时,得到以下响应:


%PDF-1.7 %���� 8 0 obj << /Type /Page /Parent 1 0 R /LastModified (D:20190325084627+00'00') /Resources 2 0 R /MediaBox [0.000000 0.000000 595.276000 841.890000] /CropBox [0.000000 0.000000 595.276000 841.890000] /BleedBox [0.000000 0.000000 595.276000 841.890000] /TrimBox [0.000000 0.000000 595.276000 841.890000] /ArtBox [0.000000 0.000000 595.276000 841.890000] /Contents 9 0 R /Rotate 0 /Group << /Type /Group /S /Transparency /CS /DeviceRGB >> /Annots [ 7 0 R 5 0 R ] /PZ 1 >> endobj 9 0 obj <> stream x��XMs�6��W�c2���!�Ro�#w���U{Ir�HH����#����2���}�(�/�P3I�(�۷�xR͋7z��.q��w�P���W?�iz�P�F���zK����������4\�����"Z����ȓ(�e�����{����ϛ�/go>��r�]����~��)]vB�E�d�#-&4y�ė�'~�_��}���x6l�M_֪�1$�y�.f��I�'�E����mU���o��+�[ïKc����֨O��h9O���ET�4c�i�gB����đ�9C����'i�,��QgG��Uw�F~1V6�;ٴ�4?���|d1�`���V�؛����E�G[$}��oj�[Y�65`�t>��$�ԘR�JT��+'#���@߲�JU��%Yt�7.d�i�S�o��j42�����V��lv�TFK�մ�~���DQ�'=�+s`���SCV�Z=��3ވ;��k�i��>[E�`t��%h�>@��B���s�嘦�V4��(\�JGF[),�j��bz"w �AI�ɲ��}��=�n d#�:�f�+��i�6#��C����M��5A�D��4M�8wy#+5�����H��k��E5��g!���gԊ���q�Bt���&����">-i�C���+�ϻӱ<2֐=ֲc�;%�F+�r?���Q��8ի�%e W����<燮����R5�;�r(B�[Y��d�����9����\�@�|�@͢ME$�2�N� �|���C���E�8�AAl;������sA;΂��w9 �92N��T��B�B�aA0��<���ɂՋ�o��A�@c>Q�(;i�죦�A�U~b�W �k�*� �ʞSh7�;�i�W���l�:����Pk":�T�-�SݥfP��<�> endobj 3 0 obj <> endobj 4 0 obj <> endobj 2 0 obj << /ProcSet [/PDF /Text /ImageB /ImageC /ImageI] /Font << /F1 3 0 R /F2 4 0 R >> /XObject << >> >> endobj 7 0 obj <> /H /I>> endobj 5 0 obj << /Type /Annot /Subtype /Widget /Rect [510.236220 629.291575 552.755906 671.811260] /P 8 0 R /F 4 /FT /Sig /T (��Signature) /Ff 0 /V 6 0 R >> endobj 6 0 obj << /Type /Sig /Filter /Adobe.PPKLite /SubFilter /adbe.pkcs7.detached /ByteRange[0 2666 14410 5443] /Contents<000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000> /Reference [ << /Type /SigRef /TransformMethod /DocMDP /TransformParams << /Type /TransformParams /P 1 /V /1.2 >> >> ] /M (D:20190325084627+00'00') >> endobj 10 0 obj << /Producer (��TCPDF 6.2.26 \(http://www.tcpdf.org\)) /CreationDate `

我已经考虑到TCPDF提供的示例来构建代码:https://tcpdf.org/examples/example_052/除此之外,我还对TCPDF中的数字签名进行了一些研究:https://andreas.heigl.org/2014/04/13/create-signed-pdf-files/ ...前面的回复,我对此表示红色:https://groups.google.com/forum/#!msg/symfony2/CsPpq0tnsmk/efaf-eIELYEJ

最后,这是我编写的代码,是从Ajax调用的。我将网站托管在Google Cloud(应用引擎PHP灵活环境)中:

require __DIR__ . '/../../vendor/autoload.php';

$filename = $_FILES['files']['tmp_name'][0];

$password = $_POST["pass"];
$results = array();

if(openssl_pkcs12_read(file_get_contents($filename), $results, $password)){

    $fileKey = fopen('/tmp/'.$ide_same.'.pfx', 'w+');
                                        $file = 
    fopen('/tmp/'.$ide_same.'.crt', 'w+');

    $result1=null;
    $result2=null;
    $result3=null;

    fwrite($fileKey,$results['pkey']);

    fclose($fileKey);

    //


    openssl_pkey_export($results['cert'],$result1);

    $extracerts=$results['extracerts'];


    openssl_pkey_export($extracerts[0],$result2);

    openssl_pkey_export($extracerts[1],$result3);

    fwrite($file,$result1);
    fwrite($file,$result2);
    fwrite($file,$result3);

    fclose($file);

    // create new PDF document

    $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

    $pdf->SetPrintHeader(false);
    $pdf->SetPrintFooter(false);

    $pdf->SetMargins(30, 25, 30, true);  //$left,$top,$right

    $pdf->AddPage();                     // pretty self-explanatory

    // set core font
    $pdf->SetFont('helvetica', '', 10);

    $pdf->Write(1, 'blablabla', '', 0, 'L', true, 0, false, false, 0);
    $pdf->Ln();

    $pdf->Write(1, 'blablabla,', '', 0, 'L', true, 0, false, false, 0);
    $pdf->Ln();


    $privateKey='/tmp/'.$ide_same.'.pfx';

    $certificate='/tmp/'.$ide_same.'.crt';

    $pdf->Write(1, $_POST["nome"].' '.$_POST["apel"].', '.$_POST["psto"], '', 0, 'C', true, 0, false, false, 0);

    //

    $pdf->setSignature($certificate, $privateKey, $password, '', 1, array());

    // create content for signature (image and/or text)
    $pdf->Image('/../img/Seal.png', 180, 60, 15, 15, 'PNG');

    // define active area for signature appearance
    $pdf->setSignatureAppearance(180, 60, 15, 15);

    header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
    header("Cache-Control: no-store, no-cache, must-revalidate");
    header("Cache-Control: post-check=0, pre-check=0", false);
    header("Pragma: no-cache");  
    header("Content-type: application/pdf");
    header("Content-disposition: inline; filename=file.pdf");

    $pdf->Output('/tmp/Carta_'.$ide_same.'.pdf','D');    // send the file inline to the browser (default)

}else{

    echo json_encode(openssl_error_string());

}

我期望的预期结果是PDF,签名正确,可通过Adobe Reader验证。

非常感谢您的帮助!

0 个答案:

没有答案