Skip to content

PdfWriter as context manager while passing existing file not working in 5.2.0 #3087

Open
@AlbertUnruh

Description

@AlbertUnruh

After upgrading pypdf from 5.1.0 to 5.2.0 my function to merge multiple pdfs didn't merge my files anymore.

I wasn't able to find anything in the change-log about any breaking changes.

Environment

Which environment were you using when you encountered the problem?

$ python -m platform
Windows-10-10.0.19045-SP0

$ python -c "import pypdf;print(pypdf._debug_versions)"  # 5.1.0
pypdf==5.1.0, crypt_provider=('local_crypt_fallback', '0.0.0'), PIL=11.1.0

$ python -c "import pypdf;print(pypdf._debug_versions)"  # 5.2.0
pypdf==5.2.0, crypt_provider=('local_crypt_fallback', '0.0.0'), PIL=11.1.0

Code + PDF

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

def merge_pdfs(*pdfs: Path, destination: Path):
    """Merge multiple pdfs into one big pdf at the given destination."""
    with PdfWriter(destination) as merger:
        merger.add_metadata(PdfReader(pdfs[0]).metadata)
        for pdf in pdfs:
            merger.append(pdf)

# merge_pdfs() works in 5.1.0 just fine
# merge_pdfs() doesn't work in 5.2.0 anymore

I don't think that any specific files are required.
I tested it with multiple files and it didn't work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    is-regressionRegression introduced as a side-effect of another changeworkflow-mergeFrom a users perspective, merging 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