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

时间:2016-04-22 11:57:26

标签: php fpdf

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

如果返回的行数大于第一页可以容纳剩余的行跳转到第二页但数据全部在这个位置,列没有对齐,并且它们会在每个页面生成时逐步降级。

我知道我需要设置一些行/行计数,如果达到行/行计数,请开始一个新的页面,但是即使在阅读了很多帖子之后我仍然知道我的知识空白关于如何做到的文件。

我的代码是:

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

任何人都可以向我提供一些帮助,而不是如何在第二页或第三页等页面上显示剩余的数据。

非常感谢你的时间。

干杯。

1 个答案:

答案 0 :(得分:1)

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

`$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始终计数

希望能有所帮助