Skip to content

Rotate PDF fails if /Rotate is set to an IndirectObject #337

@kgengler

Description

@kgengler

Some example page objects below:

This works because '/Rotate' is set to an integer value
{'/Contents': IndirectObject(5, 0), '/Rotate': 270, '/Type': '/Page', '/Resources': IndirectObject(6, 0), '/CropBox': [0, 0, 612, 792], '/Parent': IndirectObject(1, 0), '/MediaBox': [0, 0, 612, 792]}

This will work because '/Rotate' is missing and get will return the default 0 value
{'/Parent': IndirectObject(3, 0), '/Contents': IndirectObject(4, 0), '/Type': '/Page', '/Resources': IndirectObject(6, 0), '/MediaBox': [0, 0, 612, 792]}

But if you encounter a case where '/Rotate' gets set to an IndirectObject such below:
{'/Contents': IndirectObject(2, 0), '/Rotate': IndirectObject(12, 0), '/Type': '/Page', '/Resources': IndirectObject(3, 0), '/CropBox': [0, 0, 603, 751], '/Parent': IndirectObject(13, 0), '/MediaBox': [0, 0, 603, 751]}

PyPDF2 throws an exception when attempting to rotate the page.

Example Code:

pdf_reader = PyPDF2.PdfFileReader('broken.pdf')
for page_num in range(pdf_reader.numPages):
    page = pdf_reader.getPage(page_num)
    page.rotateClockwise(90)

Output:

Traceback (most recent call last):
  File "./main.py", line 26, in <module>
    page.rotateClockwise(90)
  File "~/Desktop/pypdftest/lib/python2.7/site-packages/PyPDF2/pdf.py", line 2150, in rotateClockwise
    self._rotate(angle)
  File "~/pypdftest/lib/python2.7/site-packages/PyPDF2/pdf.py", line 2166, in _rotate
    self[NameObject("/Rotate")] = NumberObject(currentAngle + angle)
TypeError: unsupported operand type(s) for +: 'IndirectObject' and 'int'

Metadata

Metadata

Assignees

No one assigned

    Labels

    is-bugFrom a users perspective, this is a bug - a violation of the expected behavior with a compliant PDFneeds-example-codeThe issue needs a minimal and complete (e.g. all imports) example showing the problemneeds-pdfThe issue needs a PDF file to show the problem

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions