Skip to content

Commit

Permalink
Allow to set PDF identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
liZe committed May 19, 2022
1 parent d0be36b commit 879261c
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pydyf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import re
import zlib
from codecs import BOM_UTF16_BE
from hashlib import md5

VERSION = __version__ = '0.1.2'

Expand Down Expand Up @@ -469,12 +470,13 @@ def write_line(self, content, output):
self.current_position += len(content) + 1
output.write(content + b'\n')

def write(self, output, version=b'1.7'):
def write(self, output, version=b'1.7', identifier=None):
"""Write PDF to output.
:param output: Output stream.
:type output: binary :term:`file object`
:param bytes version: PDF version, default is 1.7.
:param bytes identifier: PDF file identifier.
"""
# Write header
Expand Down Expand Up @@ -503,6 +505,13 @@ def write(self, output, version=b'1.7'):
self.write_line(f'/Size {len(self.objects)}'.encode(), output)
self.write_line(b'/Root ' + self.catalog.reference, output)
self.write_line(b'/Info ' + self.info.reference, output)
if identifier is not None:
data = b''.join(
obj.data for obj in self.objects if obj.free != 'f')
data_hash = md5(data).hexdigest().encode()
self.write_line(
b'/ID [' + String(identifier).data + b' ' +
String(data_hash).data + b']', output)
self.write_line(b'>>', output)
self.write_line(b'startxref', output)
self.write_line(f'{self.xref_position}'.encode(), output)
Expand Down

0 comments on commit 879261c

Please sign in to comment.