How to create pdf page in php

小云云
Release: 2023-03-22 16:42:01
Original
1643 people have browsed it


FPDF stands for "Free PDF". The FPDF class library provides basic PDF creation functions, and its source code and usage rights are free. This article mainly shares with you how to create pdf pages in php, I hope it can help you.

Advantages of PDF format documents

  1. General: PDF documents can be used normally on UNIX and Windows systems.

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

  3. Beautiful: PDF documents are largely compatible with Chinese encoding and retain the current page layout.

  4. Exquisite: In most cases, generating PDF documents will reduce the file size.

FPDF class library download

  • FPDF class library download address: http://www.fpdf.org/

  • FPDF class library Chinese plug-in download address: http://www.fpdf.org/download/chinese.zip

##FPDF class library configuration

  1. Download FPDF file.

  2. Extract the downloaded compressed file to the project root directory.

  3. Reference the FPDF class library in the project (code below).

  4. Copy after login
Specific operations of FPDF class library

Create object

new FPDF([string page-orientation [, string measure-unit [, string page-format]]]);

/*  
    page-orientation:可选参数,表示PDF文档为横向或纵向,默认 P
        取值:P:纵向 L:横向
    measure-unit:可选参数,表示计量单元,默认 mm
        取值:pt:点     mm:毫米       cm:厘米       in:英寸
    page-format:可选参数,纸张类型,默认 A4
        取值: A4、A5、Letter等
*/
Copy after login

Add new page

void AddPage([string page-orientation]);/*  
    page-orientation:可选参数,表示PDF文档为横向或纵向,默认 P
        取值:P:纵向 L:横向
*/
Copy after login

Set font

void SetFont(string font [, string style [, float size]]);/*
    font:表示字体;
    style:可选参数,表示样式,默认为普通样式;
        取值:B:粗体     I:斜体        U:下划线
    size:可选参数,表示字体大小,默认为12pt;
*/
Copy after login

Add cells

void Cell(float width, float height, string txt, int border, int ln, string align, boolean fill, string link);/*
    width:增加单元格宽度。
    height:增加单元格高度。
    str:放置在单元格中的文本。
    border:单元格边框。
    ln:换行高度,默认为0,即换一行。
    align:对齐方式,默认居左,R时居右,C时居中。
    fill:是否颜色填充,默认false。
    link:添加链接,默认无链接.

    * Cell()函数是FPDF中输出文字的主要方式之一。
*/
Copy after login

Output document

String Output([string name [, string dest]]);
/*
    name:可选参数,表示要储存的文件名。
    dest:可选参数,操作内容。
        取值:        I:将PDF文档直接在浏览器中显示。        D:下载PDF文档。
        F:保存为本地文件。
        S:返回一个字符串值。
*/
Copy after login

Insert picture

void Image(string file, float x, float y float width, float height);/*
    file:图片路径。
    x:图片位置的横坐标。
    y:图片位置的纵坐标。
    width:图片宽度。
    height:图片高度。
*/
Copy after login

Solve the Chinese garbled problem

  1. Downloading FPDF The Chinese plug-in chinese.php file creates a

    PDF_Chinese() object.

  2. Set the page encoding to GB2312 or use the

    iconv() function to change the string encoding.

  3. /*示例代码如下*/ AddGBFont ('GB',iconv("UTF-8","gbk",'微软雅黑'));  
        $pdf -> AddPage ();  
        $pdf -> SetFont ('GB', '', 20);  
        $pdf -> Cell(0,0,iconv("UTF-8","gbk",'你好,世界!'));    $pdf -> Write (5, iconv("UTF-8","gbk",'你好,世界!'));  
        $pdf -> Output();  
    ?>
    Copy after login
Set the header and footer

By overriding the

Header() method and Footer()# in the FPDF class ## Method to set header and footer.

SetFont('GB','',10);        $this->Write(10,iconv("UTF-8","gbk",'这是页眉!'));        $this->Ln(20);
    }    function Footer(){
        $this->SetY(-15);        $this->SetFont('GB','',10);        $this->Cell(0,10,iconv("UTF-8","gbk",'这是页脚!'));
    }
}$pdf=new PDF('P','mm','A4');$pdf -> AddGBFont ('GB',iconv("UTF-8","gbk",'微软雅黑'));  
$pdf -> AddPage ();  
$pdf -> SetFont ('GB', '', 20);  
$pdf -> Cell(0,0,iconv("UTF-8","gbk",'你好,世界!'));$pdf -> Write (5, iconv("UTF-8","gbk",'你好,世界!'));  
$pdf -> Output();  
?>
Copy after login
Set/get the position of an element on the page

void setX(float x);    
//设置某元素在页面的X坐标,单位为mm。如x为负数,则表示自页面右端向左的距离。void setY(float y [, boolean resetX]);    
//设置某元素在页面的Y坐标,单位为mm。如y为负数,则表示自页面底部向上的距离。若可选参数resetX为真则重置X坐标。void setXY(float x, float y);  
  //设置某元素在页面的(X,Y)坐标,规则如上,定位Y时不重置X坐标。float getX();   
   //获得某元素当前X坐标。float getY();    
   //获得某元素当前Y坐标。
Copy after login

Output string

void Write(float h, string txt [, mixed link]);/*
    h:定义字符串的行高。
    txt:指定输出字符串。
    link:可选参数,设置链接。
*/
Copy after login

Line break

void Ln([float h]);//h:设置行高,默认值为最后输出的行的高度。
Copy after login

Text output

void MultiCell(float width, float height, string txt, int border, string align, boolean fill);/*
    width:单元格宽度。
    height:单元格高度。
    txt:放置在单元格中的文本。
    border:单元格边框,默认为0。
    align:对齐方式。默认居左,R=居右,C=居中。
    fill:是否颜色填充。默认false。

    * MultiCell()函数是FPDF输出大段文字的主要方法,可自动换行。
*/
Copy after login

Draw a table

Use the

Cell()

function to create cells in a loop and finally form a table.

 AddGBFont();$pdf -> AddPage();$pdf -> SetFont('GB','',14);$header = array('姓名','年龄','性别','工资');$data = array();$data[0] = array('小张','24','男','5,000.00');$data[1] = array('小王','22','女','4,000.00');$width = array(40,40,40,40);for($i=0;$i 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();?>
Copy after login
Note

    Some information contains the
  • Open()

    method of the FPDF class library, but it is not actually included in the class library. Using the Open() method will cause an error.

  • When using the FPDF class to generate PDF files, the encoding format should be set to GB2312 (or GB related encoding), otherwise even if the PDF_Chinese class is inherited, it will still be garbled.

The above is the detailed content of How to create pdf page in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!