fpdf2 icon indicating copy to clipboard operation
fpdf2 copied to clipboard

call .write_html in footer cause RecursionError

Open socrateslee opened this issue 1 year ago • 4 comments

Describe the bug

Error details Call .write_html in footer will result in a recursive call _perform_page_break -> add_page -> footer loop, therefore result in a RecursionError: maximum recursion depth exceeded error.

Minimal code

import fpdf

def render():
    class MyPDF(fpdf.FPDF):
            def footer(sub):
                sub.set_y(-15)
                sub.write_html("<p>this is a footer</p>")
    pdf = MyPDF(format='A4')
    pdf.add_page()
    pdf.write_html("<p>this is main content</p>")
    pdf.output('test.pdf')

render()

Environment Please provide the following information:

  • Operating System: Windows with WSL2...
  • Python version: python 3.12
  • fpdf2 version used: fpdf2 @ git+https://github.com/py-pdf/fpdf2@6f7a207d9c0b7e3f537b6a5f9f25e6ab0b693f8e

socrateslee avatar Jul 10 '24 00:07 socrateslee

Thanks for reporting this, and welcome to fpdf2, @socrateslee !

That's quite an interesting bug you found there. We should probably disable automatic page breaks while actually processing a page break (and when writing the last footer at the end of the document).

gmischler avatar Jul 10 '24 07:07 gmischler

@allcontributors please add @socrateslee for bug

@socrateslee: would you like to submit a PR to fix this? A good starting point would be to add unit test for this cas in test/html/test_html.py

Lucas-C avatar Aug 19 '24 11:08 Lucas-C

@Lucas-C

I've put up a pull request to add @socrateslee! :tada:

allcontributors[bot] avatar Aug 19 '24 11:08 allcontributors[bot]

I opened #1247 to fix this.

Lucas-C avatar Aug 20 '24 06:08 Lucas-C