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 curl 登录163邮箱并抓取邮箱好友列表的代码(经测试)
Apr 07 PHP
PHP循环输出指定目录下的所有文件和文件夹路径例子(简单实用)
May 10 PHP
简单分析ucenter 会员同步登录通信原理
Aug 25 PHP
CI框架学习笔记(一) - 环境安装、基本术语和框架流程
Oct 26 PHP
php生成静态html页面的方法(2种方法)
Sep 14 PHP
php+html5实现无刷新图片上传教程
Jan 22 PHP
PHP 无限级分类
May 04 PHP
Laravel框架基于ajax和layer.js实现无刷新删除功能示例
Jan 17 PHP
PHP基于mcript扩展实现对称加密功能示例
Feb 21 PHP
PHP实现二维数组按照指定的字段进行排序算法示例
Apr 23 PHP
PhpSpreadsheet设置单元格常用操作汇总
Nov 13 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 7的一些引人注目的新特性简单介绍
2015/11/08 PHP
简单谈谈 php 文件锁
2017/02/19 PHP
PHP观察者模式原理与简单实现方法示例
2017/08/25 PHP
PHP自动生成缩略图函数的源码示例
2019/03/18 PHP
php+jQuery ajax实现的实时刷新显示数据功能示例
2019/09/12 PHP
js判断浏览器的比较全的代码
2007/02/13 Javascript
实用的JS正则表达式(手机号码/IP正则/邮编正则/电话等)
2013/01/11 Javascript
JavaScript将数据转换成整数的方法
2014/01/04 Javascript
火狐下input焦点无法重复获取问题的解决方法
2014/06/16 Javascript
利用jquery操作Radio方法小结
2014/10/20 Javascript
JavaScript实现的内存数据库LokiJS介绍和入门实例
2014/11/17 Javascript
jQuery遮罩层实现方法实例详解(附遮罩层插件)
2015/12/08 Javascript
jQuery ajax时间差导致的变量赋值问题分析
2016/01/22 Javascript
JS基于clipBoard.js插件实现剪切、复制、粘贴
2016/05/03 Javascript
JS iFrame加载慢怎么解决
2016/05/13 Javascript
讲解vue-router之什么是编程式路由
2018/05/28 Javascript
JS实现给数组对象排序的方法分析
2019/06/24 Javascript
javascript 原型与原型链的理解及实例分析
2019/11/23 Javascript
[56:17]NB vs Infamous 2019国际邀请赛淘汰赛 败者组 BO3 第三场 8.22
2019/09/05 DOTA
Python实现的下载8000首儿歌的代码分享
2014/11/21 Python
举例讲解Python设计模式编程中对抽象工厂模式的运用
2016/03/02 Python
利用TensorFlow训练简单的二分类神经网络模型的方法
2018/03/05 Python
详解python运行三种方式
2019/05/13 Python
使用python求解二次规划的问题
2020/02/29 Python
如何使用pandas读取txt文件中指定的列(有无标题)
2020/03/05 Python
Python3.7将普通图片(png)转换为SVG图片格式(网站logo图标)动起来
2020/04/21 Python
python 6种方法实现单例模式
2020/12/15 Python
HTML5调用手机发短信和打电话功能
2020/04/29 HTML / CSS
美国轮胎网站:Priority Tire
2018/11/28 全球购物
Luxplus荷兰:以会员价购买美容产品等,独家优惠
2019/08/30 全球购物
会计工作心得体会
2014/01/13 职场文书
元旦晚会邀请函
2014/02/01 职场文书
公司开业庆典策划方案
2014/06/04 职场文书
经理任命书模板
2014/06/06 职场文书
百家讲坛观后感
2015/06/12 职场文书
2016年政治理论学习心得体会
2016/01/25 职场文书