PHP 7 FPDF Example to Create Colorful Table in PDF Document From Text File in Browser

PHP 7 FPDF Example to Create Colorful Table in PDF Document From Text File in Browser

<?php
 
require('fpdf/fpdf.php');
 
class PDF extends FPDF {

// Get data from the text file
function getDataFrmFile($file) {
 
// Read file lines
$lines = file($file);

// Get a array for returning output data
$data = array();

// Read each line and separate the semicolons
foreach($lines as $line)
$data[] = explode(';', chop($line));
return $data;
}
 
// Simple table
function getSimpleTable($header, $data) {

// Header
foreach($header as $column)
$this->Cell(40, 7, $column, 1);
$this->Ln(); // Set current position

// Data
foreach($data as $row) {
foreach($row as $col)
$this->Cell(40, 6, $col, 1);
$this->Ln(); // Set current position
}
}
 
// Get styled table
function getStyledTable($header, $data) {

// Colors, line width and bold font
$this->SetFillColor(1, 1, 0);
$this->SetTextColor(144,156,244);
$this->SetDrawColor(100, 1, 0);
$this->SetLineWidth(5);
$this->SetFont('', 'B');

// Header
$colWidth = array(40, 35, 40, 45);
for($i = 0; $i < count($header); $i++)
$this->Cell($colWidth[$i], 7,
$header[$i], 1, 0, 'C', 1);
$this->Ln();

// Setting text color and color fill
// for the background
$this->SetFillColor(1, 235, 255);
$this->SetTextColor(0);
$this->SetFont('');

// Data
$fill = 0;
foreach($data as $row) {

// Prints a cell, first 2 columns are left aligned
$this->Cell($colWidth[0], 6, $row[0], 'LR', 0, 'L', $fill);
$this->Cell($colWidth[1], 6, $row[1], 'LR', 0, 'L', $fill);

// Prints a cell,last 2 columns are right aligned
$this->Cell($colWidth[2], 6, number_format($row[2]),
'LR', 0, 'R', $fill);
$this->Cell($colWidth[3], 6, number_format($row[3]),
'LR', 0, 'R', $fill);
$this->Ln();
$fill=!$fill;
}
$this->Cell(array_sum($colWidth), 0, '', 'T');
}
}
// Instantiate a PDF object
$pdf = new PDF();
 
// Column titles given by the programmer
$header = array('Name','City','Age','Salary(In thousands)');
 
// Get data from the text files
$data = $pdf->getDataFrmFile('employees.txt');
 
// Set the font as required
$pdf->SetFont('Arial', '', 14);
 
// Add a new page
$pdf->AddPage();
$pdf->getSimpleTable($header,$data);
$pdf->AddPage();
$pdf->getStyledTable($header,$data);
$pdf->Output();
?>

Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment