The expected result can be achieved by subclassing FPDF's FPDF
class, and adding an $orderPageNo
property to keep track of the current order's "sub-page number". You can then use that property in your customized Header
or Footer
method.
Example:
<?php
require('fpdf/fpdf.php');
class PDF extends FPDF {
protected $orderNumber, $orderPageNo;
function AcceptPageBreak() {
$accept = parent::AcceptPageBreak();
if ($accept) {
$this->orderPageNo++;
}
return $accept;
}
function Header() {
$this->Cell(50, 30, 'Order #'.$this->orderNumber);
$this->Cell(50, 30, 'Page '.$this->orderPageNo, 0, 1);
}
function Order($orderNumber, $items) {
$this->orderNumber = $orderNumber;
$this->orderPageNo = 1;
$this->AddPage();
for ($i = 1; $i <= $items; $i++) {
$this->Cell(30, 12, 'Item #'.$i, 1, 1);
}
}
}
$pdf = new PDF();
$pdf->SetFont('Arial', '', 12);
$orders = array(1 => 15, 2 => 25, 3 => 35);
foreach ($orders as $orderNumber => $items) {
$pdf->Order($orderNumber, $items);
}
$pdf->Output();