您的位置:首页 > 编程语言 > PHP开发

php 存PDF文件及其在线预览功能

2015-11-25 10:57 756 查看
正值奥运时期,一觉醒来有种想用PHP打印PDF来记录各国金牌的想法,即使中国队那么不争气我也忍了。

今天使用的类叫FPDF,FPDF这个PHP Class允许你采用纯PHP(更确切地说就是不需要使用PDFlib)来生成PDF文件。它以PHP Class展现并且加速PDF文档在编程语言中的进程。它所具有的特点包括:可选择的单元大小,页面格式和页边距;页眉和页脚管理;自动分页;自动换行与文本自动对齐等等。 同时它还支持多种图片格式,如JPEG,PNG,TrueType和Type1等等。试用一下你一定不会失望。

1、从网上下载fpdf的类,然后实用require包含进来(附件里也有)。

[php] view
plaincopyprint?

require('./fpdf/fpdf.php');//包含pdf文件

2、然后我们定义一个数组,数组的内容是今天的奖牌情况

[php] view
plaincopyprint?

/*

* country,国家

* nationalFlag,国旗

* glod,金牌数量

* silver,银牌数量

* copper,银牌数量

*/

$platle=array(

array('country'=>'PRC','nationalFlag'=>'rpc.jpg','glod'=>27,'silver'=>13,'copper'=>15),

array('country'=>'USA','nationalFlag'=>'usa.jpg','glod'=>25,'silver'=>16,'copper'=>4),

array('country'=>'UK','nationalFlag'=>'uk.jpg','glod'=>14,'silver'=>7,'copper'=>8)

);

3、既然第一步已经包含进类来了,然后咱再自己定义一个类以方便定义自己的内容

[php] view
plaincopyprint?

class PDF extends FPDF{

/*

* $count,国家

* $nationalFlag,国旗的图片地址

* $imageX,国旗的x坐标

* $imageY,国旗的Y坐标

* $goldTotal,金牌总数

* $silverTotal,银牌总数

* $copperTotal,铜牌总数

*/

function createHead($country,$nationalFlag,$imageX,$imageY,$goldTotal,$silverTotal,$copperTotal){

$this->setFont('Arial','B','24');//定义一个字体及样式

$this->cell(40,20,$country,15);//显示各个国家的信息

$this->image($nationalFlag,$imageX,$imageY);//国旗的位置

$this->setX('70');//设置国旗的坐标

$this->setTextColor(200,160,12);//设置金牌的颜色

$this->cell(40,20,$goldTotal);//显示金牌数量

$this->setX('100');//设置金牌数量的显示位置

$this->setTextColor(170,162,138);//设置银牌的颜色

$this->cell(40,20,$silverTotal);//显示银牌数量

$this->setX('130');//设置银牌数量的显示位置

$this->setTextColor(187,120,68);//设置铜牌的颜色

$this->cell(40,20,$copperTotal);//显示银牌的位置

$this->ln();//进行换行

$this->setTextColor(0,0,0);//因为设置的颜色能在整个页面都有效,所以这里每次输入一个国家的信息就恢复初始的颜色

}

}

使用到的各种方法解释:

1、SetFont(string family [, string style [, float size]])

设定字符串的字型。在文字或整篇文章显示之前,该方法设置的字体在整个PDF文件中都试用。

参数1:family,设定需要的字体。

参数2:style,设定样式。(空字符串代表普通文字,U代表下划线,B代表粗体,I代表斜体)。

参数3:size,字体的大小。

2、Cell(float w [, float h [, string txt]])

显示一个储存格 (长方形范围),同时,也提供其它功能选项,包括(边框、背景颜色、字符串)。储存格左上角的位置为目前位置。

注:该方法中还有其他的参数,没有列出来。因为在该实验中没有用到。

参数1:w储存格宽度。 若为:0,这个储存格会延伸至页的右边边缘。

参数2:储存格高度。默认值为:0.

参数3:txt,要打印出来的内容。

3、Ln([float h])

完成行中断,并且换行。会使到目前的(x,y)坐标改变,横坐标(x)回到去左边边缘,并且将纵坐标(y)高度增加。

参数1:h,下一行的距离或高度。默认值为:高度相等于最后显示的高度。

4、SetTextColor(int r [, int g, int b])

定义文字使用的颜色。可以使用RGB色彩或灰阶模式来定义。这个功能可以在一页之前建立,而且那个色彩值会保留在一页到另一页。

3、类已经定义完了,最后调用一下

[php] view
plaincopyprint?

$pdf=new PDF();//实例化类

$pdf->AddPage();

$imageX=40;//设置图片的初始X值

$imageY=15;//设置图片的初始Y值

foreach($platle as $key=>$value){

$pdf->createHead($value['country'],$value['nationalFlag'],$imageX,$imageY,$value['glod'],$value['silver'],$value['copper']);

$imageY+=20;

}

$pdf->output();//输出PDF

注:

1、AddPage()方法是用来生成一个新的页面的。每调用一次都生成一个新的页面。

2、outPut()方法是用来输出PDF的。 有参数保存;无参可预览,手动保存

2、

在PHP中,生成PDF时,可以用pdflib,但这个东西是要钱的,开源的话,

可以使用fphp,这个东西的下载在:
http://www.fpdf.org/
并且有中文文档下载哦

1 初步使用

<?php

require('fpdf.php');

$pdf=new FPDF();

$pdf->AddPage();

$pdf->SetFont('Arial','B',16);

$pdf->Cell(40,10,'Hello World!');

$pdf->Output();

?>

很明显是设置了字体,之后设置了cell,

