1

I am trying to write a PHP script that uses FPDF to produce a PDF document. So far I am able to produce a PDF but only when the number of rows returned from my MySQL query are less than the page height.

If the number of rows returned are greater than the first page can accommodate the remaining rows jump to a second page but the data is allover the place, columns are not aligning and they seam to stepdown as each page to produced.

I know I need to put some sort of line/row count in place and if the line/row count is reached, start a new page but heres where I am totally a at blank with my knowledge even after reading many posts and documents on how this is done.

The code I have is:

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

Can anyone offer me some help no how to deal with displaying the remaining rwos of data on the second or third and so on pages.

Many thanks in advance for your time.

Cheers.

1 Answer 1

1

Add maximum record per page to make sure you have limit per page to display records, here is the code you need to add

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

Create New Page when counter exceed maximum record per page

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 } ......

After that you need to make sure the $counter_rec always count up

Hope that can helped

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.