Draw Table while creating PDF document

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.
Adding table to PDF document by using Cell with equal width by using alignment & position after cell

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.
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 center 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
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 align.
Next parameter Background 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 )
Note the fifth parameter ( position ) is set to 1 as this is the fourth column and next cell has to start from new line . Fill set to false .

Full code is here
$pdf = new FPDF(); 

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

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


Output is here

Displaying database records in a table

With this knowledge of drawing table in a pdf document, we will learn how to use data from a MySQL table to display records in a table.

Collect records from MySQL table and display PDF document

MultiCell Cell()
Adding Image to PDF File
Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net


    riddhi desai




    One tutorial on MultiCell is available here, it can be used. We will try to develop one sample.


    Wonderful tutorial. Explained very well with examples and the result.Thank you very much.

    Post your comments , suggestion , error , requirements etc here

    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer