您的位置:首页 > 其它

使用fpdf生成pdf

2013-07-01 16:30 253 查看
在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();
?>

其他的详细见文档,中文的哦
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: