Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate footer when ToC spans multiple pages #548

Closed
Tillrzhtgrfho opened this issue Sep 19, 2022 · 2 comments
Closed

Duplicate footer when ToC spans multiple pages #548

Tillrzhtgrfho opened this issue Sep 19, 2022 · 2 comments

Comments

@Tillrzhtgrfho
Copy link

Tillrzhtgrfho commented Sep 19, 2022

In the footer of the first page, the actual number of pages and {nb} are overlapping.

If the Table of contents has multiple pages, the footer() function is called twice. Once when the ToC placeholder is inserted and another time when the actual ToC is created and a new page begins in render_toc().

Minimal code

This code creates an pdf with a footer and a Table of contents that spans multiple pages.

from fpdf import FPDF


class TestPDF(FPDF):   
    def render_toc(self, outline):
        self.x = self.l_margin
        self.set_font(style='', size=12)
        for section in outline:
            self.ln()
            self.cell(txt=section.name)
 
    def footer(self):
        self.set_y(-15)
        self.set_font("helvetica", "I", 8)
        self.cell(0, 10, f"Page {self.page_no()}/{{nb}}", align="C")
        
     
pdf = TestPDF()
pdf.set_font(family='helvetica', size=12)
pdf.add_page()
pdf.insert_toc_placeholder(TestPDF.render_toc, pages=2)
for i in range(1,80):
    pdf.set_font(style='B')
    pdf.start_section(f"Section {i}")
    pdf.cell(txt=f"Section {i}")
    pdf.ln()
pdf.output("footer_toc_test.pdf")

Some thoughts

If i am not mistaken, this would be solved for my example (and probably most simple footers) by first inserting the table of contents and then substituting the page numbers. Inside _putpages().
But this fix would not solve the underlying issue of the footer being called twice. I am not sure about that. In insert_toc_placeholder() the placeholder pages are created with self.add_page() which also creates a footer. Maybe it would be better to create "real" blank pages without a footer.

Environment

  • OS Windows and Linux
  • Python version 3.6.9
  • fpdf2 version 2.5.7
@Lucas-C
Copy link
Member

Lucas-C commented Sep 20, 2022

Thank you for reporting this @Tillrzhtgrfho!

@all-contributors please add @Tillrzhtgrfho for bug

I opened #552 to solve this

@allcontributors
Copy link

@Lucas-C

I've put up a pull request to add @Tillrzhtgrfho! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants