Skip to content

Change in behavior from 3.10.0 to 3.11.0 #1953

@ArkieCoder

Description

@ArkieCoder

The minimal example below works fine with pypdf 3.10.0, but fails beginning with 3.11.0. The expectation is that the code would work in 3.11.0, or else a deprecation message would be displayed.

Environment

Python 3.11.4

Code + PDF

No PDF is generated due to the error.

from io import BytesIO
from pathlib import Path

from pypdf import PdfReader, PdfWriter, Transformation

LETTER_W = 612
LETTER_H = 792


def create_song_sheet(input_file: Path, output_file: Path) -> None:
    pdf_pages = []
    reader = PdfReader(input_file)
    pdf_page_count = len(reader.pages)

    for page in reader.pages:
        new_letter_page_writer = PdfWriter()

        ## scale the PDF to fit a 8.5 x 11 inch US Letter page
        ## but preserve the aspect ratio by setting the height
        ## to 11, scaling the width accordingly, and centering
        ## the result on the page
        new_letter_page = new_letter_page_writer.add_blank_page(
            width=LETTER_W, height=LETTER_H
        )

        ## set the mediabox width and height so that things don't
        ## get chopped off when we center the scaled PDF
        source_width = page.mediabox.width
        source_height = page.mediabox.height
        source_aspect = source_width / source_height
        new_width = LETTER_H * source_aspect
        width_offset = (LETTER_W - new_width) / 2

        new_letter_page.merge_transformed_page(
            page, Transformation((1, 0, 0, 1, width_offset, 0))
        )

        tmp_pdf = BytesIO()
        new_letter_page_writer.write(tmp_pdf)

        new_letter_page_writer.close()
        pdf_pages.append(
            {"file": tmp_pdf, "name": input_file, "pagecount": pdf_page_count}
        )   

    merger = PdfWriter()
    for pdf_page in pdf_pages:
        ## ERROR OCCURS HERE ON PROCESSING THE FIRST PAGE ##
        merger.append(pdf_page["file"])

    merger.write(output_file)
    merger.close()


create_song_sheet(
    Path("/home/moose/Github/py-pdf/pypdf/resources/crazyones.pdf"),
    Path("out-1953.pdf"),
)

Traceback

Here is the traceback from the file above

Traceback (most recent call last):
  File "/home/moose/Github/py-pdf/pypdf/issue-1953.py", line 55, in <module>
    create_song_sheet(
  File "/home/moose/Github/py-pdf/pypdf/issue-1953.py", line 49, in create_song_sheet
    merger.append(pdf_page["file"])
  File "/home/moose/Github/py-pdf/pypdf/pypdf/_writer.py", line 2768, in append
    self.merge(
  File "/home/moose/Github/py-pdf/pypdf/pypdf/_utils.py", line 432, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/moose/Github/py-pdf/pypdf/pypdf/_writer.py", line 2849, in merge
    srcpages[pg.indirect_reference.idnum] = self.add_page(
                                            ^^^^^^^^^^^^^^
  File "/home/moose/Github/py-pdf/pypdf/pypdf/_writer.py", line 389, in add_page
    return self._add_page(page, list.append, excluded_keys)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/moose/Github/py-pdf/pypdf/pypdf/_writer.py", line 322, in _add_page
    page = cast("PageObject", page_org.clone(self, False, excluded_keys))
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/moose/Github/py-pdf/pypdf/pypdf/generic/_data_structures.py", line 186, in clone
    d__._clone(self, pdf_dest, force_duplicate, ignore_fields)
  File "/home/moose/Github/py-pdf/pypdf/pypdf/generic/_data_structures.py", line 286, in _clone
    v.clone(pdf_dest, force_duplicate, ignore_fields)
  File "/home/moose/Github/py-pdf/pypdf/pypdf/generic/_base.py", line 294, in clone
    obj.clone(pdf_dest, force_duplicate, ignore_fields)
  File "/home/moose/Github/py-pdf/pypdf/pypdf/generic/_data_structures.py", line 102, in clone
    arr.append(dup.indirect_reference)
               ^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'DecodedStreamObject' object has no attribute 'indirect_reference'

Metadata

Metadata

Assignees

No one assigned

    Labels

    Has MCVEA minimal, complete and verifiable example helps a lot to debug / understand feature requestsis-bugFrom a users perspective, this is a bug - a violation of the expected behavior with a compliant PDF

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions