Fpdf 在数据行到达第一页末尾时添加另一页

Fpdf add another page when data rows reach the end of first page

我正在尝试编写一个使用 FPDF 生成 PDF 文档的 PHP 脚本。到目前为止,我能够生成 PDF,但前提是从我的 MySQL 查询返回的行数小于页面高度。

如果返回的行数大于第一页可以容纳的剩余行数,则跳转到第二页,但数据到处都是,列没有对齐,并且它们会随着每一页的生成而逐渐下降。

我知道我需要进行某种 line/row 计数,如果达到 line/row 计数,则开始一个新页面,但我对此一无所知即使在阅读了许多关于如何做到这一点的帖子和文档之后。

我的密码是:

class PDF extends FPDF
{

// Page footer
function Footer()
{
    // Position at 1.5 cm from bottom
    $this->SetY(-15);
    // Arial italic 8
    $this->SetFont('Arial','I',8);
    // Page number
    $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
}

$pdf=new FPDF();
$pdf->AliasNbPages();
$pdf->AddPage('L');

$pdf->SetFont('Arial', 'B', 18);
$pdf->Text(230, 18, 'iMaint');

$pdf->SetFont('Arial', 'B', 18);
$pdf->Text(250, 18, 'Jobsheet');

$pdf->SetY(28);
$pdf->SetX(10);
$pdf->SetFont('Arial', '', 11);
$pdf->Text(10, 28, 'Jobsheet date:');

$pdf->SetY(8);
$pdf->SetX(37); 
$pdf->SetFont('Arial', '', 11);
$pdf->cell(10,38, $ToDay);

$pdf->SetY(13);
$pdf->SetX(10); 
$pdf->SetFont('Arial', '', 11);
$pdf->cell(10,38, 'Audit date:');

$pdf->SetY(13);
$pdf->SetX(37); 
$pdf->SetFont('Arial', '', 11);
$pdf->cell(10,38, date("d-m-Y", strtotime($row_RoomAudit['CompStamp'])));

$pdf->SetFont('Arial', '', 12);
$pdf->Text(240.5, 28, 'Hotel ID:');

$pdf->SetY(10);
$pdf->SetX(183.2);
$pdf->SetFont('Arial', 'B', 18);
$pdf->Text(10, 18, 'Room:');

$pdf->SetFont('Arial', '', 12);
$pdf->SetY(23);
$pdf->SetX(260);    
$pdf->cell(14,8, $_SESSION['hotel']);

$pdf->SetY(12);
$pdf->SetX(30);
$pdf->SetFont('Arial', 'B', 18);
$pdf->cell(14,8, $row_RoomAudit['Room']);

$pdf->Ln(10);

//Fields Name position
$Y_Fields_Name_position = 40;
//Table position, under Fields Name
$Y_Table_Position = 46;

$pdf->SetFillColor(232,232,232);
$pdf->SetFont('Arial','B',11);
$pdf->SetY($Y_Fields_Name_position);
$pdf->SetX(10);
$pdf->Cell(32,6,'Seq',1,0,'L',1);
$pdf->SetX(32);
$pdf->Cell(130,6,'Item',1,0,'L',1);
$pdf->SetX(130);
$pdf->Cell(155,6,'Job description',1,0,'L',1);

$pdf->Ln();

//Initialize the 3 columns and the total
$column_seq = "";
$column_seq_header = "";
$column_job_description = "";
$column_completed = "";
$column_engineer = "";
foreach ($row_RoomAudit as $key => $val) {
     if (strpos($val, '1') !== FALSE && substr( $key, 0, 3)=='Seq' ){
         mysql_select_db($database_iMaint, $iMaint);
        $query_QuestionLookup = sprintf("SELECT SeqHeader, SeqText FROM SequenceNo WHERE SeqID = '".$key."'");
        $QuestionLookup = mysql_query($query_QuestionLookup, $iMaint) or die(mysql_error());
        $row_QuestionLookup = mysql_fetch_assoc($QuestionLookup);
        $totalRows_QuestionLookup = mysql_num_rows($QuestionLookup);

        $seq = $key;
        $Seq_header = $row_QuestionLookup['SeqHeader'];
        $jobdescription = $row_QuestionLookup['SeqText'];

        $column_seq = $column_seq.$seq."\n";
        $column_seq_header = $column_seq_header.$Seq_header."\n";
        $column_job_description =   $column_job_description.$jobdescription."\n";

        $pdf->SetFont('Arial','',8);
        $pdf->SetY($Y_Table_Position);
        $pdf->SetX(10);
        $pdf->MultiCell(22,6,$column_seq,1);
        $pdf->SetY($Y_Table_Position);
        $pdf->SetX(32);
        $pdf->MultiCell(98,6,$column_seq_header,1);
        $pdf->SetY($Y_Table_Position);
        $pdf->SetX(130);
        $pdf->MultiCell(155,6,$column_job_description,1);
    }
}


$i = 0;
$pdf->SetY($Y_Table_Position);
while ($i < $totalRows_RoomAudit)
{
$pdf->SetX(10);
$pdf->MultiCell(184,6,'',1);
$i = $i +1;
}

$pdf->Output();

任何人都可以帮助我如何处理在第二页或第三页等页面上显示剩余数据的rwos。

非常感谢您的宝贵时间。

干杯。

添加每页最大记录以确保每页显示记录有限制,这是您需要添加的代码

`$column_completed = "";
$column_engineer = "";
$max_per_page = 15; // adjust for your max per page this mean 16 record per page
$counter_rec = 0;
......`

当计数器超过每页的最大记录数时创建新页面

if (strpos($val, '1') !== FALSE && substr( $key, 0, 3)=='Seq' ){ if ($counter_rec > 0 && $counter_rec % 15 == 0){ $pdf->AddPage('L'); // rewrite header for next page here } ......

之后你需要确保 $counter_rec 总是向上计数

希望能帮到你