Home >Backend Development >PHP Tutorial >How to create pdf page in php

How to create pdf page in php

小云云
小云云Original
2018-03-29 15:06:461835browse


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. <?phpdefine(&#39;FPDF_FONTPATH&#39;,&#39;font/&#39;);require_once(&#39;fpdf/fpdf.php&#39;);?>
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等
*/

Add new page

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

Set font

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

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中输出文字的主要方式之一。
*/

Output document

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

Insert picture

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

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. /*示例代码如下*/<?php
        require_once(&#39;fpdf/chinese.php&#39;);    $pdf=new PDF_Chinese(&#39;P&#39;,&#39;mm&#39;,&#39;A4&#39;);    $pdf -> AddGBFont (&#39;GB&#39;,iconv("UTF-8","gbk",&#39;微软雅黑&#39;));  
        $pdf -> AddPage ();  
        $pdf -> SetFont (&#39;GB&#39;, &#39;&#39;, 20);  
        $pdf -> Cell(0,0,iconv("UTF-8","gbk",&#39;你好,世界!&#39;));    $pdf -> Write (5, iconv("UTF-8","gbk",&#39;你好,世界!&#39;));  
        $pdf -> Output();  
    ?>
Set the header and footer

By overriding the

Header() method and Footer()# in the FPDF class ## Method to set header and footer. <pre class="brush:php;toolbar:false;" style="font-size:14px;line-height:22px;">&lt;?phprequire_once(&amp;#39;fpdf/chinese.php&amp;#39;);class PDF extends PDF_Chinese{ function Header(){ $this-&gt;SetFont(&amp;#39;GB&amp;#39;,&amp;#39;&amp;#39;,10); $this-&gt;Write(10,iconv(&quot;UTF-8&quot;,&quot;gbk&quot;,&amp;#39;这是页眉!&amp;#39;)); $this-&gt;Ln(20); } function Footer(){ $this-&gt;SetY(-15); $this-&gt;SetFont(&amp;#39;GB&amp;#39;,&amp;#39;&amp;#39;,10); $this-&gt;Cell(0,10,iconv(&quot;UTF-8&quot;,&quot;gbk&quot;,&amp;#39;这是页脚!&amp;#39;)); } }$pdf=new PDF(&amp;#39;P&amp;#39;,&amp;#39;mm&amp;#39;,&amp;#39;A4&amp;#39;);$pdf -&gt; AddGBFont (&amp;#39;GB&amp;#39;,iconv(&quot;UTF-8&quot;,&quot;gbk&quot;,&amp;#39;微软雅黑&amp;#39;)); $pdf -&gt; AddPage (); $pdf -&gt; SetFont (&amp;#39;GB&amp;#39;, &amp;#39;&amp;#39;, 20); $pdf -&gt; Cell(0,0,iconv(&quot;UTF-8&quot;,&quot;gbk&quot;,&amp;#39;你好,世界!&amp;#39;));$pdf -&gt; Write (5, iconv(&quot;UTF-8&quot;,&quot;gbk&quot;,&amp;#39;你好,世界!&amp;#39;)); $pdf -&gt; Output(); ?&gt;</pre>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坐标。

Output string

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

Line break

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

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输出大段文字的主要方法,可自动换行。
*/

Draw a table

Use the

Cell()

function to create cells in a loop and finally form a table. <pre class="brush:php;toolbar:false;" style="font-size:14px;line-height:22px;">&lt;?phprequire_once(&amp;#39;fpdf/chinese.php&amp;#39;);$pdf = new PDF_Chinese(&amp;#39;P&amp;#39;,&amp;#39;mm&amp;#39;,&amp;#39;A4&amp;#39;);$pdf -&gt; AddGBFont();$pdf -&gt; AddPage();$pdf -&gt; SetFont(&amp;#39;GB&amp;#39;,&amp;#39;&amp;#39;,14);$header = array(&amp;#39;姓名&amp;#39;,&amp;#39;年龄&amp;#39;,&amp;#39;性别&amp;#39;,&amp;#39;工资&amp;#39;);$data = array();$data[0] = array(&amp;#39;小张&amp;#39;,&amp;#39;24&amp;#39;,&amp;#39;男&amp;#39;,&amp;#39;5,000.00&amp;#39;);$data[1] = array(&amp;#39;小王&amp;#39;,&amp;#39;22&amp;#39;,&amp;#39;女&amp;#39;,&amp;#39;4,000.00&amp;#39;);$width = array(40,40,40,40);for($i=0;$i&lt;count($header);$i++){ $pdf -&gt; Cell($width[$i],6,iconv(&quot;UTF-8&quot;,&quot;gbk&quot;,$header[$i]),1); }$pdf -&gt; Ln();foreach($data as $row){ $pdf -&gt; Cell($width[0],6,iconv(&quot;UTF-8&quot;,&quot;gbk&quot;,$row[0]),1); $pdf -&gt; Cell($width[1],6,iconv(&quot;UTF-8&quot;,&quot;gbk&quot;,$row[1]),1); $pdf -&gt; Cell($width[2],6,iconv(&quot;UTF-8&quot;,&quot;gbk&quot;,$row[2]),1); $pdf -&gt; Cell($width[3],6,iconv(&quot;UTF-8&quot;,&quot;gbk&quot;,$row[3]),1); $pdf -&gt; Ln(); }$pdf -&gt; Output();?&gt;</pre>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!

Statement:
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