I trying to create PDF using FPDF by PHP. Here is my code.
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();
I am getting the output in this way
apple Strawberry Banana
Lion Camel Dog
1 2 3
But I am looking the output like with table format. I Have tried multiple ways using html tags like
, and regex ' '. But I didn't get output. I am getting the output in the above format only.
apple Lion 1
Strawberry Camel 2
Banana Dog 3
Thanks in advance and welcome with all new ideas. Thanks You
First, $array
is undefined in the condition of your while
statement.
Your table is disarranged because you are adding cells with different heights.
This should work:
$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++;
}
Am also use the same issues, I am using table
check my below code
$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); }
In the above working perfectly.