Using FPDF to print barcodes in table, but last row prints after table, how to fix this?

  fpdf, mysql, php

output here

I am printing barcodes in table using getX() and getY(). but after the
table, at the end of page, the barcode is still printing. how can i
stop this problem and only print in rows.

$q="select * FROM pack where bogie_type_id_='$type' order by activity * 1 ";
$statement = $connect->prepare($q);
$result = $statement->fetchAll();

foreach ($result as $row) {

 $pdf->Code39($pdf->GetX()+3, $pdf->GetY()+3, '%'.$row['activity'].' START',1,10);
 $pdf->Code39($pdf->GetX()+200, $pdf->GetY()+3, '%'.$row['activity'].' END',1,10);


Source: Ask PHP