设为首页 收藏本站
查看: 821|回复: 0

[经验分享] PHP利用FPDI 制作PDF 档案 (php合并pdf, php签名pdf)

[复制链接]

尚未签到

发表于 2017-3-25 09:34:13 | 显示全部楼层 |阅读模式
  昨天研究如何在既有的PDF 档案上放入中文字,虽然找到支援中文的FPDF ,但是有些Unicode 字集我实在试不出如何显示(如:堃) 。
  我的同事建议我用图形来解决看看,以下就是我的实验过程(我用的是Windows 平台) 。
  我用的相关技术如下:


名称
说明
下载网址


FPDF
PHP 上用来产生PDF 的第三方套件
[http://www.fpdf.org/](http://www.fpdf.org/)


FPDI
用来载入一个已存在的PDF 档案,以供FPDF 使用
[http://fpdi.setasign.de/](http://fpdi.setasign.de/)


香港参考宋体(DFSongSd.ttf)
包含许多Windows 内建中文字体所没有的中文字
[http://glyph.iso10646hk.net/index.jsp](http://glyph.iso10646hk.net/index.jsp)


GD 2
PHP 产生图形用的延伸套件
PHP Win32 内建


iconv
转换文字编码
PHP Win32 内建
  首先,我利用FPDI 来载入一个已存在的PDF :

<?php
error_reporting (E_ALL);
require_once ('FPDI/FPDI.php');
// 建立 FPDI 物件
$pdf = new FPDI();
// 載入現在 PDF 檔案
$page_count = $pdf->setSourceFile("test.pdf");
// 匯入現在 PDF 檔案的第一頁
$tpl = $pdf->importPage(1);
// 在新的 PDF 上新增一頁
$pdf->addPage();
// 在新增的頁面上使用匯入的第一頁
$pdf->useTemplate($tpl);
// 輸出成本地端 PDF 檔案
$pdf->output("final.pdf", "F");
// 結束 FPDI 剖析器
$pdf->closeParsers();
?>
   
  FPDI 是继承自FPDF 这个类别,所以它本身就算是一个加强型的FPDF 。 上面的程式会把现有PDF 的第一页输出成新的PDF 档案。
  接着我把表单传送过来的文字,放到一个现有的图形档上。 这里有两种实作方式:如果HTML 页面编码是Big5 ,那么我会先把表单传送过来的文字用iconv 转成UTF-8 编码;如果HTML 页面编码已经是UTF-8 ,那么就不必再用iconv转换。 我采用的是第一种方式:

<?php
$text = isset($_POST['text']) ? trim($_POST['text']) : NULL;
$is_created = FALSE;
if ($text)
{
// 產生圖片
$img = imagecreatefrompng('test.png');
// 設定黑色畫筆
$black = imagecolorallocate($img, 0, 0, 0);
// 轉換文字編碼
$utf_text = iconv('big5', 'utf-8', $text);
// 繪製文字
imagettftext($img, 30, 0, 10, 40, $black, "DFSongSd.ttf", $utf_text);
// 儲存圖片
imagepng($img, 'final.png');
imagedestroy($img);
$is_created = TRUE;
}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=big5" />
<title>測試</title>
</head>
<body>
<form name="form1" id="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="text" id="text" value="<?php echo $text; ?>" />
<input type="submit" name="Submit" value="送出" />
<a href="./">重新製作</a>
</form>
<?php if ($is_created) { ?>
<img src="final.png" alt="<?php echo $text; ?>" />
<?php } ?>
</body>
</html>
   
  如此一来在输入文字并按下送出钮后,就会在现有图片上加上指定的文字。 当然文字的位置要加以计算,我是先用最简单的方式来完成。
  最后就是要将制作好的图片加到PDF 上了, FPDF 提供了image 函式来让我们可以在PDF 上放置图形。 实作方式如下:

// 產生圖片
$img = imagecreatefrompng('test.png');
$black = imagecolorallocate($img, 0, 0, 0);
$utf_text = iconv('big5', 'utf-8', $text);
imagettftext($img, 30, 0, 10, 40, $black, "DFSongSd.ttf", $utf_text);
imagepng($img, 'final.png');
imagedestroy($img);
// 載入現在 PDF 的第一頁
$pdf = new FPDI();
$page_count = $pdf->setSourceFile("test.pdf");
$tpl = $pdf->importPage(1);
$pdf->addPage();
$pdf->useTemplate($tpl);
// 放置圖形
$pdf->image("final.png", 75, 85, 50);
// 輸出成本地端 PDF 檔
$pdf->output("final.pdf", "F");
$pdf->closeParsers();
   
  要注意的是,貼上去的圖形可以大一點,這樣縮小並貼到 PDF 時會有比較好的列印效果。而圖形的放置位置和大小,也要經過計算再貼上去。
  原文:http://www.jaceju.net/blog/archives/55/
  实例:
  1. 导入完整pdf

<?php
require_once('fpdf/fpdf.php');
require_once('fpdi/fpdi.php');
// initiate FPDI
$pdf = new FPDI();
// get the page count
$pageCount = $pdf->setSourceFile('Performance_Fact_Sheet.pdf');
// iterate through all pages
for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++)
{
// import a page
$templateId = $pdf->importPage($pageNo);
// get the size of the imported page
$size = $pdf->getTemplateSize($templateId);
// create a page (landscape or portrait depending on the imported page size)
if ($size['w'] > $size['h']) {
$pdf->AddPage('L', array($size['w'], $size['h']));
} else {
$pdf->AddPage('P', array($size['w'], $size['h']));
}
// use the imported page
$pdf->useTemplate($templateId);
$pdf->SetFont('Helvetica');
$pdf->SetXY(5, 5);
$pdf->Write(8, 'A complete document imported with FPDI');
}
// Output the new PDF
$pdf->Output();
  2. 导入pdf的单页

<?php
require_once('fpdf/fpdf.php');
require_once('fpdi/fpdi.php');
// initiate FPDI
$pdf = new FPDI();
// add a page
$pdf->AddPage();
// set the source file
$pdf->setSourceFile("Fantastic-Speaker.pdf");
// import page 1
$tplIdx = $pdf->importPage(1);
// use the imported page and place it at point 10,10 with a width of 100 mm
$pdf->useTemplate($tplIdx, 10, 10, 100);
// now write some text above the imported page
$pdf->SetFont('Helvetica');
$pdf->SetTextColor(255, 0, 0);
$pdf->SetXY(30, 30);
$pdf->Write(0, 'This is just a simple text');
$pdf->Output();
  3. 导入多pdf

<?php
require_once('fpdf/fpdf.php');
require_once('fpdi/fpdi.php');
// define some files to concatenate
$files = array(
'Boombastic-Box.pdf',
'Fantastic-Speaker.pdf',
'Noisy-Tube.pdf'
);
// initiate FPDI
$pdf = new FPDI();
// iterate through the files
foreach ($files AS $file) {
// get the page count
$pageCount = $pdf->setSourceFile($file);
// iterate through all pages
for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
// import a page
$templateId = $pdf->importPage($pageNo);
// get the size of the imported page
$size = $pdf->getTemplateSize($templateId);
// create a page (landscape or portrait depending on the imported page size)
if ($size['w'] > $size['h']) {
$pdf->AddPage('L', array($size['w'], $size['h']));
} else {
$pdf->AddPage('P', array($size['w'], $size['h']));
}
// use the imported page
$pdf->useTemplate($templateId);
$pdf->SetFont('Helvetica');
$pdf->SetXY(5, 5);
$pdf->Write(8, 'A simple concatenation demo with FPDI');
}
}
// Output the new PDF
$pdf->Output();
  4. 导入pdf文件,并且在最后一页签名

<?php
require_once('fpdf/fpdf.php');
require_once('fpdi/fpdi.php');
// initiate FPDI
$pdf = new FPDI();
// get the page count
$pageCount = $pdf->setSourceFile('Performance_Fact_Sheet.pdf');
// iterate through all pages
for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++)
{
// import a page
$templateId = $pdf->importPage($pageNo);
// get the size of the imported page
$size = $pdf->getTemplateSize($templateId);
// create a page (landscape or portrait depending on the imported page size)
if ($size['w'] > $size['h']) $pdf->AddPage('L', array($size['w'], $size['h']));
else $pdf->AddPage('P', array($size['w'], $size['h']));
// use the imported page
$pdf->useTemplate($templateId);
// sign when last page
if($pageNo==$pageCount)
{
// sign with your name
$pdf->SetFont('Arial','B','12');
$pdf->SetXY(68, 129); // you should keep testing untill you find out correct x,y values
$pdf->Write(7, 'Gideon Liang');
// sign with current date
$pdf->SetXY(40, 144); // you should keep testing untill you find out correct x,y values
$pdf->Write(7, '12/03/2014');
}
}
// Output the new PDF
$pdf->Output();
  更多实例:http://manuals.setasign.com/fpdi-manual/the-fpdi-class/examples/

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-355054-1-1.html 上篇帖子: PHP 多维数组搜索 PHP multi dimensional array search 下篇帖子: php session新手入门教程
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表