Skip to content

Update Merge with Rotation example in documentation to avoid deprecation warning #3668

@trawick

Description

@trawick

I am receiving the deprecation warning below in my code. Looking in the documentation, I found an example that essentially matches my code, and it receives the same deprecation warning. It would be great if the documentation were updated to show how to avoid that warning.

            deprecate(
                "Calling `PageObject.replace_contents()` for pages not assigned to a writer is deprecated "
                "and will be removed in pypdf 7.0.0. Attach the page to the writer first or use "
                "`PdfWriter(clone_from=...)` directly. The existing approach has proved being unreliable."
            )

Environment

Which environment were you using when you encountered the problem?

$ python -m platform
# Linux-5.15.0-171-generic-x86_64-with-glibc2.35

$ python -c "import pypdf;print(pypdf._debug_versions)"
# pypdf==6.7.5, crypt_provider=('cryptography', '46.0.5'), PIL=12.1.1

Code + PDF

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

Here is my run of the example, substituting my own PDF files:

>>> reader_base = PdfReader(pdf1)
Ignoring wrong pointing object 27 0 (offset 0)
Ignoring wrong pointing object 73 0 (offset 0)
Ignoring wrong pointing object 120 0 (offset 0)
Ignoring wrong pointing object 170 0 (offset 0)
>>> page_base = reader_base.pages[0]
>>> reader = PdfReader(pdf2)
Ignoring wrong pointing object 27 0 (offset 0)
Ignoring wrong pointing object 76 0 (offset 0)
Ignoring wrong pointing object 127 0 (offset 0)
>>> page_box = reader.pages[0]
>>> transformation = Transformation().rotate(45)
>>> page_box.add_transformation(transformation)
>>> page_base.merge_page(page_box)
/path/to/virtualenv/lib/python3.10/site-packages/pypdf/_page.py:1180: DeprecationWarning: Calling `PageObject.replace_contents()` for pages not assigned to a writer is deprecated and will be removed in pypdf 7.0.0. Attach the page to the writer first or use `PdfWriter(clone_from=...)` directly. The existing approach has proved being unreliable.
  self.replace_contents(ContentStream(new_content_array, self.pdf))

Share here the PDF file(s) that cause the issue.

AFAIK you don't need my PDF files, as the deprecation is related to the use of the API.

Traceback

N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    nf-documentationNon-functional change: Documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions