Skip to content
Related Articles

Related Articles

Improve Article

How to create a table in PDF file from external text files using PHP ?

  • Last Updated : 26 Aug, 2021

In this article, we will learn to create a table in a PDF file from an external text file with PHP by using FPDF. It is a free PHP class that contains many functions for creating and modifying PDFs. The FPDF class includes many features like page formats, page headers, footers, automatic page break, line break, image support, colors, links, and many more.

Approach: You need to download the FPDF class from the FPDF website and include it in your PHP script.

require('fpdf/fpdf.php');

Instantiate and use the FPDF class according to your need as shown in the following examples.

$pdf=new FPDF();

Example: The following example generates a table in a PDF file from an external text file “employees.txt” having employee data. The file can be downloaded or previewed as needed. Refer to the comments in the code for better understanding and customization as per the application’s needs.

PHP




<?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(255, 0, 0);
        $this->SetTextColor(255);
        $this->SetDrawColor(128, 0, 0);
        $this->SetLineWidth(.3);
        $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(224, 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();
?>

employees.txt: The following is the content for file “employees.txt” which is used in the above HTML file.

Aman;Varanasi;33;80750
Beena;Bombay;30;10100
Derek;Culcutta;43;52950
Fanny;Hubli;30;51000
Tom;Pondicherry;23;58000
Gleny;Bombay;32;82000
George;Atlanta;32;10500
Ishita;Dubai;34;36940
Iman;Rome;30;57560
Lily;Ranikhet;25;42400
Nihita;Agra;41;15600
Polima;Jaipur;45;99500
Seela;Madras;50;39300
Swati;Spain;41;88390
Janu;London;24;58860

Output:

Table in PDF file




My Personal Notes arrow_drop_up
Recommended Articles
Page :