Draw Table while creating PDF document
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.
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
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
For all the cells to add borders we will keep the frame parameter inside Cell as 1
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.
For centre align we will keep the value as ‘C’
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 .
$pdf->Cell($width_cell,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 CellHere is a sample code for data cell ( fourth cell of row one )
$pdf->Cell($width_cell,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
Output is here
Displaying database records in a tableWith 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.
This article is written by plus2net.com team.