Use PHP to create and modify PDF documents

Source: Internet
Author: User
Tags vars

[Php100] the full name of PDF is Portable Document Format. It is a Portable Document Format released by Adobe. PDF has a feature unrelated to the operating system, which makes it an ideal document format for electronic document distribution and digital information dissemination on the Internet. Today we will discuss how to use PHP to create PDF documents and use PHP to modify PDF files.

To use a PDF file in PHP, we need to use a TCPDF package. a php class is used to read PDF files.

Create a PDF file in PHP

You can download the TCPDF package from the link below.

TCPDF-PHP class for PDF: http://sourceforge.net/projects/tcpdf/files/

This is a free and easy-to-use plug-in package. The following example shows how to use the TCPDF package.

Example 1: Use PHP to generate a simple PDF document

 
 
  1.  
  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 002');  
  12. $pdf->SetSubject('TCPDF Tutorial');  
  13. $pdf->SetKeywords('TCPDF, PDF, example, test, guide');  
  14.  
  15. // remove default header/footer  
  16. $pdf->setPrintHeader(false);  
  17. $pdf->setPrintFooter(false);  
  18.  
  19. // set default monospaced font  
  20. $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);  
  21.  
  22. //set margins  
  23. $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);  
  24.  
  25. //set auto page breaks  
  26. $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);  
  27.  
  28. //set image scale factor  
  29. $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);   
  30.  
  31. //set some language-dependent strings  
  32. $pdf->setLanguageArray($l);   
  33.  
  34. // ---------------------------------------------------------  
  35.  
  36. // set font  
  37. $pdf->SetFont('times', 'BI', 20);  
  38.  
  39. // add a page  
  40. $pdf->AddPage();  
  41.  
  42. // print a line using Cell()  
  43. $pdf->Cell(0, 10, 'Example 002', 1, 1, 'C');  
  44.  
  45. // ---------------------------------------------------------  
  46.  
  47. //Close and output PDF document  
  48. $pdf->Output('example_002.pdf', 'I');  
  49. ?> 

Use PHP to modify PDF documents

Next we will discuss how to use PHP to modify the PDF document. Suppose we need to add an image to the PDF using the PHP program. The sample code is as follows:

Example 2: add an image in PDF using PHP

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.