PDF from Database using FPDF

This solutions came to my mind when i was asked to allow registrar to create student certificates online when they were using the school's local website - similar to TISS certificate generator - i used the free FPDF library which is a PHP class which allows to generate PDF files with pure PHP - the aim is to allow the registrar of school to be able to take the student’s id and course id and generate the student's certificate for a course - the main aim is to show how the class is used in a simple and easy way for use in a small scale - There are two files. The first file, the user inputs the student id and course id and hits create certificate. The file points to the fpdf file that goes and creates a pdf file and displays the certificate
1 answer

PDF from Database using FPDF

First Page is a form to accept student id and course id

Create Certificate

Create Certificate

Student ID:
Course ID:

The second page (certificatereportpdf.php)

<?php
define('FPDF_FONTPATH','font/'); // so fpdf knows where the fonts are when it starts writing
require('mysql_table.php'); //on this page, the table selection was already done
$date=date(date);
class PDF extends PDF_MySQL_Table
{
function Header() // to make the certificate look nice
{
//Title
$this->SetFont('Arial','',14);

$this->Cell(0,6,'Certificate ',0,1,'C');
$this->Ln(10);

$this->Cell(0,6,'$date ',0,1,'C');// these are like text boxes
$this->Ln(10);

$this->Cell(0,6, 'This is the official Certificate.',0,1,'L');
$this->Ln(16);

//Ensure table header is output
parent::Header();
}
}

//Connect to database
mysql_connect('localhost','root','');
mysql_select_db('studentsdb');

$studentid=$_POST['studentid'];
$courseid=$_POST['courseid'];
$date=date(date);

//create a new page
$pdf=new PDF();
$pdf->Open();
$pdf->AddPage();

//table: specify columns

$pdf->AddCol('studentid',25,'Student ID','C');
$pdf->AddCol('firstname',25,'First Name','L');
$pdf->AddCol('lastname',25,'Last Name','L');
$pdf->AddCol('courseid',25,'Course ID','L');
$pdf->AddCol('result',35,'Result Type','L');
$pdf->AddCol('timestaken',30,'Organization','L');

$prop=array('HeaderColor'=>array(255,150,100),
'color1'=>array(210,245,255),
'color2'=>array(255,255,210),
'padding'=>2);

//insert the fields into the columns of the table
$pdf->Table('studentid,firstname,lastname,courseid,result,timestaken from amalgamation where studentid="$studentid" && courseid=$courseid order by ',$prop);

//show it
$pdf->Output();
?>

Taggings: