7 Practical
7 Practical
Practical No. 7: a)Write a PHP Program to create a PDF document by using graphics
concept
b) Write a simple PHP Program to create image using php
I. Practical Significance
FPDF is a PHP class which allows to generate PDF files with pure PHP, that is to say without using
the PDFlib library. F from FPDF stands for Free: you may use it for any kind of usage and modify it
to suit your needs.
FPDF has other advantages: high level functions. Here is a list of its main features:
FPDF requires no extension (except Zlib to enable compression and GD for GIF support). The latest
version requires at least PHP 5.1.
// Page footer
function Footer()
{
1
Web Based Application Development Using PHP(22619)
// Line break
$this->Ln(20);
}
// Page footer
function Footer()
{
// Position at 1.5 cm from bottom
$this->SetY(-15);
// Arial italic 8
$this->SetFont('Arial','I',8);
// Page number
$this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
var $widths;
var $aligns;
function SetWidths($w)
{
//Set the array of column widths
$this->widths=$w;
}
function SetAligns($a)
{
//Set the array of column alignments
$this->aligns=$a;
}
function Row($data)
2
Web Based Application Development Using PHP(22619)
{
//Calculate the height of the row
$nb=0;
for($i=0;$i<count($data);$i++)
$nb=max($nb,$this->NbLines($this->widths[$i],$data[$i]));
$h=5*$nb;
//Issue a page break first if needed
$this->CheckPageBreak($h);
//Draw the cells of the row
for($i=0;$i<count($data);$i++)
{
$w=$this->widths[$i];
$a=isset($this->aligns[$i]) ? $this->aligns[$i] : 'L';
//Save the current position
$x=$this->GetX();
$y=$this->GetY();
//Draw the border
$this->Rect($x,$y,$w,$h);
//Print the text
$this->MultiCell($w,5,$data[$i],0,$a);
//Put the position to the right of the cell
$this->SetXY($x+$w,$y);
}
//Go to the next line
$this->Ln($h);
}
function CheckPageBreak($h)
{
//If the height h would cause an overflow, add a new page immediately
if($this->GetY()+$h>$this->PageBreakTrigger)
$this->AddPage($this->CurOrientation);
}
function NbLines($w,$txt)
{
//Computes the number of lines a MultiCell of width w will take
$cw=&$this->CurrentFont['cw'];
if($w==0)
$w=$this->w-$this->rMargin-$this->x;
$wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
$s=str_replace("\r",'',$txt);
$nb=strlen($s);
if($nb>0 and $s[$nb-1]=="\n")
$nb--;
$sep=-1;
$i=0;
$j=0;
$l=0;
$nl=1;
while($i<$nb)
{
$c=$s[$i];
if($c=="\n")
{
$i++;
$sep=-1;
3
Web Based Application Development Using PHP(22619)
$j=$i;
$l=0;
$nl++;
continue;
}
if($c==' ')
$sep=$i;
$l+=$cw[$c];
if($l>$wmax)
{
if($sep==-1)
{
if($i==$j)
$i++;
}
else
$i=$sep+1;
$sep=-1;
$j=$i;
$l=0;
$nl++;
}
else
$i++;
}
return $nl;
}
}
// Title
$pdf->SetFont('Arial','B',20);
$pdf->Cell(80);
$pdf->Cell(0,5,'RESUME',0,1);
$pdf->SetFont('Times','',12);
$pdf->Image('images.jpeg',150,20,-300);
$pdf->Cell(0,7,'Name: Tuba ',0,1);
$pdf->Cell(0,7,'Address: huiu,shydu9ik.sdysdu,jjsidu ',0,1);
$pdf->Cell(0,10,'_____________________________________________ ',0,1);
$pdf->SetFont('Times','B',12);
$pdf->Cell(0,7,'Objective:' ,0,1);
$pdf->SetFont('Times','',12);
$pdf->Cell(0,10,'To be a part of your company',0,1);
$pdf->SetFont('Times','B',12);
$pdf->Cell(0,20,'Education Profile:' ,0,1);
//Table with 20 rows and 4 columns
$pdf->SetWidths(array(20,80,80));
$pdf->Row(array('Sr No.','Qualification','College'));
$pdf->SetFont('Times','',12);
$pdf->Row(array('1.','SSC','ABC high school'));
$pdf->Row(array('2','HSC','XYZ College'));
$pdf->Row(array('3','Diploma','ARKP Polytechnic'));
4
Web Based Application Development Using PHP(22619)
$pdf->AliasNbPages();
$pdf->Output();
?>
Output:
?>
Output:
5
Web Based Application Development Using PHP(22619)
IV. Conclusion
Thus we have successfully completed the given experiment based on
a)Write a PHP Program to create a PDF document by using graphics concept
b) Write a simple PHP Program to create image using php