During the Olympics, I woke up with the idea that I would like to use PHP to print PDF files to record gold medals in various countries. Even if the Chinese team was so disappointing, I would have endured it.
The class used today is fpdf. The PHP class fpdf allows you to use pure PHP (more specifically, pdflib is not required) to generate PDF files. 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 ).
Require ('./fpdf. php'); // contains a PDF file
2. Then we define an array where the contents of the array are today's medals.
/** Country, country * nationalflag, national flag * glod, gold medal quantity * Silver, silver medal quantity * copper, silver medal count */$ platle = array (Array ('country' => 'prc', 'nationalflag' => 'rpc.jpg ', 'glod' => 27, 'Silver '=> 13, 'Copper' => 15), array ('country' => 'usa.jpg ', 'nationalflag' => 'usa.jpg ', 'glod' => 25, 'Silver '=> 16, 'Copper' => 4), array ('country' => 'uk ', '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.
Class PDF extends fpdf {/** $ count, country * $ nationalflag, image address of the National Flag * $ imagex, X coordinate of the National Flag * $ imagey, Y coordinate of the National 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 position $ this-> setx ('70'); // set the coordinates of the Flag $ this-> settextcolor (200,160, 12 ); // set the Gold Medal Color $ this-> cell (100, $ goldtotal); // display the gold medal quantity $ this-> setx ('20140901 '); // set the display position of the number of gold medals $ this-> settextcolor (170,162,138); // set the silver color $ this-> cell (, $ silvertotal ); // display the number of silver medals $ this-> setx ('20140901'); // set the display position of the number of silver medals $ this-> settextcolor (130, 68 ); // set the bronze color $ this-> cell (, $ coppertotal); // display the silver position $ this-> Ln (); // wrap $ this-> settextcolor (, 0); // because the set color can be valid throughout the page, so here, the initial color will be restored every time you enter the information of a country }}
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.
3. The class has been defined. Finally, call
$ 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 ['null'], $ 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: http://download.csdn.net/detail/iamduoluo/4497568