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

ToC rendering orientation "sticks" when adding landscape pages to end of document #1312

Closed
kevinthenet opened this issue Dec 2, 2024 · 6 comments
Assignees

Comments

@kevinthenet
Copy link

kevinthenet commented Dec 2, 2024

When a document ends with pages in landscape mode, the rendering mode for the table of contents is "stuck" in landscape when the render_toc_function is called. However, the page itself renders in portrait, leading to some weird behavior.

I would expect one of the following behaviors:

  • the orientation to be configurable via parameters for pdf.insert_toc_placeholder
  • orientation to be reset to portrait by default when invoking render_toc_function
  • orientation to be configurable at the top-level (something like pdf.set_orientation(orientation="P"))

Error details

If an exception is raised, it is very important that you provide the full error message.
Otherwise members of the fpdf2 community won't be able to help you with your problem.

There's no related error message, but below are some proofs this is a bug from the minimal example below:

epw (title page): 190.0015555555555
epw (toc placeholder insert): 190.0015555555555
epw (portrait content): 190.0015555555555
epw (landscape content): 277.0000833333333
epw (at toc render time): 277.0000833333333

This happens for the case where landscape content is added at the end of the document and does not occur if the document ends with portrait content

Minimal code
Please include some minimal Python code reproducing your issue:

from fpdf import FPDF

pdf = FPDF()

# Global Styles
pdf.set_font("Helvetica", size=12)


# from 2.8.1 test_outline.py
def p(pdf, text, **kwargs):
    "Inserts a paragraph"
    pdf.multi_cell(
        w=pdf.epw,
        h=pdf.font_size,
        text=text,
        new_x="LMARGIN",
        new_y="NEXT",
        **kwargs,
    )


# title page
pdf.add_page(orientation="P")
pdf.cell(w=0, text="Title Page")
print(f"epw (title page): {pdf.epw}")


# table of contents
# from 2.8.1 test_outline.py
def render_toc(pdf, outline):
    pdf.y += 50
    pdf.set_font("Helvetica", size=16)
    pdf.underline = True
    p(pdf, "Table of contents:")
    pdf.underline = False
    pdf.y += 20
    pdf.set_font("Helvetica", size=12)
    print(f"epw (at toc render time): {pdf.epw}")
    for section in outline:
        link = pdf.add_link(page=section.page_number)
        p(
            pdf,
            f'{" " * section.level * 2} {section.name} {"." * (60 - section.level*2 - len(section.name))} {section.page_number}',
            align="C",
            link=link,
        )


# note: we add page explicitly in portrait orientation
pdf.add_page(orientation="P")
# setup placeholder
pdf.insert_toc_placeholder(render_toc_function=render_toc)
print(f"epw (toc placeholder insert): {pdf.epw}")


# portrait content
pdf.add_page(orientation="P")
pdf.start_section("1.0 First Section", level=0)
p(pdf, text="Lorem ipsum dolor sit amet, consectetur adipiscing elit.")

pdf.start_section("1.1 First Subsection", level=1)
p(pdf, text="Cras dignissim arcu euismod metus rhoncus, a vehicula nisi dapibus.")
print(f"epw (portrait content): {pdf.epw}")


# landscape content at the end of the document
pdf.add_page(orientation="L")
pdf.start_section("2.0 Second Section", level=0)
p(pdf, text="Nullam tempus magna quam, ac dictum neque blandit quis.")

pdf.start_section("2.1 Second Subsection", level=1)
p(pdf, text="Proin ultrices lorem in posuere elementum. Sed vel arcu risus.")
print(f"epw (landscape content): {pdf.epw}")

# # Uncomment to test if adding portrait content at the end still causes bug
# pdf.add_page(orientation="P")
# pdf.start_section("3.0 Third Section", level=0)
# p(pdf, text="Lorem ipsum dolor sit amet, consectetur adipiscing elit.")

# pdf.start_section("3.1 Third Subsection", level=1)
# p(pdf, text="Cras dignissim arcu euismod metus rhoncus, a vehicula nisi dapibus.")
# print(f"epw (portrait content): {pdf.epw}")

# output will render the table of contents in landscape
pdf.output("landscape-toc.pdf")

Environment
Please provide the following information:

  • Operating System: MacOS
  • Python version: 3.12.6
  • fpdf2 version used: 2.8.1

Please note this issue persists when using the latest unreleased version @ master branch.

@Lucas-C
Copy link
Member

Lucas-C commented Dec 3, 2024

Thank you for the bug report @kevinthenet 👍

@allcontributors please add @kevinthenet for bug

Copy link

@Lucas-C

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

@Lucas-C
Copy link
Member

Lucas-C commented Dec 3, 2024

I opened PR #1315 to fix this

@Lucas-C Lucas-C self-assigned this Dec 3, 2024
@Lucas-C Lucas-C closed this as completed in 1f2b9e4 Dec 3, 2024
@Lucas-C
Copy link
Member

Lucas-C commented Dec 3, 2024

@kevinthenet: you can test the fix using the master branch of this repo

@kevinthenet
Copy link
Author

Just checked and can verify this issue is now fixed with the latest changes in maser. Thanks so much for the fix and the speedy response time! 🚀

@Lucas-C
Copy link
Member

Lucas-C commented Dec 16, 2024

This fixup has been included in the new release published today:
https://github.com/py-pdf/fpdf2/releases/tag/2.8.2

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