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

Error when using multi-page tables in table of contents placeholder #1079

Closed
nocalla opened this issue Dec 31, 2023 · 5 comments
Closed

Error when using multi-page tables in table of contents placeholder #1079

nocalla opened this issue Dec 31, 2023 · 5 comments

Comments

@nocalla
Copy link

nocalla commented Dec 31, 2023

An error occurs when using fpdf.table in the render_toc_function of insert_toc_placeholder. From what I've been able to work out, this seems to be tied to how the table function refers to page numbers when doing a page break, but the specifics are beyond me. I don't think this is related to #136, as I've experimented with different placeholder page counts and table row counts.

Error details

Traceback (most recent call last):
  File "USER\PROJECT\TEST\test_pdf_error.py", line 32, in <module>
    PDF.output("Test.pdf")
  File "USER\PROJECT\.venv\Lib\site-packages\fpdf\fpdf.py", line 4964, in output
    self._insert_table_of_contents()
  File "USER\PROJECT\.venv\Lib\site-packages\fpdf\fpdf.py", line 4440, in _insert_table_of_contents
    tocp.render_function(self, self._outline)
  File "USER\PROJECT\TEST\test_pdf_error.py", line 14, in render_toc
    with pdf.table(
  File "USER\AppData\Local\Programs\Python\Python312\Lib\contextlib.py", line 144, in __exit__
    next(self.gen)
  File "USER\PROJECT\.venv\Lib\site-packages\fpdf\fpdf.py", line 4927, in table
    table.render()
  File "USER\PROJECT\.venv\Lib\site-packages\fpdf\table.py", line 202, in render
    self._fpdf._perform_page_break()
  File "USER\PROJECT\.venv\Lib\site-packages\fpdf\fpdf.py", line 3369, in _perform_page_break
    self.add_page(same=True)
  File "USER\PROJECT\.venv\Lib\site-packages\fpdf\fpdf.py", line 866, in add_page
    self._beginpage(
  File "USER\PROJECT\.venv\Lib\site-packages\fpdf\fpdf.py", line 944, in _beginpage
    page = self.pages[self.page]
           ~~~~~~~~~~^^^^^^^^^^^
KeyError: 2

Minimal code

To see how this performs with different numbers of entries, try changing the NUMBER_OF_ENTRIES variable.

from fpdf import FPDF
from fpdf.fonts import FontFace

NUMBER_OF_ENTRIES = 60
PLACEHOLDER_PAGES = 2


def render_toc(pdf: FPDF, outline: list) -> None:
    pdf.set_font(size=20)
    pdf.ln()
    pdf.cell(text="Table of contents:", align="C")
    pdf.ln()

    table_rows = [[x.name, str(x.page_number)] for x in outline]
    headings_style = FontFace(emphasis="")
    with pdf.table(
        rows=table_rows,
        headings_style=headings_style,
        width=int(pdf.epw),
        text_align="LEFT",
        borders_layout="SINGLE_TOP_LINE",
    ):
        pass


PDF = FPDF()
PDF.set_font(family="helvetica", style="", size=30)
PDF.add_page()
PDF.insert_toc_placeholder(
    render_toc_function=render_toc, pages=PLACEHOLDER_PAGES
)
for i in range(NUMBER_OF_ENTRIES):
    PDF.start_section(name=str(i), level=0)
    PDF.cell(text=str(i))
    PDF.ln()
PDF.output("Test.pdf")

Environment
Please provide the following information:

  • Operating System: Windows 11
  • Python version: Python 3.12.1
  • fpdf2 version used: 2.7.7
@Lucas-C
Copy link
Member

Lucas-C commented Jan 2, 2024

Hi @nocalla!

Thank you for the clear bug report 🙂

I'm going to investigate

@Lucas-C Lucas-C self-assigned this Jan 2, 2024
@Lucas-C
Copy link
Member

Lucas-C commented Jan 2, 2024

@allcontributors please add @nocalla for bug

Copy link

@Lucas-C

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

@Lucas-C
Copy link
Member

Lucas-C commented Jan 2, 2024

I fixed this in PR #1082 and merged into the master branch of this repo, but not released yet.

You can install this unreleased latest version this way, if you want to test that this fix solves your initial problem:

pip install git+https://github.com/py-pdf/fpdf2.git@master

@nocalla
Copy link
Author

nocalla commented Jan 2, 2024

Works perfectly, thanks for the quick resolution!

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