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

Collapsing outlines not working: parameter is_open in add_outline_item has no effect #2994

Open
dowo-2987 opened this issue Dec 6, 2024 · 0 comments
Labels
PdfWriter The PdfWriter component is affected workflow-annotation Everything about annotating PDF files

Comments

@dowo-2987
Copy link

dowo-2987 commented Dec 6, 2024

I tried adding a (nested) outline to a document, and I wanted it to be collapsed by default when opening the file. So I set is_open to False in writer.add_outline_item, but the outlines are still all expanded.

I looked a bit into the code, and from my understanding the problem lies in writer.add_outline_destination when calling parent.insert_child:
When giving the inc_parent_counter, a default inc_parent_counter_outline is passed if is_open == True, and some lambda returning 0 at all times otherwise. However it seems to be exactly the inc_parent_counter_outline that handles setting "/Count" to -1 if the outlines should be collapsed. So I removed the if statement (so it always passes inc_parent_counter_outline), and now my outlines are collapsed. So this seems to fix it, however I have no idea whether that breaks anything else that doesn't affect my usecase.

In case it helps:
Change

parent.insert_child(
            page_destination_ref,
            before,
            self,
            page_destination.inc_parent_counter_outline
            if is_open
            else (lambda x, y: 0),
        )

to

parent.insert_child(
            page_destination_ref,
            before,
            self,
            page_destination.inc_parent_counter_outline,
        )

Environment

python 3.10.12 in WSL

$ python3 -m platform
# Linux-5.15.167.4-microsoft-standard-WSL2-x86_64-with-glibc2.35

$ python3 -c "import pypdf;print(pypdf._debug_versions)"
# pypdf==5.1.0, crypt_provider=('cryptography', '43.0.3'), PIL=10.3.0

Code + PDF

This is a minimal (I hope), complete example that shows the issue:

import pypdf

pdf_in = "test.pdf"
pdf_out = "output.pdf"
reader = pypdf.PdfReader(pdf_in)
writer = pypdf.PdfWriter()

# Copy the pdf
for page in reader.pages:
    writer.add_page(page)

# Create outline
parent = writer.add_outline_item("Parent Item", 0, is_open=False)
writer.add_outline_item("Child Item", 0, parent=parent, is_open=False)

# Write the new file
with open(pdf_out, "wb") as f:
        writer.write(f)

Any PDF should produce this issue, however I append a random minimal pdf that does produce the issue: test.pdf

Traceback

There is no traceback.

@dowo-2987 dowo-2987 changed the title is_open in add_outline_item doesn't do anything when set to False Collapsing outlines not working: parameter is_open in add_outline_item has no effect Dec 6, 2024
@stefan6419846 stefan6419846 added PdfWriter The PdfWriter component is affected workflow-annotation Everything about annotating PDF files labels Dec 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PdfWriter The PdfWriter component is affected workflow-annotation Everything about annotating PDF files
Projects
None yet
Development

No branches or pull requests

2 participants