100% found this document useful (1 vote)
194 views6 pages

7 Practical

This document provides instructions for two PHP programming exercises: 1) Write a program to create a PDF document using the FPDF library and graphics concepts like adding pages, headers, footers, images, fonts, cells, etc. 2) Write a simple program to create an image using PHP functions like imagecreate, imagecolorallocate, and imagestring to set the background color, text color and add strings. The document explains the necessary theoretical background on FPDF functions and PHP image functions and provides sample code for both exercises.

Uploaded by

huzaifa Dabir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
194 views6 pages

7 Practical

This document provides instructions for two PHP programming exercises: 1) Write a program to create a PDF document using the FPDF library and graphics concepts like adding pages, headers, footers, images, fonts, cells, etc. 2) Write a simple program to create an image using PHP functions like imagecreate, imagecolorallocate, and imagestring to set the background color, text color and add strings. The document explains the necessary theoretical background on FPDF functions and PHP image functions and provides sample code for both exercises.

Uploaded by

huzaifa Dabir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Web Based Application Development Using PHP(22619)

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:

● Choice of measure unit, page format and margins


● Page header and footer management
● Automatic page break
● Automatic line break and text justification
● Image support (JPEG, PNG and GIF)
● Colors
● Links
● TrueType, Type1 and encoding support
● Page compression

FPDF requires no extension (except Zlib to enable compression and GD for GIF support). The latest
version requires at least PHP 5.1.

II. Minimum Theoretical Background


1. we create an FPDF object. The constructor is used here with the default values: pages are in
A4 portrait and the unit of measure is millimeter. It could have been specified explicitly with:
$pdf = new FPDF('P','mm','A4');
2. New Page can be added using: AddPage();
3. It's mandatory to select a font with SetFont():
$pdf->SetFont('Arial','B',16);
4. A cell is a rectangular area, possibly framed, which contains a line of text. It is output at the
current position. We specify its dimensions, its text (centered or aligned), if borders should be
drawn, and where the current position moves after it (to the right, below or to the beginning of
the next line).
$pdf->Cell(60,10,'Powered by FPDF.',0,1,'C');
5. ​Header()​ methods to process page headers
// Page header
function ​Header​()
{
​// Header Body
}
6. Footer()​ methods to process page footers.

// Page footer
function ​Footer​()
{

1
Web Based Application Development Using PHP(22619)

​// Footer Body


}
7. I​ mage()​ method by specifying its upper-left corner and its width. The height is calculated
automatically to respect the image proportions.
Image​(​'logo.png'​,​10​,​6​,​30​);
8. ​SetY()​ method which allows to set position at an absolute location in the page, starting from
the top or the bottom.
SetY​(-​15​);
III. Exercise:
1. Write a program to create PDF in php
Input:
<?php
require("./fpdf182/fpdf.php");

class PDF extends FPDF


{
// Page header
function Header()
{
// Logo
$this->Image('index.png',80,10,50);
// Arial bold 15
$this->SetFont('Arial','B',15);
$this->Cell(80);

// 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;
}
}

// Instantiation of inherited class


$pdf = new PDF();
$pdf->AddPage();

// 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:

2. Write a simple PHP Program to create image using php


Input:
<?php

// Create the size of image or blank image


$image = imagecreate(500, 300);
// Set the background color of image
$background_color = imagecolorallocate($image, 0, 153, 0);
// Set the text color of image
$text_color = imagecolorallocate($image, 255, 255, 255);
// Function to create image which contains string.
imagestring($image, 5, 180, 100, "Hello this is", $text_color);
imagestring($image, 3, 160, 120, "Web based Programming using
PHP", $text_color);
header("Content-Type: image/png");
imagepng($image);

?>
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

You might also like