Skip to content

Reusing PdfMerger after write generates PDF with extra pages #1337

@bashirmindee

Description

@bashirmindee

I was trying to merge the same PDF with itself multiple number of times. 1st I want to have the original PDF, then I want to have the PDF duplicated, then I want to duplicate it three times, and so forth.

Environment

ubuntu 20.04
Python==3.8.12+
Package Version


pip==21.1.1
PyPDF2==2.10.5
setuptools==56.0.0
typing-extensions==4.3.0

$ python -m platform
Linux-5.11.0-40-generic-x86_64-with-glibc2.29
$ python -c "import PyPDF2;print(PyPDF2.__version__)"
2.10.5

Code + PDF

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

##script.py

from PyPDF2 import PdfReader, PdfMerger

merger = PdfMerger()
reader = PdfReader("blank.pdf")

for j in range(9):
    merger.append(reader)
    merger.write(f"generated_pdfs/{len(merger.pages)}.pdf")

Here is the blank.pdf that causes the issue.

Expected behavior

1.pdf: must contain 1 page but contains 1 page ✅
2.pdf: must contain 2 page but contains 2 page ❌
3.pdf: must contain 3 page but contains 3 page ❌
4.pdf: must contain 4 page but contains 4 page ❌
5.pdf: must contain 5 page but contains 5 page ❌
6.pdf: must contain 6 page but contains 6 page ❌

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedWe appreciate help everywhere - this one might be an easy start!is-bugFrom a users perspective, this is a bug - a violation of the expected behavior with a compliant PDFworkflow-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