1. 程式人生 > 其它 >PHP tcpdf實現pdf電子簽名

PHP tcpdf實現pdf電子簽名

2021年6月18日10:50:25

官方文件https://tcpdf.org/examples/example_052/

幾個問題需要注意的問題

/*
NOTES:
 - To create self-signed signature: openssl req -x509 -nodes -days 365000 -newkey rsa:1024 -keyout tcpdf.crt -out tcpdf.crt
 - To export crt to p12: openssl pkcs12 -export -in tcpdf.crt -out tcpdf.p12
 - To convert pfx certificate to pem: openssl pkcs12 -in tcpdf.pfx -out tcpdf.crt -nodes
*/

1,這裡使用帶證書就是自己使用openssl自我簽名的,你也可以去https://letsencrypt.org/生成一個做測試

2,如果你要上正式環境請申請ca證書,如果是內部使用https://letsencrypt.org/也足夠了

3,如何檢視pdf檔案的簽名,你使用瀏覽器開啟是看不到的,你可以使用office wps等,開啟簽名就可以看到簽名了

4,現在電子合同流行,基本板塊就是 個人簽名+合同電子簽名+個人身份認證,其實如果自己做平臺的話,除了需要對對接一些個人身份認證之外的,其他都是OK的,其實技術難度不大

5,tcpdf目前支援的php是從5.3開始,7.x,8.x是不是支援還不知道,但是fpdf是支援7.x和8.x,也可以看下

<?php
//============================================================+
// File name   : example_052.php
// Begin       : 2009-05-07
// Last Update : 2013-05-14
//
// Description : Example 052 for TCPDF class
//               Certification Signature (experimental)
//
// Author: Nicola Asuni
//
// (c) Copyright:
//               Nicola Asuni
//               Tecnick.com LTD
//               www.tecnick.com
//               [email protected]
//============================================================+
/** * Creates an example PDF TEST document using TCPDF * @package com.tecnick.tcpdf * @abstract TCPDF - Example: Certification Signature (experimental) * @author Nicola Asuni * @since 2009-05-07 */ // Include the main TCPDF library (search for installation path). require_once('tcpdf_include.php'); // create new PDF document $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); // set document information $pdf->SetCreator(PDF_CREATOR); $pdf->SetAuthor('Nicola Asuni'); $pdf->SetTitle('TCPDF Example 052'); $pdf->SetSubject('TCPDF Tutorial'); $pdf->SetKeywords('TCPDF, PDF, example, test, guide'); // set default header data $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 052', PDF_HEADER_STRING); // set header and footer fonts $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN)); $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA)); // set default monospaced font $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED); // set margins $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT); $pdf->SetHeaderMargin(PDF_MARGIN_HEADER); $pdf->SetFooterMargin(PDF_MARGIN_FOOTER); // set auto page breaks $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM); // set image scale factor $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); // set some language-dependent strings (optional) if (@file_exists(dirname(__FILE__).'/lang/eng.php')) { require_once(dirname(__FILE__).'/lang/eng.php'); $pdf->setLanguageArray($l); } // --------------------------------------------------------- /* NOTES: - To create self-signed signature: openssl req -x509 -nodes -days 365000 -newkey rsa:1024 -keyout tcpdf.crt -out tcpdf.crt - To export crt to p12: openssl pkcs12 -export -in tcpdf.crt -out tcpdf.p12 - To convert pfx certificate to pem: openssl pkcs12 -in tcpdf.pfx -out tcpdf.crt -nodes */ // 設定簽名苗藥證書 $certificate = 'file://data/cert/tcpdf.crt'; // set additional information $info = array( 'Name' => 'TCPDF', 'Location' => 'Office', 'Reason' => 'Testing TCPDF', 'ContactInfo' => 'http://www.tcpdf.org', ); // set document signature $pdf->setSignature($certificate, $certificate, 'tcpdfdemo', '', 2, $info); // set font $pdf->SetFont('helvetica', '', 12); // add a page $pdf->AddPage(); // print a line of text $text = 'This is a <b color="#FF0000">digitally signed document</b> using the default (example) <b>tcpdf.crt</b> certificate.<br />To validate this signature you have to load the <b color="#006600">tcpdf.fdf</b> on the Arobat Reader to add the certificate to <i>List of Trusted Identities</i>.<br /><br />For more information check the source code of this example and the source code documentation for the <i>setSignature()</i> method.<br /><br /><a href="http://www.tcpdf.org">www.tcpdf.org</a>'; $pdf->writeHTML($text, true, 0, true, 0); // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // *** set signature appearance *** // create content for signature (image and/or text) $pdf->Image('images/tcpdf_signature.png', 180, 60, 15, 15, 'PNG'); // define active area for signature appearance $pdf->setSignatureAppearance(180, 60, 15, 15); // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // *** set an empty signature appearance *** $pdf->addEmptySignatureAppearance(180, 80, 15, 15); // --------------------------------------------------------- //Close and output PDF document $pdf->Output('example_052.pdf', 'D'); //============================================================+ // END OF FILE //============================================================+