From 8ff5078de9c13e8a844f13b7b0e9d33d4231a00c Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 23 Sep 2022 19:03:40 +0200 Subject: Merging upstream version 0.3.0. Signed-off-by: Daniel Baumann --- docs/changelog.rst | 36 +++++++++++++++++++++++++++++++++++- pydyf/__init__.py | 33 ++++++++++++++++++++++++++++++--- 2 files changed, 65 insertions(+), 4 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 5243edc..76f5813 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -2,6 +2,40 @@ Changelog ========= +Version 0.3.0 +------------- + +Released on 2022-09-19. + +New features: + +* Support marked content +* Allow version and ID to be specified when initializing PDF objects + +Contributors: + +* Guillaume Ayoub + +Backers and sponsors: + +* Grip Angebotssoftware +* Manuel Barkhau +* Crisp BV +* SimonSoft +* Menutech +* Spacinov +* KontextWork +* René Fritz +* NCC Group +* Kobalt +* Tom Pohl +* John R Ellis +* Moritz Mahringer +* Gábor +* Piotr Horzycki +* Andrew Ittner + + Version 0.2.0 ------------- @@ -11,7 +45,7 @@ Dependencies: * Python 3.7+ is now needed, Python 3.6 is not supported anymore - New features: +New features: * `d0be36b `_: Allow to set PDF version diff --git a/pydyf/__init__.py b/pydyf/__init__.py index a627a13..fac9ecc 100755 --- a/pydyf/__init__.py +++ b/pydyf/__init__.py @@ -8,7 +8,7 @@ import zlib from codecs import BOM_UTF16_BE from hashlib import md5 -VERSION = __version__ = '0.2.0' +VERSION = __version__ = '0.3.0' def _to_bytes(item): @@ -97,6 +97,15 @@ class Stream(Object): #: Compress the stream data if set to ``True``. Default is ``False``. self.compress = compress + def begin_marked_content(self, tag, property_list=None): + """Begin marked-content sequence.""" + self.stream.append(f'/{tag}') + if property_list is None: + self.stream.append(b'BMC') + else: + self.stream.append(property_list) + self.stream.append(b'BDC') + def begin_text(self): """Begin a text object.""" self.stream.append(b'BT') @@ -172,6 +181,10 @@ class Stream(Object): """End path without filling or stroking.""" self.stream.append(b'n') + def end_marked_content(self): + """End marked-content sequence.""" + self.stream.append(b'EMC') + def end_text(self): """End text object.""" self.stream.append(b'ET') @@ -409,7 +422,18 @@ class Array(Object, list): class PDF: """PDF document.""" - def __init__(self): + def __init__(self, version=b'1.7', identifier=None): + """Create a PDF document. + + :param bytes version: PDF version. + :param bytes identifier: PDF file identifier. + + """ + #: PDF version, as :obj:`bytes`. + self.version = _to_bytes(version) + #: PDF file identifier. + self.identifier = identifier + #: Python :obj:`list` containing the PDF’s objects. self.objects = [] @@ -470,7 +494,7 @@ class PDF: self.current_position += len(content) + 1 output.write(content + b'\n') - def write(self, output, version=b'1.7', identifier=None): + def write(self, output, version=None, identifier=None): """Write PDF to output. :param output: Output stream. @@ -479,6 +503,9 @@ class PDF: :param bytes identifier: PDF file identifier. """ + version = self.version if version is None else _to_bytes(version) + identifier = self.identifier if identifier is None else identifier + # Write header self.write_line(b'%PDF-' + version, output) self.write_line(b'%\xf0\x9f\x96\xa4', output) -- cgit v1.2.3