Skip to content
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

ENH: Add Enforce from PDF2.0 in viewer_preferences #2511

Merged
merged 7 commits into from
Mar 11, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
ENH: Add Enforce from PDF2.0 in viewer_preferences
closes #2509
in order to use it:
```
#to set it:
    writer.viewer_preferences.enforce += writer.viewer_preferences.PRINTINGSCALING
#to clear it:
    writer.viewer_preferences.enforce = None
```
  • Loading branch information
pubpub-zz committed Mar 10, 2024

Verified

This commit was signed with the committer’s verified signature.
bhaoo Bhao
commit 294ef15a811c51024231b7d8e4326c206b9ba864
12 changes: 12 additions & 0 deletions pypdf/generic/_viewerpref.py
Original file line number Diff line number Diff line change
@@ -59,6 +59,12 @@ def _get_arr(self, key: str, deft: Optional[List[Any]]) -> NumberObject:
return self.get(key, None if deft is None else ArrayObject(deft))

def _set_arr(self, key: str, v: Optional[ArrayObject]) -> None:
if v is None:
try:
del self[NameObject(key)]
except KeyError:
pass
return
if not isinstance(v, ArrayObject):
raise ValueError("ArrayObject is expected")
self[NameObject(key)] = v
@@ -69,6 +75,10 @@ def _get_int(self, key: str, deft: Optional[NumberObject]) -> NumberObject:
def _set_int(self, key: str, v: int) -> None:
self[NameObject(key)] = NumberObject(v)

@property
def PRINTINGSCALING(self) -> NameObject:
pubpub-zz marked this conversation as resolved.
Show resolved Hide resolved
stefan6419846 marked this conversation as resolved.
Show resolved Hide resolved
return NameObject("/PrintScaling")

def __new__(cls: Any, value: Any = None) -> "ViewerPreferences":
def _add_prop_bool(key: str, deft: Optional[BooleanObject]) -> property:
return property(
@@ -140,6 +150,8 @@ def _add_prop_int(key: str, deft: Optional[int]) -> property:
cls.print_pagerange = _add_prop_arr("/PrintPageRange", None)
cls.num_copies = _add_prop_int("/NumCopies", None)

cls.enforce = _add_prop_arr("/Enforce", ArrayObject())

return DictionaryObject.__new__(cls)

def __init__(self, obj: Optional[DictionaryObject] = None) -> None:
8 changes: 8 additions & 0 deletions tests/test_writer.py
Original file line number Diff line number Diff line change
@@ -1807,6 +1807,14 @@ def test_viewerpreferences():
writer.viewer_preferences.direction = "/R2L"
assert len(writer.root_object["/ViewerPreferences"]) == 1

assert writer.viewer_preferences.enforce == []
assert "/Enforce" not in writer.viewer_preferences
writer.viewer_preferences.enforce += writer.viewer_preferences.PRINTINGSCALING
stefan6419846 marked this conversation as resolved.
Show resolved Hide resolved
assert writer.viewer_preferences["/Enforce"] == ["/PrintScaling"]
writer.viewer_preferences.enforce = None
assert "/Enforce" not in writer.viewer_preferences
writer.viewer_preferences.enforce = None

del reader.trailer["/Root"]["/ViewerPreferences"]
assert reader.viewer_preferences is None
writer = PdfWriter(clone_from=reader)
Loading