[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
-
-
- 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, 10, 'Example 002', 1, 1, 'C');
-
- // ---------------------------------------------------------
-
- //Close and output PDF document
- $pdf->Output('example_002.pdf', 'I');
- ?>
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
-
- 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', 50, 50, 100, 150, '', 'http://www.tcpdf.org', '', true, 150);
-
- // ---------------------------------------------------------
-
- //Close and output PDF document
- $pdf->Output('example_009.pdf', 'I');
- ?>