php实现往pdf中加数字签名操作示例【附源码下载】
程序员文章站
2022-06-05 16:12:23
本文实例讲述了php实现往pdf中加数字签名操作。分享给大家供大家参考,具体如下:
//=====================================...
本文实例讲述了php实现往pdf中加数字签名操作。分享给大家供大家参考,具体如下:
//============================================================+ // 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 // info@tecnick.com //============================================================+ /** * 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 */ // set certificate file $certificate = 'file://data/cert/tcpdf.crt'; $certificate = 'file://'.realpath('./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" rel="external nofollow" >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 //============================================================+
其中tcpdf_include.php文件(源自tcpdf插件)如下:
<?php //============================================================+ // file name : tcpdf_include.php // begin : 2008-05-14 // last update : 2014-12-10 // // description : search and include the tcpdf library. // // author: nicola asuni // // (c) copyright: // nicola asuni // tecnick.com ltd // www.tecnick.com // info@tecnick.com //============================================================+ /** * search and include the tcpdf library. * @package com.tecnick.tcpdf * @abstract tcpdf - include the main class. * @author nicola asuni * @since 2013-05-14 */ // always load alternative config file for examples require_once('config/tcpdf_config_alt.php'); // include the main tcpdf library (search the library on the following directories). $tcpdf_include_dirs = array( realpath('../tcpdf.php'), '/usr/share/php/tcpdf/tcpdf.php', '/usr/share/tcpdf/tcpdf.php', '/usr/share/php-tcpdf/tcpdf.php', '/var/www/tcpdf/tcpdf.php', '/var/www/html/tcpdf/tcpdf.php', '/usr/local/apache2/htdocs/tcpdf/tcpdf.php' ); foreach ($tcpdf_include_dirs as $tcpdf_include_path) { if (@file_exists($tcpdf_include_path)) { require_once($tcpdf_include_path); break; } } //============================================================+ // end of file //============================================================+
eng.php文件如下:
<?php //============================================================+ // file name : eng.php // begin : 2004-03-03 // last update : 2010-10-26 // // description : language module for tcpdf // (contains translated texts) // english // // author: nicola asuni // // (c) copyright: // nicola asuni // tecnick.com ltd // manor coach house, church hill // aldershot, hants, gu12 4rq // uk // www.tecnick.com // info@tecnick.com //============================================================+ /** * tcpdf language file (contains translated texts). * @package com.tecnick.tcpdf * @brief tcpdf language file: english * @author nicola asuni * @since 2004-03-03 */ // english global $l; $l = array(); // page meta descriptors -------------------------------------- $l['a_meta_charset'] = 'utf-8'; $l['a_meta_dir'] = 'ltr'; $l['a_meta_language'] = 'en'; // translations -------------------------------------- $l['w_page'] = 'page'; //============================================================+ // end of file //============================================================+
补充:
tcpdf.crt文件点击此处。
tcpdf插件点击此处。
更多关于php相关内容感兴趣的读者可查看本站专题:《php文件操作总结》、《php加密方法总结》、《php编码与转码操作技巧汇总》、《php数据结构与算法教程》、《php程序设计算法总结》、《php数学运算技巧总结》及《php字符串(string)用法总结》
希望本文所述对大家php程序设计有所帮助。