FPDF +关联数组键值列

时间:2015-06-12 08:12:38

标签: php arrays pdf fpdf

我尝试使用FPDF library创建PDF。我有一个像这样的数组:

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 'aez@dqd.com' (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)

现在我想在我的pdf中输出以下内容:

enter image description here

但我真的坚持如何创造这张桌子......我已经从fpdf上阅读了this教程,但没有从中得到任何明智的建议。我可以帮助我吗?

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

要使用此库创建表,您可以执行类似这样的操作

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