昨天研究如何在既有的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/
相关推荐
FPDI 合并 多个pdf文件 FPDI 合并 多个pdf文件 FPDI 合并 多个pdf文件 FPDI 合并 多个pdf文件 FPDI 合并 多个pdf文件 FPDI 合并 多个pdf文件 FPDI 合并 多个pdf文件
1.PHP使用插件fpdf fpdi 可以加数字字母水印和图片水印(中文乱码)
FPDI-免费的PDF文档导入器 :red_exclamation_mark: 本文档引用了FPDI2。不建议使用版本1,并停止开发。 :red_exclamation_mark: FPDI是PHP类的集合,它使开发人员可以从现有的PDF文档中读取页面并将它们用作模板,...
pfdi php操作PDF利器,至利于操作PDF文件的创建与修改
使用FPDF生成PDF文件,里面讲得很详细,哈哈,入门容易
3.插件fpdf fpdi 可以加数字字母转图片后加水印(中文乱码)
2.PHP使用插件fpdf fpdi chinese.php 可以加文字水印和图片水印
fpdi读取部分版本PDF失败
php可以利用这个库生成pdf,而不需要依赖其他扩展库,移植性很高,而且很方便
Laravel PDF合并安装通过作曲家$ composer require webklex/laravel-pdfmerger设置将服务提供者添加到config/app.php的providers数组中。 'providers' => [ . . . Webklex \ PDFMerger \ Providers \ ...
FPDI 是继承自FPDF 这个类别,所以它本身就算是一个加强型的FPDF 。
fpdf是利用php直接生成pdf文件,该类提供了多个功能接口,运行程序员自己定义pdf的样式,台湾朋友在其中加入了中文化补丁支撑繁体和简体。
$pdf = new FPDI_Protection(); // set the format of the destinaton file, in our case 6×9 inch //$pdf->FPDF('P', 'in', array('6','9')); $pdf->FPDF('P', 'in'); //Calculate the number of page
使用fpdf+fpdi+chinese.php实现中文改写pdf不乱码 demo.php为案例
PHP类FPDF库,生成PDF文件生成PDF文件生成PDF文件
FPDF的中文支持chinese.php
因项目中需要使用到pdf模板打印, 直接使用fpdi进行绝对定位有点繁琐, 还有很多繁琐的设置, 所以我根据项目需求在原有功能基础上造了一个pdf相对定位的文字填充和打印功能
php添加水印php添加水印php添加水印,用着可以,特此分享一下。
由于 http://www.fpdf.org 无法访问,所在把1.6版本的fpdf放在这里.
FPDI 扩展FPDI 和 FPDF 库的扩展,提供方便和快速模板化的方法。安装您可以使用安装 FPDI Extended。 将以下内容添加到composer.json文件的require键中: "m1ke/fpdi-ext": "dev-master"作者由和撰写。例子 $users=...