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程序设计有所帮助。

(0)

相关推荐

  • php FPDF类库应用实现代码

    复制代码 代码如下: <?php require('chinese.php'); class PDF extends PDF_Chinese { function Header() //设置页眉 { $this->SetFont('GB','',10); $this->Write(10,'XX公司产品名录'); $this->Ln(20); //换行 } function Footer() //设置页脚 { $this->SetY(-15); $this->SetFon

  • PHP中使用imagick实现把PDF转成图片

    PHP Manual里,对imagick的描述,真的是简洁,每个成员函数,点击打开就看到如下文本: 复制代码 代码如下: Warning This function is currently not documented; only its argument list is available. 刚才解决了PHP加载问题后,对图片的处理相当方便,网上随便找了一段: 复制代码 代码如下: <?php Header("Content-type: image/jpeg");    /*

  • PHP中使用Imagick读取pdf并生成png缩略图实例

    pdf生成png首页缩略图 (服务器需要支持Imagick)  复制代码 代码如下: /** * PDF2PNG    * @param $pdf  待处理的PDF文件 * @param $path 待保存的图片路径 * @param $page 待导出的页面 -1为全部 0为第一页 1为第二页 * @return      保存好的图片路径和文件名 */   function pdf2png($pdf,$path,$page=0)  {       if(!is_dir($path))    

  • PHP实现HTML生成PDF文件的方法

    本文实例讲述了在linux中利用HTML2FPDF与wkhtmltoimage把网页html直接生成pdf格式的文件方法,分享给大家供大家参考.具体实现方法如下: 找到一款在FPDF和HTML2FPDF源码基础上开发的一套开源程序,作者很给力.基本解决了中文(以及日语.韩语及东南亚和全球语言)乱码的问题,在Windows/Linux开发环境下测试可用,不需要安装别的组件支持,是没有VPS和独立服务器的网站开发者的福音. 不多说,源码名称是MPDF,官方地址是:http://www.mpdf1.c

  • PHP中使用TCPDF生成PDF文档实例

    实际工作中,我们要使用PHP动态的创建PDF文档,目前有许多开源的PHP创建PDF的类库,今天我给大家来介绍一款优秀的PDF库,它就是TCPDF,TCPDF是一个用于快速生成PDF文件的PHP5函数包.TCPDF基于FPDF进行扩展和改进,增强了实用功能. 特性 TCPDF具有以下特性: 1.支持页面页脚: 2.支持HTML标签代码: 3.支持jpg/png/gif/svg图形图像: 4.支持表格: 5.支持中文字符:(有些PDF类不支持中文或者处理中文相当麻烦) 6.自动分页,自动页码,等等.

  • php 生成签名及验证签名详解

    php 生成签名及验证签名 <?php /** * 根据原文生成签名内容 * * @param string $data 原文内容 * * @return string * @author confu */ function sign($data) { $filePath = 'test.p12'; if(!file_exists($filePath)) { return false; } $pkcs12 = file_get_contents($filePath); if (openssl_p

  • PHP使用MPDF类生成PDF的方法

    由于公司业务的需要,最近需要把html静态文件生成pdf,在网上找了很多类文件来实现,效果都不是很好.最先用的是tcpdf这个类特别的慢,而且当前版本有一个很让人头疼的问题-css中的背景图片无法获取到,找了很多资料都无法解决.最后发现mpdf可能实现该功能,大喜过望,而且效率也比tcpdf快. mpdf的官方下载地址:http://www.mpdf1.com/mpdf/download 下载后里面有实例,可以参照着做一个就知道了.当然官方网站也有实例,网址:http://mpdf1.com/c

  • PHP在网页中动态生成PDF文件详细教程

    本文详细介绍使用 PHP 动态构建 PDF 文件的整个过程.使用免费 PDF 库 (FPDF) 或 PDFLib-Lite 等开源工具进行实验,并使用 PHP 代码控制 PDF 内容格式. 有时您需要准确控制要打印的页面的呈现方式.在这种情况下,HTML 就不再是最佳选择了.PDF 文件使您能够完全控制页面的呈现方式,以及文本.图形和图像在页面上的呈现方式.遗憾的是,用来构建 PDF 文件的 API 不属于 PHP 工具包的标准部件.现在您需要提供一点帮助. 当您在网络上搜索,寻找对 PHP 的

  • php实现生成PDF文件的方法示例【基于FPDF类库】

    本文实例讲述了php实现生成PDF文件的方法.分享给大家供大家参考,具体如下: 首先要下载FPDF http://www.fpdf.org/ 或者点击此处本站下载. 例子:将下面的文件保存在web根目录,与附件fpdf17处于同一级 <?php ini_set('display_errors', '0'); ini_set('max_execution_time', '60'); require ('fpdf17/chinese.php'); $pdf = new PDF_Chinese();

  • PHP下SSL加密解密、验证、签名方法(很简单)

    超级简单,依赖于OpenSSL扩展,这里就不多废话了,直接奉上代码 签名: function sign($data) { //读取私钥文件 $priKey = file_get_contents('key/rsa_private_key.pem'); //转换为openssl密钥,必须是没有经过pkcs8转换的私钥 $res = openssl_get_privatekey($priKey); //调用openssl内置签名方法,生成签名$sign openssl_sign($data, $si

  • PHP实现在线阅读PDF文件的方法

    本文实例讲述了PHP实现在线阅读PDF文件的方法.分享给大家供大家参考.具体实现方法如下: <?php if(!function_exists('read_pdf')) { function read_pdf($file) { if(strtolower(substr(strrchr($file,'.'),1)) != 'pdf') { echo '文件格式不对.'; return; } if(!file_exists($file)) { echo '文件不存在'; return; } head

  • 使用PHP把HTML生成PDF文件的几个开源项目介绍

    利用PHP编码生成PDF文件是一个非常耗时的工作.在早期,开发者使用PHP并借助FPDF来生成PDF文件.但是如今,已经有很多函数库可以使用了,并且能够从你提供的HTML文件生成PDF文档.这让原先耗时的工作变得非常简单了. FPDF是很早就被使用的,其特点如下: FPDF FPDF是一个允许使用纯PHP生成PDF文档的PHP类,换句话说,没有使用PDFlib 函数库.FPDF中的F代表免费和自由:你可以在任何情况下使用,并且支持自定义,来满足你特定的需求 特点: 1.可以选择单元.页面格式和边

随机推荐