【问题标题】:FPDF + associative array key value columnFPDF + 关联数组键值列
【发布时间】:2015-06-12 08:12:37
【问题描述】:

我正在尝试使用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 中输出以下输出:

但我真的不知道如何创建这个表......我已经阅读了来自 fpdf 的 this 教程,但没有从中得到任何更明智的信息。有人可以帮我吗?

【问题讨论】:

    标签: php arrays pdf fpdf


    【解决方案1】:
    <?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();
    ?>
    

    【讨论】:

    • 谢谢,您现在是否可以更改行单元格中的填充?
    • 可能在函数 'Row' 中,你需要相应地设置 x 和 y 的位置,我会检查一下
    【解决方案2】:

    要使用此库创建表,您可以执行以下操作

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-09
      • 1970-01-01
      • 2016-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-08
      • 1970-01-01
      相关资源
      最近更新 更多