php实现往pdf中加数字签名操作示例【附源码下载】


Posted in PHP onAugust 07, 2018

本文实例讲述了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 相关文章推荐
Oracle 常见问题解答
Oct 09 PHP
《APMServ 5.1.2》使用图解
Oct 23 PHP
PHP调用Webservice实例代码
Jul 29 PHP
discuz免激活同步登入代码修改方法(discuz同步登录)
Dec 24 PHP
php基于session实现数据库交互的类实例
Aug 03 PHP
深入浅析php json 格式控制
Dec 24 PHP
PHP编写RESTful接口
Feb 23 PHP
PHP-FPM实现性能优化
Mar 31 PHP
PHP针对中英文混合字符串长度判断及截取方法示例
Mar 31 PHP
使用一个for循环将N*N的二维数组的所有值置1实现方法
May 29 PHP
php5对象复制、clone、浅复制与深复制实例详解
Aug 14 PHP
PHP中类与对象功能、用法实例解读
Mar 27 PHP
php使用环形链表解决约瑟夫问题完整示例
Aug 07 #PHP
postman的安装与使用方法(模拟Get和Post请求)
Aug 06 #PHP
PHP实现的解汉诺塔问题算法示例
Aug 06 #PHP
PHP实现普通hash分布式算法简单示例
Aug 06 #PHP
PHP实现的无限分类类库定义与用法示例【基于thinkPHP】
Aug 06 #PHP
PHP常用字符串函数小结(推荐)
Aug 05 #PHP
PHP使用标准库spl实现的观察者模式示例
Aug 04 #PHP
You might like
PHP采集腾讯微博的实现代码
2012/01/19 PHP
PHP 正则表达式常用函数
2014/08/17 PHP
php实现背景图上添加圆形logo图标的方法
2016/11/17 PHP
php实现与python进行socket通信的方法示例
2017/08/30 PHP
php使用curl获取header检测开启GZip压缩的方法
2018/08/15 PHP
JavaScript Prototype对象
2009/01/07 Javascript
替代window.event.srcElement效果的可兼容性的函数
2009/12/18 Javascript
jquery中eq和get的区别与使用方法
2011/04/14 Javascript
JSON格式化输出
2014/11/10 Javascript
jquery中EasyUI实现异步树
2015/03/01 Javascript
深入理解JavaScript系列(39):设计模式之适配器模式详解
2015/03/04 Javascript
使用Node.js实现HTTP 206内容分片的教程
2015/06/23 Javascript
jquery实现向下滑出的二级导航下滑菜单效果
2015/08/25 Javascript
使用Node.js处理前端代码文件的编码问题
2016/02/16 Javascript
微信小程序 wx.request(接口调用方式)详解及实例
2016/11/23 Javascript
详解打造 Vue.js 可复用组件
2017/03/24 Javascript
微信JS-SDK选取手机照片上传功能
2017/04/21 Javascript
JavaScript门道之标准库
2018/05/26 Javascript
微信小程序的开发范式BeautyWe.js入门详解
2019/07/10 Javascript
24行JavaScript代码实现Redux的方法实例
2019/11/17 Javascript
写了个监控nginx进程的Python脚本
2012/05/10 Python
在Python的web框架中中编写日志列表的教程
2015/04/30 Python
详解Python下Flask-ApScheduler快速指南
2018/11/04 Python
解决pycharm的Python console不能调试当前程序的问题
2019/01/20 Python
django-利用session机制实现唯一登录的例子
2020/03/16 Python
Python转换字典成为对象,可以用&quot;.&quot;方式访问对象属性实例
2020/05/11 Python
tensorflow dataset.shuffle、dataset.batch、dataset.repeat顺序区别详解
2020/06/03 Python
纯css3实现的竖形无限级导航
2014/12/10 HTML / CSS
德国前卫设计师时装在线商店:Luxury Loft
2019/11/04 全球购物
查询优化的一般准则有哪些
2015/03/08 面试题
学生会竞选自荐信
2013/10/12 职场文书
村抢险救灾方案
2014/05/09 职场文书
优秀教师个人总结
2015/02/11 职场文书
婚庆答谢词大全
2015/09/29 职场文书
2016大学生求职自荐信范文
2016/01/28 职场文书
vue实力踩坑之push当前页无效
2022/04/10 Vue.js