fpdf 使用 foreach 循环生成多个页面

fpdf generate multiple pages using a foreach loop

我是 fpdf 的新手,我有一段打印数组值的短代码。我的问题是我不知道如何在单独的页面上输出这些值。

我的代码:

<?php
require('fpdf.php');
$pdf = new FPDF();

//Create an array
$myarray = array(1,2,3);

//loop through the array with a foreach and print the results on different pages
foreach($myarray as $value)
{
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);  
$pdf->Cell(40,10,$value);
$pdf->Output();

}

?>

目前代码只打印第一个值并停止。非常重视您的帮助。

谢谢。

您必须在 foreach 之外调用输出函数。在您的函数中,它在第一个循环中被调用,并在第一次中断循环。

require('fpdf.php');
$pdf = new FPDF();

$myarray = array(1,2,3);

$pdf->SetFont('Arial','B',16);  
foreach($myarray as $value){
    $pdf->AddPage();
    $pdf->Cell(40,10,$value);
}
$pdf->Output();