How to print a PDF file (simple application of fpdf) _ PHP Tutorial

Source: Internet
Author: User
Tags pdflib
Teach you how to print a PDF (simple application of fpdf ). The class used today is FPDF. the PHPClass of FPDF allows you to use pure PHP (more specifically, PDFlib is not required) to generate PDF files. It is displayed in PHPClass and accelerates the use of PDF documentation in programming today's Class FPDF. the fpdf php Class allows you to use pure PHP (more specifically, PDFlib is not required) to generate a PDF file. It is presented in PHP Class and accelerates the process of PDF documents in programming languages. It has the following features: selectable unit size, page format and margins, header and footer management, automatic paging, automatic line feed and automatic text alignment. It also supports multiple image formats, such as JPEG, PNG, TrueType, and type1. You will not be disappointed to try it out.
1. download the fpdf class from the Internet, and then include the practical require (which is also included in the attachment ).

The code is as follows:


Require ('./fpdf. php'); // contains a PDF file


2. then we define an array where the contents of the array are today's medals.

The code is as follows:


/*
* Country, country
* NationalFlag, national flag
* Glod, gold medal count
* Silver, silver quantity
* Copper, silver medal count
*/
$ Platle = array (
Array ('country' => 'prc', 'nationalflag' => 'rpc.jpg ', 'glod' => 27, 'Silver' => 13, 'Copper' => 15 ),
Array ('country' => 'USA', 'null' => 'usa.jpg ', 'glod' => 25, 'Silver' => 16, 'Copper' => 4 ),
Array ('country' => 'u', 'nationalflag' => 'uk.jpg ', 'glod' => 14, 'Silver' => 7, 'Copper '=> 8)
);


3. since the first step has already been included in the class, you can define another class to define your own content.

The code is as follows:


Class PDF extends FPDF {
/*
* $ Count, country
* $ NationalFlag, the image address of the flag
* $ ImageX: x coordinate of the flag
* $ ImageY, Y coordinate of the flag
* $ GoldTotal, total gold medals
* $ SilverTotal, total silver medals
* $ CopperTotal, total bronze medals
*/
Function createHead ($ country, $ nationalFlag, $ imageX, $ imageY, $ goldTotal, $ silverTotal, $ copperTotal ){
$ This-> setFont ('arial', 'B', '24'); // defines a font and style.
$ This-> cell (, $ country, 15); // Display information of each country
$ This-> image ($ nationalFlag, $ imageX, $ imageY); // flag location
$ This-> setX ('70'); // sets the coordinates of the flag.
$ This-> setTextColor (200,160, 12); // you can specify the gold medal color.
$ This-> cell (, $ goldTotal); // displays the number of gold medals
$ This-> setX ('000000'); // you can specify the number of gold medals to display.
$ This-> setTextColor (170,162,138); // Set the silver color
$ This-> cell (, $ silverTotal); // displays the number of silver medals
$ This-> setX ('20140901'); // you can specify the silver medal quantity.
$ This-> setTextColor (187,120, 68); // Set the bronze color
$ This-> cell (, $ copperTotal); // display the silver position
$ This-> ln (); // line feed
$ This-> setTextColor (, 0); // because the set color can be valid throughout the page, the initial color is restored every time you enter a country information.
}
}


Explanation of the methods used:
1. SetFont (string family [, string style [, float size])
Set the font of the string. The font set by this method is used throughout the PDF file before text or full text is displayed.
Parameter 1: family. set the font.
Parameter 2: style. set the style. (An empty string represents normal text, U represents underline, B represents bold, and I represents italic ).
Parameter 3: size, font size.

2. Cell (float w [, float h [, string txt])
Displays a cell (rectangular range) and provides other options, including border, background color, and string ). The upper left corner of the bucket is the current location.
Note: There are other parameters in this method, which are not listed. Because it is not used in this experiment.
Parameter 1: w storage space width. If it is: 0, the bucket will extend to the right edge of the page.
Parameter 2: storage height. The default value is 0.
Parameter 3: txt, the content to be printed.

3. Ln ([float h])
The line is interrupted and line feed is completed. This will change the current (x, y) coordinates, return the x coordinate to the left edge, and increase the height of the y coordinate.
Parameter 1: h, the distance or height of the next row. The default value is: The height is equal to the last displayed height.

4. SetTextColor (int r [, int g, int B])
Define the color of the text. It can be defined in RGB color or grayscale mode. This function can be created before one page, and the color value is retained on one page to another.

4. the class has been defined. finally, call

The code is as follows:


$ Pdf = new PDF (); // instantiation class
$ Pdf-> AddPage ();
$ ImageX = 40; // set the initial X value of the image.
$ ImageY = 15; // set the initial Y value of the image.
Foreach ($ platle as $ key => $ value ){
$ Pdf-> createHead ($ value ['country'], $ value ['nationalflag'], $ imageX, $ imageY, $ value ['glod'], $ value ['Silver '], $ value ['Copper']);
$ ImageY + = 20;
}
$ Pdf-> output (); // output PDF


Note:
1. the AddPage () method is used to generate a new page. Each call generates a new page.
2. the outPut () method is used to outPut PDF files.
Download Code and class files: Click to download

Using Class allows you to generate PDF files using pure PHP (more specifically, PDFlib is not required. It is presented as a PHP Class and accelerates the development of PDF documents in programming...

Related Article

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.