0

I create a form letter from an order table. Each order can have either 1 or 2 pages. The PDF contains all orders. Now I want to put the page numbers for every order on the PDF.

First Order: Pages 1 and 2, Second Order: Page 3, Third Order: Page 4.

The number of pages depends on how many articles a customer ordered (max 2 pages).

PageNo() uses the whole document for numbering. Maybe someone had the same problem?

Matthias Wiehl
  • 1,799
  • 16
  • 22
Jake
  • 175
  • 6
  • 17

1 Answers1

0

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();
Matthias Wiehl
  • 1,799
  • 16
  • 22