0

The goal I am trying to achieve is to run a mysql query, place the output into a foreach and then dump all the results into a pdf document using the awesome FPDF library. However only the last result record is making it into the pdf library. Here is mysql query:

 $sql1 = "SELECT 
 mytable.AddressLine1,
 mytable.OrgID
 FROM
 mytable";
 $results = $db->query($sql1);

Here is my PHP:

foreach ($results as $all_data) {
$border = 0;
$cardWidth = 1;
$cardHeight = 1;
require_once('fpdf/fpdf.php');
require_once('fpdf/fpdi.php');
$pdf = new FPDI('P','in','Letter');
$pdf->setSourceFile(realpath(ROOT_DIR . 'view' . URL_PATH . 'snTemplate.pdf'));
$pdf->AddPage();
$template = $pdf->importPage(1);
$pdf->useTemplate($template);
$pageNumber = 1;
$pdf->SetFont('Arial', '', 6);
$pdf->SetXY(0, 9.8); 
$pdf->MultiCell(8.5, .2, 'Page ' . $pageNumber, $border,'C');
$pdf->SetXY(0, 9.9);
$pdf->MultiCell(8.5, .2,' Report Form ', $border, 'C');
$pdf->SetXY(0, 10);
$pdf->MultiCell(8.5, .2,
$all_data['AddressLine1'] . ' - ' . $all_data['OrgID'], $border, 'C');
$pdf->Output('Name' . 'sanctionReport' . '.pdf','I');
}

Again there are four results in the table, but only the last recordset is output into the PDF, I would like all results to appear on their individual page, all with in the same report, really dont want to have multiple download boxes on the users screen... they tend to get frightened!

1 Answer 1

2

in each for iteration you recreate the pdf. Move the $pdf = new FPDI(..) before the for loop. And move the pdf->Output after the for loop (Output generate the pdf from the data you give it).

Sign up to request clarification or add additional context in comments.

1 Comment

@ blejzz - that did it. thank you for pointing me in right direction!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.