php create pdf file from database

In this section, we study about the pdf file generate which is retrieve from database. There are provides diffrents option likes PDF, FPDF, R & OS, panda etc. We are use FPdf class which is used for generating pdf class without any library. Fpdf is class to generate pdf file , Here F for Free, so it is tree class for generating pdf file.

Fpdf have a lot of adavances which is defines below:

  1. Choice of measure unit , page format and margins like font size,alignment etc
  2. Free source class file i.e F stands for free
  3. Page header and footer management : we can defines same header and footer parts
  4. Automatic page break i.e if contents is complete then written into the new page
  5. Automatic line break and text justification 
  6. Image support (JPEG, PNG and GIF)
  7. Colors
  8. Links
  9. TrueType, Type1 and encoding support
  10. Page compression
  11. Locality font supports

What about performance?

The Fpdf makes documents easilly developed compares to the formal pdf librarries. It is better performance compare to the other libraries.

First, we must includes the Fpdf class. We must download that class which is easily available on net. Then makes Db model controller defines the function of connection, selection database, execution of query and number of row in the database specified table.These queries are to be exectes in this models.

class DBController {
	private $host = "localhost";
	private $user = "root";
	private $password = "";
	private $database = "igeniusdev";
	
	function __construct() {
		$conn = $this->connectDB();
		if(!empty($conn)) {
			$this->selectDB($conn);
		}
	}
	
	function connectDB() {
		$conn = mysql_connect($this->host,$this->user,$this->password);
		return $conn;
	}
	
	function selectDB($conn) {
		mysql_select_db($this->database,$conn);
	}
	
	function runQuery($query) {
		$result = mysql_query($query);
		while($row=mysql_fetch_assoc($result)) {
			$resultset[] = $row;
		}		
		if(!empty($resultset))
			return $resultset;
	}
	
	function numRows($query) {
		$result  = mysql_query($query);
		$rowcount = mysql_num_rows($result);
		return $rowcount;	
	}

Second step in the controller side, make fpdf class object and defines the parameter likes add page and set font etc. We use addpage() for the new pages to be added. Then we also defines set font defines font name, font size etc.

we retrieve data from database in array and retrieve specified values from array. That values to be print in the specified cell. Then makes display output it. The code are given below:

require_once("dbconfig.php");
$db_handle = new DBController();
$result_data = $db_handle->runQuery("SELECT * FROM emplyee");
$header_data = $db_handle->runQuery("SELECT `COLUMN_NAME` 
FROM `INFORMATION_SCHEMA`.`COLUMNS` 
WHERE `TABLE_SCHEMA`='igeniusdev' 
    AND `TABLE_NAME`='emplyee'");

require('fpdf/fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',12);		
foreach($header as $heading) {
	foreach($header_data as $header_details)
		$pdf->Cell(90,12,$header_details,1);
}
foreach($result as $row) {
	$pdf->SetFont('Arial','',12);	
	$pdf->Ln();
	foreach($result_data as $result_details)
		$pdf->Cell(90,12,$result_details,1);
}
$pdf->Output();

 

Let's Think together, Say Something !