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.
General: PDF documents can be used normally on UNIX and Windows systems.
Security: PDF documents can be set to read-only mode, and passwords and other protection measures can be added.
Beautiful: PDF documents are largely compatible with Chinese encoding and retain the current page layout.
Exquisite: In most cases, generating PDF documents will reduce the file size.
FPDF class library download address: http://www.fpdf.org/
FPDF class library Chinese plug-in download address: http://www.fpdf.org/download/chinese.zip
<?phpdefine('FPDF_FONTPATH','font/');require_once('fpdf/fpdf.php');?>
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等 */
void AddPage([string page-orientation]);/* page-orientation:可选参数,表示PDF文档为横向或纵向,默认 P 取值:P:纵向 L:横向 */
void SetFont(string font [, string style [, float size]]);/* font:表示字体; style:可选参数,表示样式,默认为普通样式; 取值:B:粗体 I:斜体 U:下划线 size:可选参数,表示字体大小,默认为12pt; */
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中输出文字的主要方式之一。 */
String Output([string name [, string dest]]); /* name:可选参数,表示要储存的文件名。 dest:可选参数,操作内容。 取值: I:将PDF文档直接在浏览器中显示。 D:下载PDF文档。 F:保存为本地文件。 S:返回一个字符串值。 */
void Image(string file, float x, float y float width, float height);/* file:图片路径。 x:图片位置的横坐标。 y:图片位置的纵坐标。 width:图片宽度。 height:图片高度。 */
PDF_Chinese() object.
iconv() function to change the string encoding.
/*示例代码如下*/<?php require_once('fpdf/chinese.php'); $pdf=new PDF_Chinese('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(); ?>
Header() method and
Footer()# in the FPDF class ## Method to set header and footer. <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;" style="font-size:14px;line-height:22px;"><?phprequire_once(&#39;fpdf/chinese.php&#39;);class PDF extends PDF_Chinese{
function Header(){
$this->SetFont(&#39;GB&#39;,&#39;&#39;,10); $this->Write(10,iconv("UTF-8","gbk",&#39;这是页眉!&#39;)); $this->Ln(20);
} function Footer(){
$this->SetY(-15); $this->SetFont(&#39;GB&#39;,&#39;&#39;,10); $this->Cell(0,10,iconv("UTF-8","gbk",&#39;这是页脚!&#39;));
}
}$pdf=new PDF(&#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();
?></pre><div class="contentsignin">Copy after login</div></div>
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坐标。
void Write(float h, string txt [, mixed link]);/* h:定义字符串的行高。 txt:指定输出字符串。 link:可选参数,设置链接。 */
void Ln([float h]);//h:设置行高,默认值为最后输出的行的高度。
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输出大段文字的主要方法,可自动换行。 */
function to create cells in a loop and finally form a table. <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;" style="font-size:14px;line-height:22px;"><?phprequire_once(&#39;fpdf/chinese.php&#39;);$pdf = new PDF_Chinese(&#39;P&#39;,&#39;mm&#39;,&#39;A4&#39;);$pdf -> AddGBFont();$pdf -> AddPage();$pdf -> SetFont(&#39;GB&#39;,&#39;&#39;,14);$header = array(&#39;姓名&#39;,&#39;年龄&#39;,&#39;性别&#39;,&#39;工资&#39;);$data = array();$data[0] = array(&#39;小张&#39;,&#39;24&#39;,&#39;男&#39;,&#39;5,000.00&#39;);$data[1] = array(&#39;小王&#39;,&#39;22&#39;,&#39;女&#39;,&#39;4,000.00&#39;);$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();?></pre><div class="contentsignin">Copy after login</div></div>Note<h2 style="padding:0px;background-color:rgb(255,255,255);"></h2>
</ol>
<ul style="list-style-type: none;" class=" list-paddingleft-2">Some information contains the <li>Open()<p> method of the FPDF class library, but it is not actually included in the class library. Using the <code style="font-size:14px;line-height:22px;padding:4px 2px 0px;">Open()
method will cause an error.
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!