Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fpdf - going back to the previous page

Tags:

php

pdf

fpdf

I am generating pdf invoice using fpdf.

Some invoices containing many items, and details need to go into the second page. However, I need the total, and other details to be displayed on the first page.

Right now, if I can add new page like this: $pdf->AddPage();

But, this puts everything into the second page, whatever after this statement.

There seems to be no way to specify the page for either write or cell methods.

Rendering, and calculations are bit complex, thus don't want to store into a temp array and display after fully rendering the first page.

Thank you.

like image 287
Natkeeran Avatar asked Dec 03 '22 14:12

Natkeeran


2 Answers

I had a very similar issue and I went ahead and just added the function to the class:

function SetPage($num) {
    $this->page = $num;
}

This does literally what you'd expect. But, when you try to go back to the next page, you need to either set it explicitly or add to the Cell() functionality. Here is where it adds a page:

    if($this->y+$h>$this->PageBreakTrigger && !$this->InHeader && !$this->InFooter && $this->AcceptPageBreak())

This checks for AcceptPageBreak and obviously whether the height of the page is less than where we'll end up if we stay on the page. What I did was add a conditional here to check to see if there WAS another page below. If there was, rather than ADDING a new page, I went ahead and just set my Y to my margin and used the above function to visit that page. As such:

if ($this->page < count($this->pages)) {
        $this->SetPage(($this->page)+1);
        $this->y = .5;
    }

If you're uncomfortable with messing with FPDF, this might not be for you, but my realization after solving this issue was actually that the script was very easily navigated and left a great deal of room for simple improvements. Note that instead of checking above for another page, you could also add a variable that you could call that would temporarily tell the script that instead of adding a page, you just jump to next.

like image 90
Jared Alessandroni Avatar answered Dec 20 '22 00:12

Jared Alessandroni


if(!empty($this->replace) && !empty($this->replacement)){
    $this->pages[$n]=str_replace($this->replace,$this->replacement,$this->pages[$n]);
}

I added this code to the fpdf class in _putpages() right under /Page content line 2851

Example after adding above to code:

$pdf->replace = array("{paid_msg}","{balance}");
$pdf->replacement = array("Paid","0.00");

It can be a string or an array.

like image 38
dotcom Avatar answered Dec 20 '22 01:12

dotcom