Use PHP to create and modify PDF document _ PHP Tutorial

Source: Internet
Author: User
Use PHP to create and modify PDF documents. [Php100] the full name of PDF is PortableDocumentFormat. it is a portable document format released by Adobe. PDF has a feature unrelated to the operating system, this performance [php100 translated] PDF full name Portable Document Format, translated into Portable Document Format, 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. 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. ?>

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. // 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. ?>

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.