Home>Article>Backend Development> How to create pdf page in php
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
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.
/*示例代码如下*/ 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.
Set/get the position of an element on the page<?phprequire_once('fpdf/chinese.php');class PDF extends PDF_Chinese{ function Header(){ $this->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(); ?>
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.
Note<?phprequire_once('fpdf/chinese.php');$pdf = new PDF_Chinese('P','mm','A4');$pdf -> 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<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();?>
method of the FPDF class library, but it is not actually included in the class library. Using theOpen()
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!