The class used today is called Fpdf,fpdf this PHP class allows you to use pure PHP (or, more specifically, no pdflib) to generate PDF files. It shows and accelerates the process of PDF documents in the programming language with PHP class. Features include: selectable cell size, page format and margins, header and footer management, automatic pagination, automatic line wrapping and text alignment, and more. It also supports a variety of image formats, such as Jpeg,png,truetype, Type1, and more. Try it and you won't be disappointed.
1, download the Fpdf class from the Internet, and then the practical require included (also in the attachment).
Copy CodeThe code is as follows:
Require ('./fpdf/fpdf.php ');//PDF file included
2, then we define an array, the contents of the array is today's medal situation
Copy CodeThe code is as follows:
/*
* Country, National
* Nationalflag, National flag
* Glod, number of gold medals
* Silver, Silver number
* Copper, number of silver medals
*/
$platle =array (
Array (' country ' = ' PRC ', ' nationalflag ' = ' rpc.jpg ', ' Glod ' =>27, ' Silver ' =>13, ' copper ' =>15),
Array (' country ' = ' USA ', ' 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 been included in the class, and then we define a class to facilitate the definition of their own content
Copy CodeThe code is as follows:
Class PDF extends fpdf{
/*
* $count, Country
* $nationalFlag, the picture address of the national flag
* $imageX, the x-coordinate of the national flag
* $imageY, the y-coordinate of the national flag
* $goldTotal, total number of gold medals
* $silverTotal, Total silver
* $copperTotal, total number of bronze medals
*/
function Createhead ($country, $nationalFlag, $imageX, $imageY, $goldTotal, $silverTotal, $copperTotal) {
$this->setfont (' Arial ', ' B ', ' 24 ');//define a font and style
$this->cell (40,20, $country, 15);//display information for each country
$this->image ($nationalFlag, $imageX, $imageY);//location of the flag
$this->setx (' 70 ');//Set the coordinates of the national flag
$this->settextcolor (200,160,12);//Set the color of the gold medal
$this->cell (40,20, $goldTotal);//show the number of gold medals
$this->setx (' 100 ');//Set the display position of the number of gold medals
$this->settextcolor (170,162,138);//Set the color of silver
$this->cell (40,20, $silverTotal);//Show silver number
$this->setx (' 130 ');//Set the display position of the silver number
$this->settextcolor (187,120,68);//Set the color of the bronze
$this->cell (40,20, $copperTotal);//Display the position of the silver medal
$this->ln ();//Line break
$this->settextcolor (0,0,0);//Because the set of colors can be valid throughout the page, so each time you enter a country's information, the original color is restored.
}
}
the various methods used to explain:
1, SetFont (string family [, string style [, float size]])
Sets the font for the string. The font set by this method is tried in the entire PDF file before the text or entire article is displayed.
Parameter 1:family, set the desired font.
Parameter 2:style, set style. (An empty string represents plain text, U is an underscore, B is bold, and I represents Italic).
Parameter 3:size, the size of the font.
2. Cell (float w [, float h [, string txt]])
Displays a cell (rectangular range), along with other feature options, including (border, background color, string). The position of the upper-left corner of the cell is the current position.
Note: There are other parameters in the method that are not listed. Because it was not used in the experiment.
The parameter 1:w the width of the cell. If: 0, this cell will extend to the right edge of the page.
Parameter 2: Cell height. The default value is: 0.
Parameter 3:txt, the content to be printed out.
3, Ln ([float h])
Completes the line break and wraps the line. Causes the current (x, y) coordinate to change, and the horizontal axis (×) goes back to the left edge and increases the ordinate (y) height.
Parameter 1:h, the distance or height of the next line. The default value is: The height is equal to the last displayed height.
4, SetTextColor (int r [, int g, int b])
Defines the color used by the text. can be defined using RGB color or grayscale mode. This feature can be created before one page, and the color value will remain on one page to another page.
4, the class has been defined, and finally call
Copy the Code code as follows:
$pdf =new PDF ();//Instantiate class
$pdf->addpage ();
$imageX =40;//Set the initial x value of the picture
$imageY =15;//to set the initial Y-value of a picture
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. A new page is generated for each call.
2. The output () method is used to export the PDF.
Code and class file download: Click to download
http://www.bkjia.com/PHPjc/327856.html www.bkjia.com true http://www.bkjia.com/PHPjc/327856.html techarticle The class used today is called Fpdf,fpdf this PHP class allows you to use pure PHP (or, more specifically, no pdflib) to generate PDF files. It is presented in PHP class and accelerates PDF document programming ...