Merging upstream version 0.2.0.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
53c7599f12
commit
fe219838ef
13 changed files with 159 additions and 116 deletions
|
@ -6,8 +6,9 @@ A low-level PDF generator.
|
|||
import re
|
||||
import zlib
|
||||
from codecs import BOM_UTF16_BE
|
||||
from hashlib import md5
|
||||
|
||||
VERSION = __version__ = '0.1.2'
|
||||
VERSION = __version__ = '0.2.0'
|
||||
|
||||
|
||||
def _to_bytes(item):
|
||||
|
@ -425,7 +426,7 @@ class PDF:
|
|||
})
|
||||
self.add_object(self.pages)
|
||||
|
||||
#: PDF :class:`Dictionary` containing the PDF’s metadata.
|
||||
#: PDF :class:`Dictionary` containing the PDF’s metadata.
|
||||
self.info = Dictionary({})
|
||||
self.add_object(self.info)
|
||||
|
||||
|
@ -469,15 +470,17 @@ class PDF:
|
|||
self.current_position += len(content) + 1
|
||||
output.write(content + b'\n')
|
||||
|
||||
def write(self, output):
|
||||
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.
|
||||
:param bytes identifier: PDF file identifier.
|
||||
|
||||
"""
|
||||
# Write header
|
||||
self.write_line(b'%PDF-1.7', output)
|
||||
self.write_line(b'%PDF-' + version, output)
|
||||
self.write_line(b'%\xf0\x9f\x96\xa4', output)
|
||||
|
||||
# Write all non-free PDF objects
|
||||
|
@ -502,6 +505,13 @@ class PDF:
|
|||
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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue