使用PHP建立和修改PDF文檔_PHP教程

來源:互聯網
上載者:User
【php100譯稿】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. require_once('../config/lang/eng.php');
  2. require_once('../tcpdf.php');
  3. // create new PDF document
  4. $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  5. // set document information
  6. $pdf->SetCreator(PDF_CREATOR);
  7. $pdf->SetAuthor('Nicola Asuni');
  8. $pdf->SetTitle('TCPDF Example 002');
  9. $pdf->SetSubject('TCPDF Tutorial');
  10. $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
  11. // remove default header/footer
  12. $pdf->setPrintHeader(false);
  13. $pdf->setPrintFooter(false);
  14. // set default monospaced font
  15. $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  16. //set margins
  17. $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
  18. //set auto page breaks
  19. $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
  20. //set image scale factor
  21. $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  22. //set some language-dependent strings
  23. $pdf->setLanguageArray($l);
  24. // ---------------------------------------------------------
  25. // set font
  26. $pdf->SetFont('times', 'BI', 20);
  27. // add a page
  28. $pdf->AddPage();
  29. // print a line using Cell()
  30. $pdf->Cell(0, 10, 'Example 002', 1, 1, 'C');
  31. // ---------------------------------------------------------
  32. //Close and output PDF document
  33. $pdf->Output('example_002.pdf', 'I');
  34. ?>

使用PHP修改PDF文檔

下面我們討論如何使用PHP修改PDF文檔。假設我們需要將一張圖片通過PHP程式加入到PDF中,範例程式碼如下:

樣本二:使用PHP在PDF中增加一張圖片

 
  1. require_once('../config/lang/eng.php');
  2. require_once('../tcpdf.php');
  3. // create new PDF document
  4. $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  5. // set document information
  6. $pdf->SetCreator(PDF_CREATOR);
  7. $pdf->SetAuthor('Nicola Asuni');
  8. $pdf->SetTitle('TCPDF Example 009');
  9. $pdf->SetSubject('TCPDF Tutorial');
  10. $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
  11. // set default header data
  12. $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
  13. // set header and footer fonts
  14. $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
  15. $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
  16. // set default monospaced font
  17. $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  18. //set margins
  19. $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
  20. $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
  21. $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
  22. //set auto page breaks
  23. $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
  24. //set image scale factor
  25. $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  26. //set some language-dependent strings
  27. $pdf->setLanguageArray($l);
  28. // ---------------------------------------------------------
  29. // add a page
  30. $pdf->AddPage();
  31. // set JPEG quality
  32. $pdf->setJPEGQuality(75);
  33. // Image example
  34. $pdf->Image('../images/image_demo.jpg', 50, 50, 100, 150, '', 'http://www.tcpdf.org', '', true, 150);
  35. // ---------------------------------------------------------
  36. //Close and output PDF document
  37. $pdf->Output('example_009.pdf', 'I');
  38. ?>

更多關於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



http://www.bkjia.com/PHPjc/446362.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/446362.htmlTechArticle【php100譯稿】PDF全稱Portable Document Format,譯為可移植文檔格式,是Adobe公司推出的便攜文檔格式。PDF具有與作業系統無關的特性,這一效能...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.