FPDF TCPDF AJAX HTML Javascript jQuery PHP Example MORE

FPDF Fancy Table


fancy-table.php

<?php
require('fpdf.php');
class PDF extends FPDF
{
	/* Load data */
	function LoadData($file)
	{
		/* Read file lines */
		$lines = file($file);
		$data = array();
		foreach($lines as $line)
			$data[] = explode(';',trim($line));
		return $data;
	}
	/* Colored table */
	function FancyTable($header, $data)
	{
		/* Colors, line width and bold font */
		$this->SetFillColor(69, 171, 82);
		$this->SetTextColor(255);
		$this->SetDrawColor(209, 212, 207);
		$this->SetLineWidth(.3);
		$this->SetFont('','B');
		/* Header */
		$w = array(20,40, 40, 40, 50);
		for($i=0;$i<count($header);$i++)
			$this->Cell($w[$i],7,$header[$i],1,0,'C',true);
		$this->Ln();
		/* Color and font restoration */
		$this->SetFillColor(224,235,255);
		$this->SetTextColor(0);
		$this->SetFont('');
		/* Data */
		$fill = false;
		foreach($data as $row)
		{
			$this->Cell($w[0],6,number_format($row[0]),'LR',0,'L',$fill);
			$this->Cell($w[1],6,$row[1],'LR',0,'L',$fill);
			$this->Cell($w[2],6,number_format($row[2]),'LR',0,'R',$fill);
			$this->Cell($w[3],6,$row[3],'LR',0,'L',$fill);
			$this->Cell($w[4],6,$row[4],'LR',0,'L',$fill);
			$this->Ln();
			$fill = !$fill;
		}
		/* Closing line */
		$this->Cell(array_sum($w),0,'','T');
	}
}

$pdf = new PDF();
/* Column headings */
$header = array('Sl No','Name', 'Roll No', 'Class', 'City');
/* Data loading */
$data = $pdf->LoadData('data.txt');
$pdf->SetFont('Arial','',12);
$pdf->AddPage();
$pdf->FancyTable($header,$data);
$pdf->Output();
?>
        

Demo