<?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();
?>
Download and Install fpdf class from https://www.fpdf.org/
Keep a copy of fpdf.php file in the same directory
Keep the font directory inside in the same directory.
Use the SQL_dump.txt file to create student table in your MySQL database
Open config.php file to enter your MySQL login details.
Open index.php file to see the records in your browser ( Not PDF ).
Open index-pdf.php file to generate PDF document.
Open index1-pdf.php file to generate PDF document with link to breakup of marks.
Connecting database and executing Query
To manage data we have to connect to MySQL database and execute query to get our date. Here there are two ways to use PHP drivers to connect to MySQL and execute the functions for getting records.
You can download both the scripts inside the same Zip file. Inside MySQLI folder you can get same scripts with MySQLi connection. ( change the config.php file here also and place fpdf.php with font directory inside this folder)
This article is written by plus2net.com team.
https://www.plus2net.com
plus2net.com
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.