Tag Archives: FPDF

Class to add some table functionality to FPDF

php-banner
<?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;
}

// Simple table
function BasicTable($header, $data)
{
    // Header
    foreach($header as $col)
        $this->Cell(40,7,$col,1);
    $this->Ln();
    // Data
    foreach($data as $row)
    {
        foreach($row as $col)
            $this->Cell(40,6,$col,1);
        $this->Ln();
    }
}

// Better table
function ImprovedTable($header, $data)
{
    // Column widths
    $w = array(40, 35, 40, 45);
    // 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,$row[0],'LR');
        $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,number_format($row[3]),'LR',0,'R');
        $this->Ln();
    }
    // Closing line
    $this->Cell(array_sum($w),0,'','T');
}

// Colored table
function FancyTable($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
    $w = array(40, 35, 40, 45);
    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,$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,number_format($row[3]),'LR',0,'R',$fill);
        $this->Ln();
        $fill = !$fill;
    }
    // Closing line
    $this->Cell(array_sum($w),0,'','T');
}
}

$pdf = new PDF();
// Column headings
$header = array('Country', 'Capital', 'Area (sq km)', 'Pop. (thousands)');
// Data loading
$data = $pdf->LoadData('countries.txt');
$pdf->SetFont('Arial','',14);
$pdf->AddPage();
$pdf->BasicTable($header,$data);
$pdf->AddPage();
$pdf->ImprovedTable($header,$data);
$pdf->AddPage();
$pdf->FancyTable($header,$data);
$pdf->Output();
?>

 

Share this post:
read more

Using FPDF – Basic commands and explanations to suit

Creating PDF’s with PHP using FPDF.  This will get added to over time.

First, include the FPDF class.

require('fpdf.php');

Set up the page:

$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);

Add a cell to the page.  Cells contain the elements on the page:

$pdf->Cell(20,10,"Some text to display on the page");

One thing to watch for is that this:

$pdf->Cell(20,10,"C1");
$pdf->Cell(20,10,"C2");
$pdf->Cell(20,10,"C3");
$pdf->Cell(20,10,"C4");

Will produce: C1    C2    C3    C4

To add some line breaks:

$pdf->Cell(20,10,"C1"); 
$pdf->Ln();
$pdf->Cell(20,10,"C2"); 
$pdf->Ln();
$pdf->Cell(20,10,"C3"); 
$pdf->Ln();
$pdf->Cell(20,10,"C4");

You can also add custom spacing to the line breaks:

$pdf->Ln(10);

So far we have:

require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(20,10,"Some text to output to the PDF");
$pdf->Ln();
$pdf->Cell(20,10,"Some more text to output to the PDF");

Now we need to output the PDF to something.  Browsers can be a bit fickle when displaying PDF’s on the screen:

$pdf->Output('../../temp/file.pdf','F');

The “F” flag tells the script to output the PDF as a file called file.pdf (temp is just the directory I’m using for testing and has no relevance to FPDF).

Flags also include “D”, which is to force a file to the browser with a download trigger and “I” which outputs the PDF inline.  I have found inline display of PDF’s from FPDF work best with the <object> flag within HTML.

Share this post:
read more