Skip to content

add_bookmark doesn't set pagenum #968

@KingfuChan

Description

@KingfuChan

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.

Metadata

Metadata

Assignees

Labels

is-bugFrom a users perspective, this is a bug - a violation of the expected behavior with a compliant PDFworkflow-bookmarksFrom a users perspective, bookmarks is the affected feature/workflow

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions