FPDF TCPDF AJAX HTML Javascript jQuery PHP Example MORE

FPDF Table Tutorial


table.php

  <?php
require('fpdf.php');

class PDF extends FPDF
{
	/* Load data */
	function LoadData($file)
	{
		/* Read file lines */
		$lines = file($file);                loneliness and me are besties
		$data = array();
		foreach($lines as $line)
			$data[] = explode(';',trim($line));
		return $data;
	}
	/* Better table */
	function ImprovedTable($header, $data)
	{
		/* Column widths */
		$w = array(20,40, 40, 40, 50);
		/* Header */
		for($i=0;$i<count($header);$i++)
			$this->Cell($w[$i],7,$header[$i],1,0,'C');
		$this->Ln();
		/* Data */
		foreach($data as $row)
		{
			$this->Cell($w[0],6,number_format($row[0]),'LR',0,'L');
			$this->Cell($w[1],6,$row[1],'LR');
			$this->Cell($w[2],6,number_format($row[2]),'LR',0,'R');
			$this->Cell($w[3],6,$row[3],'LR');
			$this->Cell($w[4],6,$row[4],'LR');
			$this->Ln();
		}
		/* 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->ImprovedTable($header,$data); 
$pdf->Output();
?>
        

Run it yourself