PDF全称Portable Document Format,译为可移植文档格式,是Adobe公司推出的便携文档格式。PDF具有与操作系统无关的特性,这一性能使它成为在Internet上进行电子文档发行和数字化信息传播的理想文档格式。今天我们来讨论如何使用PHP创建PDF文档,以及使用PHP修改PDF。
 
要想在PHP中使用PDF文档,我们需要用到TCPDF包,一个PHP用来读取PDF的类。
 
PHP创建PDF文档
 
你可以从下面给出的链接下载TCPDF包。
 
TCPDF - PHP class for PDF:http://sourceforge.net/projects/tcpdf/files/
 
这是一个免费且易用的插件包,下面我们给出一些示例来演示如何使用TCPDF包。
示例一:使用PHP生成一个简单的PDF文档
 
  
  1. <?php  
  2.  
  3. require_once('../config/lang/eng.php');  
  4. require_once('../tcpdf.php');  
  5.  
  6. // create new PDF document  
  7. $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);   
  8.  
  9. // set document information  
  10. $pdf->SetCreator(PDF_CREATOR);  
  11. $pdf->SetAuthor('Nicola Asuni');  
  12. $pdf->SetTitle('TCPDF Example 002');  
  13. $pdf->SetSubject('TCPDF Tutorial');  
  14. $pdf->SetKeywords('TCPDF, PDF, example, test, guide');  
  15.  
  16. // remove default header/footer  
  17. $pdf->setPrintHeader(false);  
  18. $pdf->setPrintFooter(false);  
  19.  
  20. // set default monospaced font  
  21. $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);  
  22.  
  23. //set margins  
  24. $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);  
  25.  
  26. //set auto page breaks  
  27. $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);  
  28.  
  29. //set p_w_picpath scale factor  
  30. $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);   
  31.  
  32. //set some language-dependent strings  
  33. $pdf->setLanguageArray($l);   
  34.  
  35. // ---------------------------------------------------------  
  36.  
  37. // set font  
  38. $pdf->SetFont('times''BI', 20);  
  39.  
  40. // add a page  
  41. $pdf->AddPage();  
  42.  
  43. // print a line using Cell()  
  44. $pdf->Cell(0, 10, 'Example 002', 1, 1, 'C');  
  45.  
  46. // ---------------------------------------------------------  
  47.  
  48. //Close and output PDF document  
  49. $pdf->Output('example_002.pdf''I');  
  50. ?> 
使用PHP修改PDF文档
 
下面我们讨论如何使用PHP修改PDF文档。假设我们需要将一张图片通过PHP程序加入到PDF中,示例代码如下:
 
示例二:使用PHP在PDF中增加一张图片
 
  
  1. <?php  
  2. require_once('../config/lang/eng.php');  
  3. require_once('../tcpdf.php');  
  4.  
  5. // create new PDF document  
  6. $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);   
  7.  
  8. // set document information  
  9. $pdf->SetCreator(PDF_CREATOR);  
  10. $pdf->SetAuthor('Nicola Asuni');  
  11. $pdf->SetTitle('TCPDF Example 009');  
  12. $pdf->SetSubject('TCPDF Tutorial');  
  13. $pdf->SetKeywords('TCPDF, PDF, example, test, guide');  
  14.  
  15. // set default header data  
  16. $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);  
  17.  
  18. // set header and footer fonts  
  19. $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));  
  20. $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));  
  21.  
  22. // set default monospaced font  
  23. $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);  
  24.  
  25. //set margins  
  26. $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);  
  27. $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);  
  28. $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);  
  29.  
  30. //set auto page breaks  
  31. $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);  
  32.  
  33. //set p_w_picpath scale factor  
  34. $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);   
  35.  
  36. //set some language-dependent strings  
  37. $pdf->setLanguageArray($l);   
  38.  
  39. // ---------------------------------------------------------  
  40.  
  41. // add a page  
  42. $pdf->AddPage();  
  43.  
  44. // set JPEG quality  
  45. $pdf->setJPEGQuality(75);  
  46.  
  47. // Image example  
  48. $pdf->Image('../p_w_picpaths/p_w_picpath_demo.jpg', 50, 50, 100, 150, '''http://www.tcpdf.org''', true, 150);  
  49.  
  50. // ---------------------------------------------------------  
  51.  
  52. //Close and output PDF document  
  53. $pdf->Output('example_009.pdf''I');  
  54. ?> 
更多关于TCPDF - PHP class for PDF的示例可以参考:
http://www.tecnick.com/public/code/cp_dpage.php?aiocp_dp=tcpdf_examples
同时,你也可以使用TCPDF的基础属性进行PDF文档的管理。如果你想自己开发一个PHP的PDF文档类,可以参考PHP文档中关于PDF的一些函数介绍:http://www.php.net/manual/en/ref.pdf.php