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
-
- require_once ('.. /config/lang/eng.php ');
- require_once ('.. /tcpdf.php ');
- //Create new PDF document
- $pdf = New TCPDF (pdf_page_orientation, Pdf_unit, Pdf_page_format, True, ' UTF-8 ', false);
- //Set Document information
- $pdf ->setcreator (Pdf_creator);
- $pdf ->setauthor (' Nicola asuni ');
- $pdf ->settitle (' TCPDF Example 002 ');
- $pdf ->setsubject (' TCPDF Tutorial ');
- $pdf ->setkeywords (' TCPDF, PDF, example, Test, guide ');
- //Remove default Header/footer
- $pdf ->setprintheader (FALSE);
- $pdf ->setprintfooter (FALSE);
- //Set default monospaced font
- $pdf ->setdefaultmonospacedfont (pdf_font_monospaced);
- //set margins
- $pdf ->setmargins (Pdf_margin_left, Pdf_margin_top, pdf_margin_right);
- //set Auto Page breaks
- $pdf ->setautopagebreak (TRUE, Pdf_margin_bottom);
- //set Image Scale factor
- $pdf ->setimagescale (Pdf_image_scale_ratio);
- //set some language-dependent strings
- $pdf ->setlanguagearray ($l);
- // ---------------------------------------------------------
- //Set Font
- $pdf ->setfont (' Times ', ' BI ', 20);
- //Add a page
- $pdf ->addpage ();
- //print a line using Cell ()
- $pdf ->cell (0, ten, ' Example 002 ', 1, 1, ' C ');
- // ---------------------------------------------------------
- //close and output PDF document
- $pdf ->output (' example_002.pdf ', ' I ');
- ?>
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
-
- require_once ('.. /config/lang/eng.php ');
- require_once ('.. /tcpdf.php ');
- //Create new PDF document
- $pdf = New TCPDF (pdf_page_orientation, Pdf_unit, Pdf_page_format, True, ' UTF-8 ', false);
- //Set Document information
- $pdf ->setcreator (Pdf_creator);
- $pdf ->setauthor (' Nicola asuni ');
- $pdf ->settitle (' TCPDF Example 009 ');
- $pdf ->setsubject (' TCPDF Tutorial ');
- $pdf ->setkeywords (' TCPDF, PDF, example, Test, guide ');
- //Set default header data
- $pdf ->setheaderdata (Pdf_header_logo, Pdf_header_logo_width, Pdf_header_title, pdf_header_string);
- //Set header and footer fonts
- $pdf ->setheaderfont (Array (Pdf_font_name_main, ", Pdf_font_size_main));
- $pdf ->setfooterfont (Array (pdf_font_name_data, ", Pdf_font_size_data));
- //Set default monospaced font
- $pdf ->setdefaultmonospacedfont (pdf_font_monospaced);
- //set margins
- $pdf ->setmargins (Pdf_margin_left, Pdf_margin_top, pdf_margin_right);
- $pdf ->setheadermargin (Pdf_margin_header);
- $pdf ->setfootermargin (Pdf_margin_footer);
- //set Auto Page breaks
- $pdf ->setautopagebreak (TRUE, Pdf_margin_bottom);
- //set Image Scale factor
- $pdf ->setimagescale (Pdf_image_scale_ratio);
- //set some language-dependent strings
- $pdf ->setlanguagearray ($l);
- // ---------------------------------------------------------
- //Add a page
- $pdf ->addpage ();
- //Set JPEG quality
- $pdf ->setjpegquality (75);
- //Image Example
- $pdf ->image ('.. /images/image_demo.jpg ', ', ', ', ', ', ', ', ' http://www.tcpdf.org', ", True, 150);
- // ---------------------------------------------------------
- //close and output PDF document
- $pdf ->output (' example_009.pdf ', ' I ');
- ?>
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 ...