Create and modify PDF documents using PHP _php tutorial

Source: Internet
Author: User
Tags php pdf vars
The "php100 translation" PDF full name Portable Document format, translated as portable, is an Adobe company-launched document format. The PDF has an OS-independent feature that makes it an ideal document format for electronic document distribution and digital information dissemination on the Internet. Today we'll discuss how to create PDF documents using PHP and modify PDFs using PHP.

To use PDF documents in PHP, we need to use the Tcpdf package, a class that PHP uses to read PDFs.

PHP Create PDF document

You can download the Tcpdf package from the link given below.

Tcpdf-php class for pdf:http://sourceforge.net/projects/tcpdf/files/

This is a free and easy-to-use plug-in package, and here are some examples to show how to work with the Tcpdf package.

Example one: Using 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, ten, ' Example 002 ', 1, 1, ' C ');
  31. // ---------------------------------------------------------
  32. //close and output PDF document
  33. $pdf ->output (' example_002.pdf ', ' I ');
  34. ?>

Using PHP to modify PDF documents

Here we discuss how to modify PDF documents using PHP. Suppose we need to add a picture to the PDF via a PHP program, the sample code is as follows:

Example two: Adding a picture to a 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 ', ', ', ', ', ', ', ', ' http://www.tcpdf.org', ", True, 150);
  35. // ---------------------------------------------------------
  36. //close and output PDF document
  37. $pdf ->output (' example_009.pdf ', ' I ');
  38. ?>

For more examples of tcpdf-php class for PDF, refer to:

Http://www.tecnick.com/public/code/cp_dpage.php?aiocp_dp=tcpdf_examples

You can also use Tcpdf's underlying properties for managing PDF documents. If you want to develop a PHP PDF document class yourself, refer to the PHP documentation for some of the functions described in PDF: http://www.php.net/manual/en/ref.pdf.php



http://www.bkjia.com/PHPjc/446362.html www.bkjia.com true http://www.bkjia.com/PHPjc/446362.html techarticle The "php100 translation" PDF full name Portable Document format, translated as portable, is an Adobe company-launched document format. PDF has an operating system-independent feature, this performance ...

  • 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.