Teach you how to program print out PDF

Source: Internet
Author: User
Tags php class require

The class used today is called Fpdf,fpdf this PHP class allows you to use pure PHP (rather than using pdflib) to generate PDF files. It shows up in PHP class and accelerates the process of PDF documents in the programming language. Its features include: selectable cell size, page formatting and margins, header and footer management, automatic pagination, automatic line wrapping and text alignment, and so on. It also supports a variety of image formats, such as Jpeg,png,truetype and Type1, among others. You will not be disappointed with a trial.
1, download fpdf from the Internet class, and then practical require included (also in the attachment).

Copy Code code as follows:


require ('./fpdf/fpdf.php ');//include PDF file


2, and then we define an array, and the contents of the array is today's medal situation

Copy Code code as follows:


/*


* Country, National


* nationalflag, Flag


* glod, gold number


* Silver, silver number


* Copper, Silver number


*/


$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 Code code as follows:


class PDF extends fpdf{


/*


* $count, National


* $nationalFlag, the picture address of the national flag


* $imageX, the x-coordinate of the national flag


* $imageY, the y-coordinate of the flag


* $goldTotal, total number of gold medals


* $silverTotal, total silver


* $copperTotal, total bronze


*/


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)//displays information about each country


$this->image ($nationalFlag, $imageX, $imageY);//The position of the flag


$this->setx (' 70 ');//Set the coordinates of the flag


$this->settextcolor (200,160,12);//Set the color of the gold medal


$this->cell (40,20, $goldTotal);//show Gold number


$this->setx (' 100 ');//Set the display position of the gold number


$this->settextcolor (170,162,138);//Set the color of the 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);//show the position of the silver medal


$this->ln ();//


$this->settextcolor (0,0,0);//Because the colors are set to work throughout the page, the original color is restored each time you enter a country's information


}


}


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 the entire article is displayed.
parameter 1:family, set the font you want.
Parameter 2:style, set style. (The empty string represents the plain text, the U represents the Underline, B represents the bold, and I represents the italic).
Parameter 3:size, the size of the font.

2, Cell (float w [, float h [, string txt]])
Displays a cell (rectangular range), and also provides other functional 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.
Parameter 1:w the cell width. If: 0, the cell extends to the right edge of the page.
Parameter 2: The height of the storage cell. The default value is: 0.
Parameter 3:txt, to print out the content.

3, Ln ([float h])
Complete the line break, and wrap the line. Changes to the current (x,y) coordinates, the horizontal axis (x) is returned to the left edge, and the ordinate (y) height is increased.
Parameter 1:h, the distance or height of the next line. The default value is: Height is equal to the height displayed last.

4, SetTextColor (int r [, int g, int b])
Defines the color used for text. You can use RGB color or grayscale mode to define. This feature can be set up before one page, and that color value will remain on one page to another page.

4, the class has been defined, the last call

Copy Code code as follows:


$pdf =new PDF ();//Instantiate Class


$pdf->addpage ();


$imageX =40;//Set the initial x value of the picture


$imageY =15;//Set the initial Y-value of the 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 each time the call is invoked.
2, Output () method is used to export the PDF.
Code and class file download: Click to download

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.