Skip to content
Related Articles
Open in App
Not now

Related Articles

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

Improve Article
Save Article
  • Last Updated : 09 Sep, 2022
Improve Article
Save Article

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.


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.


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->SetDrawColor(128, 0, 0);
        $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);
        // Setting text color and color fill
        // for the background
        $this->SetFillColor(224, 235, 255);
        // 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->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

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



Table in PDF file

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!