How to make PDF page in PHP

Source: Internet
Author: User
Tags php pdf

Fpdf is "free pdf", Fpdf Class Library provides basic PDF creation function, its source code and the use of the right to be complimentary. This article mainly to share with you in PHP PDF page production method, hope to help everyone.

Document advantages in PDF format

    1. Generic: PDF documents are available in both UNIX and Windows systems.

    2. Security: PDF documents can be set to read-only mode, and protection measures such as passwords can be added.

    3. Aesthetics: PDF documents can be largely compatible with Chinese encoding and retain the current page layout.

    4. Compact: In most cases, generating a PDF document reduces the file volume.

Fpdf class Library Download

    • Fpdf class Library Download address: http://www.fpdf.org/

    • Fpdf class Library Chinese plugin download address: http://www.fpdf.org/download/chinese.zip

Fpdf Class Library Configuration

    1. Download fpdf file.

    2. Unzip the downloaded compressed file into the project root directory.

    3. Refer to the Fpdf class library in your project (code below).

<?phpdefine (' Fpdf_fontpath ', ' font/'); require_once (' fpdf/fpdf.php ');? >

Specific operations for Fpdf class libraries

Creating objects

New FPDF ([String page-orientation [, String Measure-unit [, String Page-format]]);/*      page-orientation: Optional Parameters, Indicates that the PDF document is landscape or portrait, the default p        value: P: Portrait L: Transverse    measure-unit: Optional parameter, indicating metering unit, default mm        value: pt: Point     mm: mm       cm: cm in       : inch    page-format: Optional parameter, paper type, default A4        value: A4, A5, letter, etc. */

Add a new page

void AddPage ([string page-orientation]);/*      page-orientation: Optional parameter, indicating that the PDF document is landscape or portrait, the default p        value: P: Portrait L: Landscape */

Set font

void SetFont (String font [, string style [, float size]]);/* Font    : indicates font;    Style: Optional parameter, indicating style, default to Normal style;        Value: B: Bold     I: Italic        U: Underline size    : Optional parameter, indicates font size, default is 12pt;*/

Increase cell

void Cell (float width, float height, string txt, int border, int ln, string align, Boolean fill, String link);/*    width : Increases the cell width. Height    : Increases the cell height.    Str: The text that is placed in the cell.    border: Cell border.    ln: newline height, default is 0, which is a line break.    align: Alignment, default left, R is right, C is centered.    fill: Is the color fill, false by default. Link    : Add links, default no links.    * The Cell () function is one of the main ways to output text in Fpdf. */

Output document

String Output ([string name [, string dest]);/*    name: Optional parameter indicating the name of the file to be stored.    dest: Optional parameter, operation content.        Value:        I: The PDF document is displayed directly in the browser.        D: Download the PDF document.        F: Save as local file.        S: Returns a String value. */

Insert Picture

void Image (string file, float x, float y float width, float height);/*    file: Picture path.    x: The horizontal axis of the picture position.    y: The ordinate of the picture position. Width    : Picture widths. Height    : Picture altitude. */

Solve Chinese garbled problem

    1. Download the Chinese plugin chinese.php file in fpdf to create the PDF_Chinese() object.

    2. Set the page encoding to GB2312 or use the iconv() function to change the string encoding method.

/* The sample code is as follows */<?php    require_once (' fpdf/chinese.php ');    $pdf =new Pdf_chinese (' P ', ' mm ', ' A4 ');    $pdf-Addgbfont (' GB ', Iconv ("UTF-8", "GBK", ' Microsoft Jas Black '));      $pdf-AddPage ();      $pdf-SetFont (' GB ', ', ');      $pdf-Cell (0,0,iconv ("UTF-8", "GBK", ' Hello, world! '));    $pdf-Write (5, Iconv ("UTF-8", "GBK", ' Hello, world! '));      $pdf, Output ();  ? >

Set up headers and footers

Header() Footer() set up headers and footers by overriding methods and methods in the Fpdf class.

<?phprequire_once (' fpdf/chinese.php '); class PDF extends pdf_chinese{    function Header () {        $this SetFont (' GB ', ', ten);        $this->write (10,iconv ("UTF-8", "GBK", "This is the header! '));        $this->ln ();    }    function Footer () {        $this->sety ( -15);        $this->setfont (' GB ', ', ten);        $this->cell (0,10,iconv ("UTF-8", "GBK", ' This is the footer! '));    }} $pdf =new PDF (' P ', ' mm ', ' A4 '); $pdf, Addgbfont (' GB ', Iconv ("UTF-8", "GBK", ' Microsoft Jas Black '));  $pdf-AddPage ();  $pdf-SetFont (' GB ', ', ');  $pdf-Cell (0,0,iconv ("UTF-8", "GBK", ' Hello, world! '), $pdf Write (5, Iconv ("UTF-8", "GBK", ' Hello, world! '));  $pdf, Output ();  ? >

Set/Get the position of an element in a page

void SetX (float x);    Sets the x-coordinate of an element in the page, in MM. If x is negative, it represents the distance from the right end of the page to the left. void sety (float y [, Boolean resetx]);    Sets the y-coordinate of an element in the page, in MM. If Y is negative, it represents the distance from the bottom of the page. Resets the x-coordinate if the optional parameter resetx is true. void SetXY (float x, float y);    Sets an element's (x, y) coordinates on the page, the rule as above, and the X coordinate is not reset when Y is positioned. float GetX ();      Gets the current x-coordinate of an element. float GetY ();       Gets the current y-coordinate of an element.

Output string

void Write (float h, string txt [, mixed link]);    /* H: Defines the line height of the string.    txt: Specifies the output string. Link    : Optional parameter, set link. */

Line break

void Ln ([float h]);//h: Sets the height of the row, with the default value of the last output row.

Body output

void Multicell (float width, float height, string txt, int border, string align, Boolean fill);/* Width    : cell width. Height    : cell height.    txt: The text that is placed in the cell.    border: Cell border, default is 0.    align: Alignment. The default is left, r= right, c= Center.    fill: is the color fill. False by default.    * the Multicell () function is the main way to output large pieces of text, and can be wrapped automatically. */

Draw a table

Use Cell() the function loop to create the cells and eventually form the table.

<?phprequire_once (' fpdf/chinese.php '); $pdf = new Pdf_chinese (' P ', ' mm ', ' A4 '); $pdf, Addgbfont (); $pdf AddPage (), $pdf-SetFont (' GB ', ', '), $header = Array (' name ', ' age ', ' gender ', ' wages '); $data = Array (); $data [0] = Array (' Xiao Zhang ', ' 24 ', ' Male ', ' 5,000.00 '); $data [1] = Array (' Xiao Wang ', ' 22 ', ' Female ', ' 4,000.00 '); $width = Array (40,40,40,40); for ($i =0; $i <count ($ header), $i + +) {    $pdf-Cell ($width [$i],6,iconv ("UTF-8", "GBK", $header [$i]), 1);} $pdf, Ln (), foreach ($data as $row) {    $pdf-Cell ($width [0],6,iconv ("UTF-8", "GBK", $row [0]), 1);    $pdf-Cell ($width [1],6,iconv ("UTF-8", "GBK", $row [1]), 1);    $pdf-Cell ($width [2],6,iconv ("UTF-8", "GBK", $row [2]), 1);    $pdf-Cell ($width [3],6,iconv ("UTF-8", "GBK", $row [3]), 1);    $pdf, Ln ();} $pdf, Output ();? >

Precautions

    • Part of the material contains fpdf open () method, but is not actually included in the class library. Using open () method will cause an error.

    • When you use the Fpdf class to generate a PDF file, the encoding format should be set to GB2312 (or GB related encoding), or the Pdf_chinese class remains garbled even if it is inherited.

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.