實例 tmp.php
<?php
/**
fpdf是完全獨立,不依賴任何組件,只要裝了php就可以了
又是免費的,比Zend_Pdf好多了
*/
require_once 'u.php';
//require_once 'extra.php';
$pdf=new PDF();
$pdf->Open();
$pdf->Aliasnbpages();
//添加字體
$pdf->AddFont('calligra','','calligra.php');
$pdf->SetFont('calligra',null,10);
$title='Give Me A Job';
//設(shè)置標(biāo)題頭
$pdf->SetTitle($title);
//增加一個頁面
$pdf->AddPage();
$tit='My Blog';
//定義一個鏈接,也可用Write()
$url='http://hi.baidu.com/1babyo';
$pdf->Cell(10,4,$tit,'B',0,'C',0,$url);
$tit='My Email';
$url='mailto:1babyo@163.com';
$pdf->SetX(40);
$pdf->Write(6,$tit,$url);
$y=$pdf->GetY();
//添加圖片
$pdf->Image('005.jpg',0,$y+6,210);
//設(shè)置文本顏色
$pdf->SetTextColor(255,0);
//設(shè)置填充顏色
$pdf->SetFillColor(0,120,10);
$job='Who can give me a job,I am looking forwad to.';
$y=$pdf->GetY();
$w=$pdf->GetStringWidth($job);
$pdf->Rotate(30,6+$w,$y+38);
$pdf->Ellipse($w+70,$y+30,$w-30,2,'F');
$pdf->Rotate(0);
//附加水印
$pdf->RotateText(100,$y+20,$job,30);
$pdf->Ln();
$pdf->SetTextColor();
$d=$pdf->LoadData('c.txt');
$pdf->Table($d,null,2);
$pdf->BookMark('Job');
$title='popup';
//打印文本,并添加至索引
$pdf->PrintArt($title,'popup.txt');
$title='ZendFramework';
$pdf->PrintArt($title,'zf.txt');
$title='Blog Controller';
//$pdf->BookMark($title,1,0);
$pdf->PrintArt($title,'BlogController.php',true);
$title='User Controller';
$pdf->PrintArt($title,'UserController.php',true);
//輸出pdf
$pdf->Output();
?>
pdf類 u.php:
<?php
define('FPDF_FONTPATH','../font/');
require '../fpdf.php';
class PDF extends FPDF
{
//Page header
function Header()
{
global $title;
//Arial bold 15
$this->SetFont('Times','B',15);
//Calculate width of title and position
$w=$this->GetStringWidth($title)+6;
$this->SetX((210-$w)/2);
//Colors of frame, background and text
//$this->SetDrawColor(0,80,180);
$this->SetFillColor();
$this->SetTextColor(220,250,250);
//Thickness of frame (1 mm)
$this->SetLineWidth(1);
//Title
$this->Cell($w,9,$title,0,1,'C',1);
//Line break
$this->Ln(10);
}
//Page footer
function Footer()
{
//Position at 1.5 cm from bottom
$this->SetY(-15);
//Arial italic 8
$this->SetTextColor(0,255,0);
$this->SetFont('Arial','I',12);
//Page number
$this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
function AddPage()
{
parent::AddPage();
$this->SetTextColor(220,0,200);
$this->SetFontSize(13);
$this->RotateText(160,4,'Dont leave me alone, baby',-30);
$this->SetTextColor(0,0,230);
$this->SetFontSize(10);
}
function Bookmark($txt,$level=0,$y=0)
{
$outlines=array();
if($y==-1)
$y=$this->GetY();
$this->outlines[]=array('t'=>$txt,'l'=>$level,'y'=>$y,'p'=>$this->PageNo());
}
function _putbookmarks()
{
$nb=count($this->outlines);
if($nb==0)
return;
$lru=array();
$level=0;
foreach($this->outlines as $i=>$o)
{
if($o['l']>0)
{
$parent=$lru[$o['l']-1];
//Set parent and last pointers
$this->outlines[$i]['parent']=$parent;
$this->outlines[$parent]['last']=$i;
if($o['l']>$level)
{
//Level increasing: set first pointer
$this->outlines[$parent]['first']=$i;
}
}
else
$this->outlines[$i]['parent']=$nb;
if($o['l']<=$level and $i>0)
{
//Set prev and next pointers
$prev=$lru[$o['l']];
$this->outlines[$prev]['next']=$i;
$this->outlines[$i]['prev']=$prev;
}
$lru[$o['l']]=$i;
$level=$o['l'];
}
//Outline items
$n=$this->n+1;
foreach($this->outlines as $i=>$o)
{
$this->_newobj();
$this->_out('<</Title '.$this->_textstring($o['t']));
$this->_out('/Parent '.($n+$o['parent']).' 0 R');
if(isset($o['prev']))
$this->_out('/Prev '.($n+$o['prev']).' 0 R');
if(isset($o['next']))
$this->_out('/Next '.($n+$o['next']).' 0 R');
if(isset($o['first']))
$this->_out('/First '.($n+$o['first']).' 0 R');
if(isset($o['last']))
$this->_out('/Last '.($n+$o['last']).' 0 R');
$this->_out(sprintf('/Dest [%d 0 R /XYZ 0 %.2f null]',1+2*$o['p'],($this->h-$o['y'])*$this->k));
$this->_out('/Count 0>>');
$this->_out('endobj');
}
//Outline root
$this->_newobj();
$this->OutlineRoot=$this->n;
$this->_out('<</Type /Outlines /First '.$n.' 0 R');
$this->_out('/Last '.($n+$lru[0]).' 0 R>>');
$this->_out('endobj');
}
function _putresources()
{
parent::_putresources();
$this->_putbookmarks();
}
function _putcatalog()
{
parent::_putcatalog();
if(count($this->outlines)>0)
{
$this->_out('/Outlines '.$this->OutlineRoot.' 0 R');
$this->_out('/PageMode /UseOutlines');
}
}
function Body($fichier)
{
//Read text file
$f=fopen($fichier,'r');
$txt=fread($f,filesize($fichier));
fclose($f);
//Font
//Output text in a 6 cm width column
$this->SetXY(25,25);
$this->MultiCell(160,5,$txt);
$this->Ln();
$this->Ln();
//Mention
$y=$this->GetY();
$this->Text(25,$y,'(over)');
}
function PrintArt($title,$file,$parent=false)
{
//Add chapter
$this->AddPage();
if ($parent)
$this->BookMark($title,1);
else
$this->BookMark($title);
$this->Body($file);
$this->SetTitle($title);
}
function LoadData($file)
{
//Read file lines
$lines=file($file);
$data=array();
foreach($lines as $line)
$data[]=explode(',',chop($line));
return $data;
}
function Table($data,$header=null,$scaleX=1)
{
$w=30;
$h=6;
$rowN=count($data);
$colN=count($data[0]);
//offset of table width
$offset=array(0,0,-15,-15);
$tw=$scaleX*($colN*$w+array_sum($offset));
//Header
if ($header) {
foreach($header as $k=>$col)
$this->Cell($scaleX*($w+$offset[$k]),$h+1,$col,1);
}
//Data
foreach($data as $r)
{
foreach($r as $k=>$c)
$this->Cell($scaleX*($w+$offset[$k]),$h,$c,'LT');
$this->Ln();
}
$x=$this->GetX();
$y=$this->GetY();
$ax=$x+$tw;
$this->Line($x,$y,$ax,$y);
$this->Line($ax,$y-$h*$rowN,$ax,$y);
}
function Ellipse($x,$y,$rx,$ry,$style='D')
{
if($style=='F')
$op='f';
elseif($style=='FD' or $style=='DF')
$op='B';
else
$op='S';
$lx=4/3*(M_SQRT2-1)*$rx;
$ly=4/3*(M_SQRT2-1)*$ry;
$k=$this->k;
$h=$this->h;
$this->_out(sprintf('%.2f %.2f m %.2f %.2f %.2f %.2f %.2f %.2f c',
($x+$rx)*$k,($h-$y)*$k,
($x+$rx)*$k,($h-($y-$ly))*$k,
($x+$lx)*$k,($h-($y-$ry))*$k,
$x*$k,($h-($y-$ry))*$k));
$this->_out(sprintf('%.2f %.2f %.2f %.2f %.2f %.2f c',
($x-$lx)*$k,($h-($y-$ry))*$k,
($x-$rx)*$k,($h-($y-$ly))*$k,
($x-$rx)*$k,($h-$y)*$k));
$this->_out(sprintf('%.2f %.2f %.2f %.2f %.2f %.2f c',
($x-$rx)*$k,($h-($y+$ly))*$k,
($x-$lx)*$k,($h-($y+$ry))*$k,
$x*$k,($h-($y+$ry))*$k));
$this->_out(sprintf('%.2f %.2f %.2f %.2f %.2f %.2f c %s',
($x+$lx)*$k,($h-($y+$ry))*$k,
($x+$rx)*$k,($h-($y+$ly))*$k,
($x+$rx)*$k,($h-$y)*$k,
$op));
}
function Rotate($angle=0,$x=-1,$y=-1)
{
if($x==-1)
$x=$this->x;
if($y==-1)
$y=$this->y;
if($this->angle!=0)
$this->_out('Q');
$this->angle=$angle;
if($angle!=0)
{
$angle*=M_PI/180;
$c=cos($angle);
$s=sin($angle);
$cx=$x*$this->k;
$cy=($this->h-$y)*$this->k;
$this->_out(sprintf('q %.5f %.5f %.5f %.5f %.2f %.2f cm 1 0 0 1 %.2f %.2f cm',$c,$s,-$s,$c,$cx,$cy,-$cx,-$cy));
}
}
function _endpage()
{
if($this->angle!=0)
{
$this->angle=0;
$this->_out('Q');
}
parent::_endpage();
}
function RotateText($x,$y,$txt,$angle)
{
//Text rotated around its origin
$this->Rotate($angle,$x,$y);
$this->Text($x,$y,$txt);
$this->Rotate(0);
}
}
下載 fpdf-demo.tar.bz2