Skip to content

PdfWriter add_page adds (re)writes old page instead of new #135062

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

Closed
DominiquePACCO opened this issue Jun 3, 2025 · 3 comments
Closed

PdfWriter add_page adds (re)writes old page instead of new #135062

DominiquePACCO opened this issue Jun 3, 2025 · 3 comments
Labels
type-bug An unexpected behavior, bug, or error

Comments

@DominiquePACCO
Copy link

DominiquePACCO commented Jun 3, 2025

Bug report

Bug description:

The purpose of the following code (as a simplification) is to add PDF pages alternatively from 2 different opencv images.
To make the sequence unique, a sequence number is written to each PDF page saved. The PDF is written every 2 images but this is not essential
Herewith I expect a resulting PDF file with 60 pages numbered #1, #2, #3, ... #59, #60.

However the resulting PDF file shows a sequence which in my case starts erratically from the 25th page:
#1, #2, #3, #4, #5, #6, #7, #8, #9, #10, #11, #12, #13, #14, #15, #16, #17, #18, #19, #20, #21, #22, #23, #24, #20, #22, #18, #19, #29, #30.
#31, #32, #33, #34, #35, #36, #37, #38, #39, #40, #36, #37, #34, #35, #32, #33, #30, #31, #19, #29, #21, #18, #24, #20, #55, #23, #57, #23, #59, #20

#25, #26, #27, #28 are missing an have been substituted by #20, #21, #18, #19.
Then everything is back on track to #40
In the remainder of the sequence #55, #57 and #59 are on the right page. The rest shows an 'older' page

The import of PyPDF2 is version 3.0.1

pythonSeq.pdf

    os.remove("pythonSeq.pdf")

pdf_writer = PdfWriter()

image1 = cv2.imread("Picture1.png")
image2 = cv2.imread("Picture2.png")
for seq in range(1,61,2):
    clone = image1.copy()
    cv2.putText(clone,f'Seq#{seq}', (200,30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 3)
    cv2.imwrite("python.jpg",clone)
    pdf_image = Image.open("python.jpg")
    pdf_image.save("python.pdf", format="PDF")
    image_reader = PdfReader("python.pdf")
    pdf_writer.add_page(image_reader.pages[0])

    clone = image2.copy()
    cv2.putText(clone,f'Seq#{seq+1}', (200,30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 3)
    cv2.imwrite("python.jpg",clone)
    pdf_image = Image.open("python.jpg")
    pdf_image.save("python.pdf", format="PDF")
    image_reader = PdfReader("python.pdf")
    pdf_writer.add_page(image_reader.pages[0])

[pythonSeq.pdf](https://github.com/user-attachments/files/20565446/pythonSeq.pdf)

    with open("pythonSeq.pdf", "wb") as pdf_file:
        pdf_writer.write(pdf_file)```


### CPython versions tested on:

3.13

### Operating systems tested on:

Windows
@DominiquePACCO DominiquePACCO added the type-bug An unexpected behavior, bug, or error label Jun 3, 2025
@mdboom
Copy link
Contributor

mdboom commented Jun 3, 2025

Is there a bug here in CPython itself, or is it more likely in one of the libraries you are using?

@DominiquePACCO
Copy link
Author

It's likely a bug in the PyPDF2 package, in the PdfWriter module

@mdboom
Copy link
Contributor

mdboom commented Jun 3, 2025

Thanks. I'm going to close this, then, and imagine you will have better luck filing this bug with that project.

@mdboom mdboom closed this as completed Jun 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants