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

add_bookmark doesn't set pagenum #968

Closed
KingfuChan opened this issue Jun 10, 2022 · 7 comments · Fixed by #1016
Closed

add_bookmark doesn't set pagenum #968

KingfuChan opened this issue Jun 10, 2022 · 7 comments · Fixed by #1016
Assignees
Labels
is-bug From a users perspective, this is a bug - a violation of the expected behavior with a compliant PDF workflow-bookmarks From a users perspective, bookmarks is the affected feature/workflow

Comments

@KingfuChan
Copy link

Replace this: Using add_bookmark in PdfMerger, the title is fine but none of the page numbers are applied to output PDF.

Environment

$ python -m platform
# Windows-10-10.0.22000-SP0

$ python -c "import PyPDF2;print(PyPDF2.__version__)"
# 2.1.0

Code

This is a minimal, complete example that shows the issue:

            merger = PdfMerger()
            prevgrp = ""
            pagenum = 1
            bmrkinfo = {}
            for label, url in adc_full[icao].items():
                currgrp = get_group_name(label) # string
                pdffile = download_pdf(url, tempfolder, label) # returns a PdfReader
                merger.append(pdffile)
                if prevgrp != currgrp:
                    bmrkinfo[currgrp] = {}
                    prevgrp = currgrp
                lbl = label.split(':')[-1]
                bmrkinfo[currgrp][lbl] = pagenum
                pagenum += len(pdffile.pages)

            for g, pdfgroup in bmrkinfo.items():
                pgrp = list(pdfgroup.values())[0]
                currmrk = merger.add_bookmark(g, pgrp)
                if len(pdfgroup) > 1:
                    for l, p in pdfgroup.items():
                        merger.add_bookmark(l, p, currmrk)

            merger.write(os.path.join(OUTPUT_DIR, icao+'.pdf'))

PDF

Sorry I can't share the PDF file itself.

@KingfuChan KingfuChan added the is-bug From a users perspective, this is a bug - a violation of the expected behavior with a compliant PDF label Jun 10, 2022
@MartinThoma
Copy link
Member

Thank you for reporting the issue. I could confirm it.

So far, I have no idea why this issue occurs

@austinwarnock
Copy link

austinwarnock commented Jun 20, 2022

EDITED: Updated for clarity

Also running into the same issue with the following code:

    title_page_stream = io.BytesIO()
    table_of_contents_stream = io.BytesIO()
    calculation_name = 'My Calculation'
    
    merger = PdfMerger(strict=False)
    merger.append(title_page_stream)

    root_bookmark = merger.add_bookmark(calculation_name,0)


    merger.append(table_of_contents_stream)
    table_of_contents_bookmark = merger.add_bookmark("Table of Contents", 1, root_bookmark)

@MartinThoma
Copy link
Member

@austinwarnock Examples help best if they are complete and minimal:

  • Complete means that everything, including imports and data, is in the example. People should be able to copy-paste it to a file and execute it.
  • Minimal means, that there is no extra stuff around. In your example, upload_file_to_blob is for sure not necessary to show the issue.

@austinwarnock
Copy link

UPDATE: Work around found

By reverting to 1.27.0 we were able to get the bookmarks to work again. Any newer versions did not work.

@MartinThoma
Copy link
Member

Huh, interesting! Thank you for the feedback!

@MartinThoma MartinThoma added the workflow-bookmarks From a users perspective, bookmarks is the affected feature/workflow label Jun 21, 2022
@MartinThoma
Copy link
Member

Looking at the changes, I doubt it: 1.27.0...1.27.1

However, if I remember correctly the first git tag was set wrong (it doesn't match the PyPI release). I'll make a test and a git bisect.

@MartinThoma
Copy link
Member

1.27.9 introduced the issue with 07848e5
1.27.8 should work.

I verified it with:

from PyPDF2 import PdfFileMerger as PdfMerger

merger = PdfMerger()
merger.append("crazyones.pdf")

bookmark = merger.addBookmark("A bookmark", 0)
bm2 = merger.addBookmark("deeper", 1, parent=bookmark)

merger.write("issue968.pdf")
merger.close()

MartinThoma added a commit that referenced this issue Jun 21, 2022
MartinThoma added a commit that referenced this issue Jun 21, 2022
MartinThoma added a commit that referenced this issue Jun 23, 2022
Remove code duplication

Closes #968 (introduced with #339)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
is-bug From a users perspective, this is a bug - a violation of the expected behavior with a compliant PDF workflow-bookmarks From a users perspective, bookmarks is the affected feature/workflow
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants