From cc9f49521d2525ae86815364f77e400eccac998c Mon Sep 17 00:00:00 2001 From: Guillaume Ayoub Date: Wed, 11 Feb 2026 08:33:34 +0100 Subject: [PATCH 1/3] Explicitly allow bytes for PDF Strings --- pydyf/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pydyf/__init__.py b/pydyf/__init__.py index fca2c82..9eb08ae 100755 --- a/pydyf/__init__.py +++ b/pydyf/__init__.py @@ -416,7 +416,7 @@ class String(Object): """PDF String object.""" def __init__(self, string=''): super().__init__() - #: Unicode string. + #: Unicode string or encoded bytestring. self.string = string @property From a1a6135269b3df4abd9c3974db23913aa458f07b Mon Sep 17 00:00:00 2001 From: Guillaume Ayoub Date: Wed, 11 Feb 2026 08:47:21 +0100 Subject: [PATCH 2/3] Improve and clean typing information in docstrings --- pydyf/__init__.py | 60 +++++++++++++++++------------------------------ 1 file changed, 21 insertions(+), 39 deletions(-) diff --git a/pydyf/__init__.py b/pydyf/__init__.py index 9eb08ae..c16e591 100755 --- a/pydyf/__init__.py +++ b/pydyf/__init__.py @@ -80,11 +80,11 @@ class Stream(Object): """PDF Stream object.""" def __init__(self, stream=None, extra=None, compress=False): super().__init__() - #: Python array of data composing stream. + #: Python array of serializable data composing stream. self.stream = stream or [] #: Metadata containing at least the length of the Stream. self.extra = extra or {} - #: Compress the stream data if set to ``True``. Default is ``False``. + #: Compress the stream data if set to :obj:`True`. Default is :obj:`False`. self.compress = compress def begin_marked_content(self, tag, property_list=None): @@ -197,15 +197,11 @@ def fill_stroke_and_close(self, even_odd=False): def inline_image(self, width, height, color_space, bpc, raw_data): """Add an inline image. - :param width: The width of the image. - :type width: :obj:`int` - :param height: The height of the image. - :type height: :obj:`int` - :param colorspace: The color space of the image, f.e. RGB, Gray. - :type colorspace: :obj:`str` - :param bpc: The bits per component. 1 for BW, 8 for grayscale. - :type bpc: :obj:`int` - :param raw_data: The raw pixel data. + :param int width: The width of the image. + :param int height: The height of the image. + :param str colorspace: The color space of the image, f.e. RGB, Gray. + :param int bpc: The bits per component. 1 for BW, 8 for grayscale. + :param bytes raw_data: The raw pixel data. """ data = zlib.compress(raw_data) if self.compress else raw_data @@ -297,8 +293,7 @@ def set_dash(self, dash_array, dash_phase): :param dash_array: Dash pattern. :type dash_array: :term:`iterable` - :param dash_phase: Start of dash phase. - :type dash_phase: :obj:`int` + :param int dash_phase: Start of dash phase. """ self.stream.append(b' '.join(( @@ -332,18 +327,12 @@ def set_line_width(self, width): def set_matrix(self, a, b, c, d, e, f): """Set current transformation matrix. - :param a: Top left number in the matrix. - :type a: :obj:`int` or :obj:`float` - :param b: Top middle number in the matrix. - :type b: :obj:`int` or :obj:`float` - :param c: Middle left number in the matrix. - :type c: :obj:`int` or :obj:`float` - :param d: Middle middle number in the matrix. - :type d: :obj:`int` or :obj:`float` - :param e: Bottom left number in the matrix. - :type e: :obj:`int` or :obj:`float` - :param f: Bottom middle number in the matrix. - :type f: :obj:`int` or :obj:`float` + :param float a: Top left number in the matrix. + :param float b: Top middle number in the matrix. + :param float c: Middle left number in the matrix. + :param float d: Middle middle number in the matrix. + :param float e: Bottom left number in the matrix. + :param float f: Bottom middle number in the matrix. """ self.stream.append(b' '.join(( @@ -365,18 +354,12 @@ def set_state(self, state_name): def set_text_matrix(self, a, b, c, d, e, f): """Set current text and text line transformation matrix. - :param a: Top left number in the matrix. - :type a: :obj:`int` or :obj:`float` - :param b: Top middle number in the matrix. - :type b: :obj:`int` or :obj:`float` - :param c: Middle left number in the matrix. - :type c: :obj:`int` or :obj:`float` - :param d: Middle middle number in the matrix. - :type d: :obj:`int` or :obj:`float` - :param e: Bottom left number in the matrix. - :type e: :obj:`int` or :obj:`float` - :param f: Bottom middle number in the matrix. - :type f: :obj:`int` or :obj:`float` + :param float a: Top left number in the matrix. + :param float b: Top middle number in the matrix. + :param float c: Middle left number in the matrix. + :param float d: Middle middle number in the matrix. + :param float e: Bottom left number in the matrix. + :param float f: Bottom middle number in the matrix. """ self.stream.append(b' '.join(( @@ -505,8 +488,7 @@ def page_references(self): def write_line(self, content, output): """Write line to output. - :param content: Content to write. - :type content: :obj:`bytes` + :param bytes content: Content to write. :param output: Output stream. :type output: binary :term:`file object` From 8e82a5f635409959acb675d6d1259119a6b1a5ad Mon Sep 17 00:00:00 2001 From: Guillaume Ayoub Date: Wed, 11 Feb 2026 08:56:54 +0100 Subject: [PATCH 3/3] Use clearer variable names when creating object index --- pydyf/__init__.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/pydyf/__init__.py b/pydyf/__init__.py index c16e591..74dcdda 100755 --- a/pydyf/__init__.py +++ b/pydyf/__init__.py @@ -535,19 +535,21 @@ def write(self, output, version=b'1.7', identifier=False, compress=False): self.write_line(object_.indirect, output) # Write compressed objects in object stream - stream = [[]] + stream = [] + object_index_list = [] position = 0 for i, object_ in enumerate(compressed_objects): data = object_.data stream.append(data) - stream[0].append(object_.number) - stream[0].append(position) + object_index_list.append(object_.number) + object_index_list.append(position) position += len(data) + 1 - stream[0] = ' '.join(str(i) for i in stream[0]) + object_index_string = ' '.join(str(i) for i in object_index_list) + stream.insert(0, object_index_string) extra = { 'Type': '/ObjStm', 'N': len(compressed_objects), - 'First': len(stream[0]) + 1, + 'First': len(object_index_string) + 1, } object_stream = Stream(stream, extra, compress) object_stream.offset = self.current_position