How to create tables in PDF document by using Cell function
<?Php
require "config.php";//connection to database
//SQL to get 10 records
$sql="select * from student LIMIT 0,10";
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$width_cell=array(20,50,40,40,40);
$pdf->SetFont('Arial','B',16);
//Background color of header//
$pdf->SetFillColor(193,229,252);
// Header starts ///
//First header column //
$pdf->Cell($width_cell[0],10,'ID',1,0,'C',true);
//Second header column//
$pdf->Cell($width_cell[1],10,'NAME',1,0,'C',true);
//Third header column//
$pdf->Cell($width_cell[2],10,'CLASS',1,0,'C',true);
//Fourth header column//
$pdf->Cell($width_cell[3],10,'MARK',1,0,'C',true);
//Third header column//
$pdf->Cell($width_cell[4],10,'GENDER',1,1,'C',true);
//// header ends ///////
$pdf->SetFont('Arial','',14);
//Background color of header//
$pdf->SetFillColor(235,236,236);
//to give alternate background fill color to rows//
$fill=false;
/// each record is one row ///
foreach ($dbo->query($sql) as $row) {
$pdf->Cell($width_cell[0],10,$row['id'],1,0,'C',$fill);
$pdf->Cell($width_cell[1],10,$row['name'],1,0,'L',$fill);
$pdf->Cell($width_cell[2],10,$row['class'],1,0,'C',$fill);
$pdf->Cell($width_cell[3],10,$row['mark'],1,0,'C',$fill);
$pdf->Cell($width_cell[4],10,$row['gender'],1,1,'C',$fill);
//to give alternate background fill color to rows//
$fill = !$fill;
}
/// end of records ///
$pdf->Output();
?>
PDF Output with records is here
Manik | 30-01-2019 |
Thanks for sharing example. WE have used your code and we are using issue like not displaying the record in each row wise in the pdf based on your example. Can you please help how to disaplay each record in separate in the PDF. |
smo1234 | 08-02-2019 |
There is a tutorial already there to display each record in a separate PDF |
12-03-2021 | |
thank you for sharing |
07-02-2023 | |
Hi there, How about if we text big in length, for example, one columns with 100 character, how can we deal with that? |