Skip to content

Commit

Permalink
Proposed change to py-pdf#2144
Browse files Browse the repository at this point in the history
  • Loading branch information
marcstober committed Sep 7, 2023
1 parent 05f2a65 commit 5ae75af
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pypdf/_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ class PdfReader:

@property
def viewer_preferences(self) -> Optional[ViewerPreferences]:
"""Returns the existing ViewerPreferences as a overloaded dictionniary."""
"""Returns the existing ViewerPreferences as an overloaded dictionary."""
o = cast(DictionaryObject, self.trailer["/Root"]).get(
CD.VIEWER_PREFERENCES, None
)
Expand Down
6 changes: 3 additions & 3 deletions pypdf/_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,10 +371,10 @@ def set_need_appearances_writer(self, state: bool = True) -> None:

@property
def viewer_preferences(self) -> Optional[ViewerPreferences]:
"""Returns the existing ViewerPreferences as a overloaded dictionniary."""
"""Returns the ViewerPreferences as an overloaded dictionary."""
o = cast(DictionaryObject, self._root_object).get(CD.VIEWER_PREFERENCES, None)
if o is None:
return None
o = self._create_viewer_preferences()
o = o.get_object()
if not isinstance(o, ViewerPreferences):
o = ViewerPreferences(o)
Expand All @@ -384,7 +384,7 @@ def viewer_preferences(self) -> Optional[ViewerPreferences]:
self._root_object[NameObject(CD.VIEWER_PREFERENCES)] = o
return o

def create_viewer_preference(self) -> ViewerPreferences:
def _create_viewer_preferences(self) -> ViewerPreferences:
o = ViewerPreferences()
self._root_object[NameObject(CD.VIEWER_PREFERENCES)] = self._add_object(o)
return o
Expand Down
14 changes: 8 additions & 6 deletions tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1724,7 +1724,6 @@ def test_damaged_pdf_length_returning_none():
def test_viewerpreferences():
"""
Add Tests for ViewerPreferences
https://github.com/py-pdf/pypdf/issues/140#issuecomment-1685380549
"""
url = "https://github.com/py-pdf/pypdf/files/9175966/2015._pb_decode_pg0.pdf"
name = "2015._pb_decode_pg0.pdf"
Expand Down Expand Up @@ -1776,10 +1775,13 @@ def test_viewerpreferences():
v.print_pagerange = ArrayObject()
assert len(v.print_pagerange) == 0

writer.create_viewer_preference()
assert len(writer._root_object["/ViewerPreferences"]) == 0

del reader.trailer["/Root"]["/ViewerPreferences"]
assert reader.viewer_preferences is None
writer = PdfWriter(clone_from=reader)
assert writer.viewer_preferences is None
<<<<<<< HEAD
assert "/ViewerPreferences" not in writer._root_object
=======
assert not ("/ViewerPreferences" in writer._root_object)
>>>>>>> aef2c89 (fix unit tests)

writer.viewer_preferences.direction = "/R2L"
assert len(writer._root_object["/ViewerPreferences"]) == 1

0 comments on commit 5ae75af

Please sign in to comment.