cell的用法:

显示一个储存格 (长方形范围),同时,也提供其它功能选项,包括(边框、背景颜色、字符串)。储存格左上角的位置为目前位置。文字可以除意排列或置中。则行这个命令之后,目前位置便会向右移或移到下一行。它可以会在文字上建立一个连结。若果已经启动自动分页功能,当内容超出了储存格的限制,输出数据之前会自动执行分页功能。

參數

w

储存格宽度。 若为:0,这个储存格会延伸至页的右边边缘。

h

储存格高度。默认值为:0.

txt

字符串显示。默认值为:空白

border

若果要围绕储存格边缘显示边框,可用下面的数值:

0: 没有边框

1: 边框

或包含以下一些或所有字符串(在任何指示下):

L: 左边边线

T: 顶部边线

R: 右边边线

B: 底部边线

默认值为:0

ln

则行这个功能之后,目前位置应在那里。

可用下面的数值:

0: 往右边移

1: 到下一行的开端

2: 往下面

默认值为:0

align

允许排列文字置中。可用下面的数值:

L 或空格符:左边排列 (默认值)

C: 中间排列

R: 右边排列

fill

2 将数组元素显示

<!--p

require 'fpdf.php';

$books = array (

'The Sun Also Rises, by Ernest Hemingway',

'King Rat, by James Clavell',

'The Long Tail, by Chris Anderson'

);

$pdf=new FPDF('P', 'pt', 'A4');

$pd-->AddPage();

$pdf->SetFont('Times', 'B', 16);

$pdf->Cell(0,10,'My favorite books!', 0, 2, 'C');

$pdf->SetFont('Times', '', 12);

foreach ($books AS $book) {

$pdf->MultiCell(0, 20, $book, 0, 'L');

}

$pdf->Output();

?>

3 增加图片

<!--p

require 'fpdf.php';

$pdf=new FPDF('P', 'pt', 'A4');

$pd-->AddPage();

$pdf->SetFont('Times', 'B', 16);

$pdf->Cell(0,10,'Easy PayPal with PHP', 0, 2, 'C');

$pdf->Image('easypaypalwithphp.jpg');

$pdf->Output();

?>

4 加水印

<!--p

require 'fpdf.php';

class WJGPDF extends FPDF

{

function Footer()

{

$thi-->SetY(-25);

$this->SetFont('Times', 'B', 12);

$this->Cell(0,20,'Licensed to jason@example.com', 0, 0, 'C');

}

}

$pdf=new WJGPDF('P', 'pt', 'A4');

$pdf->AddPage();

$pdf->SetFont('Times', 'B', 16);

$pdf->Cell(0,10,'Easy PayPal with PHP', 0, 2, 'C');

$pdf->Output();

以上是在脚部加了一个水印email了,注意要继承FPDF类,重写其中的footer方法

4 生成复杂table

<?php

require('fpdf.php');

class PDF extends FPDF

{

//Load data

function LoadData($file)

{

//Read file lines

$lines=file($file);

$data=array();

foreach($lines as $line)

$data[]=explode(';',chop($line));

return $data;

}

//Simple table

function BasicTable($header,$data)

{

//Header

foreach($header as $col)

$this->Cell(40,7,$col,1);

$this->Ln();

//Data

foreach($data as $row)

{

foreach($row as $col)

$this->Cell(40,6,$col,1);

$this->Ln();

}

}

//Better table

function ImprovedTable($header,$data)

{

//Column widths

$w=array(40,35,40,45);

//Header

for($i=0;$i<count($header);$i++)

$this->Cell($w[$i],7,$header[$i],1,0,'C');

$this->Ln();

//Data

foreach($data as $row)

{

$this->Cell($w[0],6,$row[0],'LR');

$this->Cell($w[1],6,$row[1],'LR');

$this->Cell($w[2],6,number_format($row[2]),'LR',0,'R');

$this->Cell($w[3],6,number_format($row[3]),'LR',0,'R');

$this->Ln();

}

//Closure line

$this->Cell(array_sum($w),0,'','T');

}

//Colored table

function FancyTable($header,$data)

{

//Colors, line width and bold font

$this->SetFillColor(255,0,0);

$this->SetTextColor(255);

$this->SetDrawColor(128,0,0);

$this->SetLineWidth(.3);

$this->SetFont('','B');

//Header

$w=array(40,35,40,45);

for($i=0;$i<count($header);$i++)

$this->Cell($w[$i],7,$header[$i],1,0,'C',true);

$this->Ln();

//Color and font restoration

$this->SetFillColor(224,235,255);

$this->SetTextColor(0);

$this->SetFont('');

//Data

$fill=false;

foreach($data as $row)

{

$this->Cell($w[0],6,$row[0],'LR',0,'L',$fill);

$this->Cell($w[1],6,$row[1],'LR',0,'L',$fill);

$this->Cell($w[2],6,number_format($row[2]),'LR',0,'R',$fill);

$this->Cell($w[3],6,number_format($row[3]),'LR',0,'R',$fill);

$this->Ln();

$fill=!$fill;

}

$this->Cell(array_sum($w),0,'','T');

}

}

$pdf=new PDF();

//Column titles

$header=array('Country','Capital','Area (sq km)','Pop. (thousands)');

//Data loading

$data=$pdf->LoadData('countries.txt');

$pdf->SetFont('Arial','',14);

$pdf->AddPage();

$pdf->BasicTable($header,$data);

$pdf->AddPage();

$pdf->ImprovedTable($header,$data);

$pdf->AddPage();

$pdf->FancyTable($header,$data);

$pdf->Output();

?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: