Skip to content

Commit

Permalink
STY: Add PdfWriter._ID attribute
Browse files Browse the repository at this point in the history
See #2003

Co-authored-by: exiledkingcc <exiledkingcc@gmail.com>
  • Loading branch information
MartinThoma and exiledkingcc committed Dec 23, 2023
1 parent 85b5768 commit 44574a2
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions pypdf/_writer.py
Expand Up @@ -215,6 +215,7 @@ def __init__(

self._encryption: Optional[Encryption] = None
self._encrypt_entry: Optional[DictionaryObject] = None
self._ID: Union[ArrayObject, None] = None

def __enter__(self) -> "PdfWriter":
"""Store that writer is initialized by 'with'."""
Expand Down Expand Up @@ -1235,7 +1236,7 @@ def generate_file_identifiers(self) -> None:
The only point of this is ensuring uniqueness. Reproducibility is not
required; see 14.4 "File Identifiers".
"""
if hasattr(self, "_ID") and self._ID and len(self._ID) == 2:
if self._ID:
id1 = self._ID[0]
else:
id1 = self._compute_document_identifier_from_content()
Expand Down Expand Up @@ -1324,6 +1325,7 @@ def encrypt(
if not use_128bit:
alg = EncryptAlgorithm.RC4_40
self.generate_file_identifiers()
assert self._ID
self._encryption = Encryption.make(alg, permissions_flag, self._ID[0])
# in case call `encrypt` again
entry = self._encryption.write_entry(user_password, owner_password)
Expand Down Expand Up @@ -1425,7 +1427,7 @@ def _write_trailer(self, stream: StreamType, xref_location: int) -> None:
NameObject(TK.INFO): self._info,
}
)
if hasattr(self, "_ID"):
if self._ID:
trailer[NameObject(TK.ID)] = self._ID
if self._encrypt_entry:
trailer[NameObject(TK.ENCRYPT)] = self._encrypt_entry.indirect_reference
Expand Down

0 comments on commit 44574a2

Please sign in to comment.