如何使用PHP数组创建PDF文件

时间:2016-10-22 08:35:35

标签: php arrays pdf pdf-generation fpdf

我试图通过PHP使用FPDF创建PDF。这是我的代码。

require 'fpdf.php';
$pdf= new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial', "", 18);
$pdf->Ln();
$fruits=["apple", "Strawberry", "Banana"];
$animals=['Lion', 'Camel', 'Dog'];
$numbers=[1,2,3];

$i=0;
while($i<count($array))
{
    $pdf->Cell(5, 5, $fruits[$i]);

    $pdf->Cell(35, 35, $animals[$i]);
$pdf->Cell(35, 35, $numbers[$i]);
    $i++;
}
$pdf->output();

我以这种方式获得输出

apple Strawberry Banana
Lion Camel Dog
1    2    3

但我正在查看表格格式的输出。我尝试过使用html标签的多种方式,例如
和正则表达式&#39; \ r \ n&#39;。但我没有得到输出。我只是以上面的格式获得输出。

apple         Lion   1
Strawberry    Camel  2
Banana        Dog    3

提前致谢并欢迎所有新想法。 谢谢你

2 个答案:

答案 0 :(得分:0)

首先,$array语句中的while未定义。

您的表格已被排除,因为您正在添加不同高度的单元格。

这应该有效:

$i = 0;
while ($i < count($fruits))
{
    $pdf->Cell(35, 8, $fruits[$i]);
    $pdf->Cell(35, 8, $animals[$i]);
    $pdf->Cell(35, 8, $numbers[$i]);
    // Insert a line break at the end of each row.
    $pdf->Ln();
    $i++;
}

答案 1 :(得分:-1)

我也使用相同的问题,我正在使用表

检查我的以下代码



     $pdf->AddPage();
        $pdf->SetFont('times','B',16);
        $htmlTable ="";
        while($i>count($array)){
            $htmlTable +='<TABLE>
            <TR>
            <TD>'$fruits[$i].'</TD>
            <TD>'.$animals[$i].'</TD>
            <TD>'.$numbers[$i].'</TD>
            </TR>
            </TABLE>';
            $pdf->WriteHTML2("<br>".$htmlTable);
        }

在上面完美地工作。