FPDF + associative array key value column

1.5k views Asked by At

I'm trying to create a PDF with the FPDF library. I have an array like this:

array (size=27)
'JobTitle' => string 'test-job' (length=8)
'lastName' => string 'zaezaeza' (length=8)
'firstName' => string 'zaezaeaz' (length=8)
'street' => string 'azezaeza' (length=8)
'streetNr' => string '54' (length=2)
'place' => string 'zaezaeza' (length=8)
'postalCode' => string '9870' (length=4)
'country' => string 'zeazeza' (length=7)
'mobile' => string '056154871' (length=9)
'email' => string '[email protected]' (length=11)
'placeOfBirth' => string 'azzazae' (length=7)
'nationality' => string 'zaezzae' (length=7)
'driversLicence' => string 'yes' (length=3)
'driversLicenceCategory' => string 'b' (length=1)
'ownCar' => string 'yes' (length=3)
'celibate' => string 'celibate' (length=8)
'LowerSecondaryStudies' => string 'degreeTitle: zaeaz
                                   educationAuthority: zaezaezaeza
                                   graduationYear: 2014' (length=75)
'dutch' => string 'No selections' (length=13)
'french' => string '' (length=0)
'english' => string '' (length=0)
'german' => string '' (length=0)
'computerKnowledge' => string 'zaezaza' (length=7)
'interestsCareer' => string 'azezaeza' (length=8)
'strongPoints' => string 'zaeazeza' (length=8)
'contact' => string 'employees: true
                     employeesWho: zaezaeza' (length=41)
'worklocation' => string 'merelbeke: true' (length=17)
'motivationSolicitation' => string 'zaezaezaeza' (length=11)

Now I would like the following output in my pdf:

enter image description here

But I'm really stuck at how I can create this table.. . I've read this tutorial from fpdf but didn't get any wiser from it. Can somebody help me on my way?

2

There are 2 answers

2
Technoid On BEST ANSWER
<?php
require('mc_table.php');

$pdf=new PDF_MC_Table();
$pdf->AddPage();
$pdf->SetFont('Arial','',14);
//Table with 27 rows and 2 columns since you have 27 values 
$pdf->SetWidths(array(30,60));

$pdf->Row(array('Key','Value'));
$pdf->Row(array('JobTitle','test-job'));
$pdf->Row(array()); //Do the same thing as above
.
.
.
.
//Do it for 27 times since you have 27 values
//Or store them in array and use a loop
$pdf->Output();
?>
0
Faiz Rasool On

For creating the table with this library you can do something like this

<?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();
?>