Skip to content

Commit 44574a2

Browse files
STY: Add PdfWriter._ID attribute
See #2003 Co-authored-by: exiledkingcc <exiledkingcc@gmail.com>
1 parent 85b5768 commit 44574a2

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

pypdf/_writer.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ def __init__(
215215

216216
self._encryption: Optional[Encryption] = None
217217
self._encrypt_entry: Optional[DictionaryObject] = None
218+
self._ID: Union[ArrayObject, None] = None
218219

219220
def __enter__(self) -> "PdfWriter":
220221
"""Store that writer is initialized by 'with'."""
@@ -1235,7 +1236,7 @@ def generate_file_identifiers(self) -> None:
12351236
The only point of this is ensuring uniqueness. Reproducibility is not
12361237
required; see 14.4 "File Identifiers".
12371238
"""
1238-
if hasattr(self, "_ID") and self._ID and len(self._ID) == 2:
1239+
if self._ID:
12391240
id1 = self._ID[0]
12401241
else:
12411242
id1 = self._compute_document_identifier_from_content()
@@ -1324,6 +1325,7 @@ def encrypt(
13241325
if not use_128bit:
13251326
alg = EncryptAlgorithm.RC4_40
13261327
self.generate_file_identifiers()
1328+
assert self._ID
13271329
self._encryption = Encryption.make(alg, permissions_flag, self._ID[0])
13281330
# in case call `encrypt` again
13291331
entry = self._encryption.write_entry(user_password, owner_password)
@@ -1425,7 +1427,7 @@ def _write_trailer(self, stream: StreamType, xref_location: int) -> None:
14251427
NameObject(TK.INFO): self._info,
14261428
}
14271429
)
1428-
if hasattr(self, "_ID"):
1430+
if self._ID:
14291431
trailer[NameObject(TK.ID)] = self._ID
14301432
if self._encrypt_entry:
14311433
trailer[NameObject(TK.ENCRYPT)] = self._encrypt_entry.indirect_reference

0 commit comments

Comments
 (0)