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 相关文章推荐
文件上传类
Oct 09 PHP
PHP树的代码,可以嵌套任意层
Oct 09 PHP
十天学会php之第二天
Oct 09 PHP
php tp验证表单与自动填充函数代码
Feb 22 PHP
php中使用临时表查询数据的一个例子
Feb 03 PHP
详解PHP中的mb_detect_encoding函数使用方法
Aug 18 PHP
一段实用的php验证码函数
May 19 PHP
Yii2――使用数据库操作汇总(增删查改、事务)
Dec 19 PHP
PHP将数据导出Excel表中的实例(投机型)
Jul 31 PHP
php高性能日志系统 seaslog 的安装与使用方法分析
Feb 29 PHP
如何使用php生成zip压缩包
Apr 21 PHP
你真的了解PHP中的引用符号(&)吗
May 12 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常用代码大全(新手入门必备)
2010/06/29 PHP
laravel-admin 实现在指定的相册下添加照片
2019/10/21 PHP
懒就要懒到底——鼠标自动点击(含时间判断)
2007/02/20 Javascript
JSON 学习之完全手册 图文
2007/05/29 Javascript
JavaScript异步加载浅析
2014/12/28 Javascript
jQuery中DOM树操作之复制元素的方法
2015/01/23 Javascript
Jquery树插件zTree用法入门教程
2015/02/17 Javascript
全面接触神奇的Bootstrap导航条实战篇
2016/08/01 Javascript
jQuery轮播图效果精简版完整示例
2016/09/04 Javascript
全屏滚动插件fullPage.js使用实例解析
2016/10/21 Javascript
微信小程序动态的加载数据实例代码
2017/04/14 Javascript
Angular.js自动化测试之protractor详解
2017/07/07 Javascript
原生JavaScrpit中异步请求Ajax实现方法
2017/11/03 Javascript
Vue 让元素抖动/摆动起来的实现代码
2018/05/31 Javascript
js实现简单扫雷
2020/11/27 Javascript
[01:03:50]DOTA2-DPC中国联赛 正赛 CDEC vs DLG BO3 第二场 2月7日
2021/03/11 DOTA
python时间整形转标准格式的示例分享
2014/02/14 Python
请不要重复犯我在学习Python和Linux系统上的错误
2016/12/12 Python
Django基于ORM操作数据库的方法详解
2018/03/27 Python
python递归全排列实现方法
2018/08/18 Python
python3.4+pycharm 环境安装及使用方法
2019/06/13 Python
python之当你发现QTimer不能用时的解决方法
2019/06/21 Python
Python打开文件、文件读写操作、with方式、文件常用函数实例分析
2020/01/07 Python
关于tf.nn.dynamic_rnn返回值详解
2020/01/20 Python
利用Tensorflow的队列多线程读取数据方式
2020/02/05 Python
在keras下实现多个模型的融合方式
2020/05/23 Python
Python虚拟环境venv用法详解
2020/05/25 Python
Python 列表反转显示的四种方法
2020/11/16 Python
乌克兰电子和家用电器商店:Foxtrot
2019/07/23 全球购物
方法名是否可以与构造器的名字相同
2012/06/04 面试题
小学生红领巾广播稿
2014/01/21 职场文书
交通局领导班子群众路线教育实践活动对照检查材料思想汇报
2014/10/09 职场文书
2015年教师自我评价范文
2015/03/04 职场文书
2015年重阳节主持词
2015/07/04 职场文书
小学班级管理心得体会
2016/01/07 职场文书
详解Redis瘦身指南
2021/05/26 Redis