Search code examples
phpfpdffpdi

Add pages to PDF using FPDF if pagecount is not divisible by 4


Im trying to print some fpdf generated PDFs but if the page total is odd or not divisible by 4 then printing to a booklet is causing me some issues

eg total pages = 73 printing to a booklet on A3 paper needs 4 pages per A3 sheet

If i use this script

$pageCount = 73;
if ($pageCount % 4 != 0) {
$newpagecount = $pageCount += 4 - ($pageCount % 4);
}

while ($pageCount < $newpagecount) {
    $pageCount++;
//add page here and keep looping until it gets to the divisble by 4 number
}

I get 76 which is correct and divisible by 4 what i need it to do is add a blank page to my source document until i get to the new divisible by 4 pageCount?

Any suggestions? Thanks!


Solution

  • In your while loop simply add a call to the AddPage method to create new pages as needed.

    while ($pageCount < $newpagecount) {
        $pageCount++;
        $yourpdf->AddPage(); //add page here and keep looping until it gets to the divisble by 4 number
    }