Search code examples
fpdf

FPDF Multiple Documents Generated In One PDF, Need To Reset Page No Per Document


I am using FPDF to generate invoices and I had to work around an issue when printing multiple documents. I am generating multiple (anywhere from 2 to 100+) invoices for print so I built a foreach loop around my FPDF code to put all my invoices in one PDF so I can hit print and be done. The problem is, FPDF uses PageNo() and {nb} to generate page numbers and the total page count in the header and if I have 500 pages it lists as Page: 1 of 500, Page: 2 of 500, Page: 3 of 500... etc.

I need to figure out a way to generate a page count and current page by invoice. So if the first invoice is 3 pages it says Page: 1 of 3, Page: 2 of 3, etc. and the next invoice is 5 pages, Page: 1 of 5, Page: 2 of 5, etc. ...even though it's all in the same PDF.

    $pdf=new PDF();
    foreach ($array_invoices as $invoice_number) {

            ...FPDF Template Here...

            /**
             * The following code checks the page length
             * If the page is over a certain length it will create a new page.
             * If not, it will add the footer.
             */

            if($pdf->GetY() < 225)
                    $pdf->SetY(238);
            elseif($pdf->getY() > 240)
                    $pdf->AddPage();
    }

    $pdf->Output();

The template has an include to the header which outputs the page number...

$this->Cell(94,0,'page '.$this->PageNo().'/{nb}' ,0,0,'R');

Solution

  • It is possible to create pagegroups, where every group has its own pagerange there is already an addon that will do the work for you: fpdf addon page groups