Pages

Saturday, March 20, 2021

Table Design in PDF By FPDF

 

PDF Table

By using Cell function we can create table. Read how this function take the parameters and draw different sizes of boxes with text inside. Each of our cell in our table will use this command.
Width :
We will use one array to declare the width of each cell ( we used 4 columns ), based on the width requirement we will fill our array with dimensions. For displaying name we need more width and for displaying mark we need less width.
$width_cell=array(10,30,20,30);
Height:
This is constant throughout the table so we kept it at 10
Text :
As per requirement we will add text to cell, we will set it to Bold and size 12 for header and normal with size 10 for inside data
Border :
For all the cells to add borders we will keep the frame parameter inside Cell as 1
Position :
We kept it 0 for keeping the next cell to the right of present cell, however for fourth column (Rightmost ) value is 1 as we have to move to next line after drawing the cell.
Align :
For centre align we will keep the value as ‘C’
Fill :
Background for all header cells we kept it as true, for data cells we kept it as false. Used SetFillColor for type of colour to be given to header .

Here is the code for second header cell
$pdf->Cell($width_cell[2],10,'CLASS',1,0,C,true);
The first parameter ( width ) takes the value of 2nd element of array
next parameter Height is set at 10
Next parameter Text is 'CLASS'
Next parameter border is set to 1 to draw border in all four sides
Next parameter Position is set to 0 to keep next cell to the right of it.
Next parameter Alignment is 'C' for center allign.
Next parameter Backgrond Fill is kept as true to fill with colour set previously.

Data Cell:


Here is a sample code for data cell ( fourth cell of row one )
$pdf->Cell($width_cell[3],10,'75',1,1,C,false); 
Note the fith parameter ( position ) is set to 1 as this is the fourth columand next cell has to start from new line . Fill set to false .

Full code is here
<?Php
require('fpdf.php');
$pdf = new FPDF(); 
$pdf->AddPage();
$pdf->SetFont('Arial','B',12);

$width_cell=array(10,30,20,30);
$pdf->SetFillColor(193,229,252); // Background color of header 
// Header starts /// 
$pdf->Cell($width_cell[0],10,'ID',1,0,C,true); // First header column 
$pdf->Cell($width_cell[1],10,'NAME',1,0,C,true); // Second header column
$pdf->Cell($width_cell[2],10,'CLASS',1,0,C,true); // Third header column 
$pdf->Cell($width_cell[3],10,'MARK',1,1,C,true); // Fourth header column
//// header is over ///////

$pdf->SetFont('Arial','',10);
// First row of data 
$pdf->Cell($width_cell[0],10,'1',1,0,C,false); // First column of row 1 
$pdf->Cell($width_cell[1],10,'John Deo',1,0,C,false); // Second column of row 1 
$pdf->Cell($width_cell[2],10,'Four',1,0,C,false); // Third column of row 1 
$pdf->Cell($width_cell[3],10,'75',1,1,C,false); // Fourth column of row 1 
//  First row of data is over 
//  Second row of data 
$pdf->Cell($width_cell[0],10,'2',1,0,C,false); // First column of row 2 
$pdf->Cell($width_cell[1],10,'Max Ruin',1,0,C,false); // Second column of row 2
$pdf->Cell($width_cell[2],10,'Three',1,0,C,false); // Third column of row 2
$pdf->Cell($width_cell[3],10,'85',1,1,C,false); // Fourth column of row 2 
//   Sedond row is over 
//  Third row of data
$pdf->Cell($width_cell[0],10,'3',1,0,C,false); // First column of row 3
$pdf->Cell($width_cell[1],10,'Arnold',1,0,C,false); // Second column of row 3
$pdf->Cell($width_cell[2],10,'Three',1,0,C,false); // Third column of row 3
$pdf->Cell($width_cell[3],10,'55',1,1,C,false); // fourth column of row 3

$pdf->Output();

?>

No comments:

Post a Comment