Menu

Strange behaviour defining a footer for the last page when using AutoPageBreak and writeHTML

Help
2017-07-24
2017-07-25
  • Abrar Sheikh

    Abrar Sheikh - 2017-07-24

    Hi,
    I'm developing a report that has a different footer on it's last page. Following Nicola's recommended approach I've overriden the Close() function on my custom TCPDF subclass like so:

    public function Close() {
    $this->last_page_flag = true;
    parent::Close();
    }

    And then on the Footer() function I do something like:

    function Footer(){
    if($this->last_page_flag){
    $footer_html = '...'; //HTML for the footer on the last page
    }
    else{
    $footer_html = '...'; //HTML for the rest of the pages
    }

    $this->setY(-$this->_footerMargin);
    $this->writeHTML($footer_html);
    

    }

    I'm using writeHTML to print HTML tables with the content of the report. This content can span several pages so I'm using AutoPageBreak. This implementation works most of the time, but there are certain situations where it prints the incorrect footer (doesn't print the last page footer when it's actually the last page). This happens when the content approaches the autopagebreak threshold (although never reaching it). In the first attachment (correct-footer.png) you can see an example of the correct footer (I've erased all the sensitive information on the header). If I add a few lines to the report (still only 1 page) it prints the wrong footer, that is, the footer for all the pages but the last (second attachment, wrong-footer.png). As you can see there's only 1 page on the report and it's the last obviously, so it should print the footer that you can see on example 1. This bug happens only when AutoPageBreak is set to true. If I disable AutoPageBreak example 2 works fine (prints the footer of the last page).

     

    Last edit: Abrar Sheikh 2017-07-24
  • Abrar Sheikh

    Abrar Sheikh - 2017-07-25

    Can someone help me?

     
    • Simon

      Simon - 2017-07-25

      I've not looked at your specific example, but if I recall correctly, it
      will have created a new page when it met the page break, and then the new
      empty page will have been removed when the pdf closed.

       

      Last edit: Simon 2017-07-25
  • Abrar Sheikh

    Abrar Sheikh - 2017-07-25

    Hi Simon,
    Yes, You are right the new page is empty and i just want to print the footer in the next page...How can i resolve this problem. Please help.

     
  • Simon

    Simon - 2017-07-25

    The correct-footer.png attachment is missing so I can't see what it is supposed to look like.

    Have a look in TCPDF.php and search for this section:

                            if ($plendiff == 0) {
                                // remove last blank page
                                $this->deletePage($this->numpages);
                            }
    

    Commenting it out may accomplish what you want.

     
  • Simon

    Simon - 2017-07-25

    Or you could run it as a transaction to find out how many pages there will be, then set a flag that makes the footer appear on the specific page number and run it again.

     
  • Abrar Sheikh

    Abrar Sheikh - 2017-07-25

    Hi Simon,
    Thank you for quick response. the Correct footer is attahced please see it.

     
  • Abrar Sheikh

    Abrar Sheikh - 2017-07-25

    Hi Simon,
    After commenting below code it is working fine.

    if ($plendiff == 0) {
    // remove last blank page
    $this->deletePage($this->numpages);
    }

    But i have one more thing to ask you that in my company we are using this tcpdf liabrary for other PDF generation too so if i comment the above code will it affect to other PDFs. Please let me know.
    And Thank you do much for your help.

     
  • Simon

    Simon - 2017-07-25

    Yes it will. This line has the effect of removing the last page if it is blank (such as when added by autopagebreak).

     
  • Abrar Sheikh

    Abrar Sheikh - 2017-07-25

    How can i solve with transaction. How can i use the transaction in this situation.

     
  • Simon

    Simon - 2017-07-25

    Have a look at this example:
    https://tcpdf.org/examples/example_047/

    You can start a transaction, then at the end of your operations, count the pages (or check to see if the current page is blank) and then work out if the current page or the previous page needs the end footer.

    If it is the previous page you can rollback the transaction and run it again specifying the page number that requires the footer.

     

Log in to post a comment.