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 相关文章推荐
PHP简介
Oct 09 PHP
经典的PHPer为什么被认为是草根?
Apr 02 PHP
Smarty安装配置方法
Apr 10 PHP
php iconv() : Detected an illegal character in input string
Dec 05 PHP
测试php函数的方法
Nov 13 PHP
什么是OneThink oneThink后台添加插件步骤
Apr 13 PHP
CI框架文件上传类及图像处理类用法分析
May 18 PHP
PHP处理CSV表格文件的常用操作方法总结
Jul 01 PHP
thinkphp跨库操作的简单代码实例
Sep 22 PHP
PHP实现多图上传(结合uploadify插件)思路分析
Nov 30 PHP
php生成图片缩略图功能示例
Feb 22 PHP
php解决约瑟夫环算法实例分析
Sep 30 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 数组实例说明
2008/08/18 PHP
php 中htmlentities导致中文无法查询问题
2018/09/10 PHP
浅谈laravel5.5 belongsToMany自身的正确用法
2019/10/17 PHP
dojo 之基础篇(二)之从服务器读取数据
2007/03/24 Javascript
JavaScript脚本性能优化注意事项
2008/11/18 Javascript
jquery实现简单易懂的图片展示小例子
2013/11/21 Javascript
jquery制作居中遮罩层效果分享
2014/02/21 Javascript
javascript制作的cookie封装及使用指南
2015/01/02 Javascript
举例讲解AngularJS中的模块
2015/06/17 Javascript
基于jquery编写分页插件
2016/03/07 Javascript
bootstrap-datetimepicker实现只显示到日期的方法
2016/11/25 Javascript
jquery设置css样式的多种方法(总结)
2017/02/21 Javascript
简单实现jQuery手风琴效果
2017/08/18 jQuery
js replace 全局替换的操作方法
2018/06/12 Javascript
微信小程序实现获取准确的腾讯定位地址功能示例
2019/03/27 Javascript
基于layui的table插件进行复选框联动功能的实现方法
2019/09/19 Javascript
Python(Tornado)模拟登录小米抢手机
2013/11/12 Python
浅谈Python peewee 使用经验
2017/10/20 Python
Python+matplotlib实现计算两个信号的交叉谱密度实例
2018/01/08 Python
django+echart绘制曲线图的方法示例
2018/11/26 Python
python中tkinter的应用:修改字体的实例讲解
2019/07/17 Python
ansible-playbook实现自动部署KVM及安装python3的详细教程
2020/05/11 Python
基于Python+QT的gui程序开发实现
2020/07/03 Python
使用matplotlib的pyplot模块绘图的实现示例
2020/07/12 Python
Python Request类源码实现方法及原理解析
2020/08/17 Python
土木工程应届生求职信
2013/10/31 职场文书
教育实习生的自我评价分享
2013/11/21 职场文书
信息专业个人的自我评价
2013/12/27 职场文书
医药专业应届毕业生求职信范文
2014/01/01 职场文书
旷课检讨书1000字
2014/02/14 职场文书
幼儿园元旦家长感言
2014/02/27 职场文书
青年志愿者活动总结
2014/04/26 职场文书
教师节活动总结
2014/08/29 职场文书
2014年“向国旗敬礼”网上签名寄语活动方案
2014/09/27 职场文书
2014教师评职称工作总结
2014/11/10 职场文书
2015年七夕爱情寄语
2015/03/24 职场文书