Skip to content

Commit

Permalink
Implemented PDF rotation degree
Browse files Browse the repository at this point in the history
  • Loading branch information
Federaffo authored and miikanissi committed May 16, 2024
1 parent 30daaf2 commit df7336f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ Additionally, **ZebrafyPDF** supports the following optional parameters:
+======================+==============================================================================================================+
| ``split_pages`` | Split the PDF into separate ZPL labels for each page (``True`` or ``False``, default ``False``) |
+----------------------+--------------------------------------------------------------------------------------------------------------+
| ``rotation`` | Rotates the PDF by the specified degree (``0``, ``90``, ``180`` or ``270``, default ``0``) |
+----------------------+--------------------------------------------------------------------------------------------------------------+



Getting Started
---------------
Expand Down
3 changes: 3 additions & 0 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ Additionally, **ZebrafyPDF** supports the following optional parameters:
+======================+==============================================================================================================+
| ``split_pages`` | Split the PDF into separate ZPL labels for each page (``True`` or ``False``, default ``False``) |
+----------------------+--------------------------------------------------------------------------------------------------------------+
| ``rotation`` | Rotates the PDF by the specified degree (``0``, ``90``, ``180`` or ``270``, default ``0``) |
+----------------------+--------------------------------------------------------------------------------------------------------------+


Conversions
-----------
Expand Down
27 changes: 26 additions & 1 deletion zebrafy/zebrafy_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class ZebrafyPDF:
:param split_pages: Split each PDF page as a new ZPL label \
(only applies if complete_zpl is set), defaults to ``False``
:param complete_zpl: Return a complete ZPL with header and footer included. \
:param rotation: Additional rotation in degrees ``0, 90, 180, or 270)`` \
Otherwise return only the graphic field, defaults to ``True``
.. deprecated:: 1.1.0
Expand All @@ -83,6 +84,7 @@ def __init__(
pos_y: int = None,
split_pages: bool = None,
complete_zpl: bool = None,
rotation: int = None,
):
self.pdf_bytes = pdf_bytes
if format is None:
Expand Down Expand Up @@ -118,6 +120,9 @@ def __init__(
if complete_zpl is None:
complete_zpl = True
self.complete_zpl = complete_zpl
if rotation is None:
rotation = 0
self.rotation = rotation

pdf_bytes = property(operator.attrgetter("_pdf_bytes"))

Expand Down Expand Up @@ -283,6 +288,26 @@ def complete_zpl(self, c):
)
self._complete_zpl = c

rotation = property(operator.attrgetter("_rotation"))

@rotation.setter
def rotation(self, r):
if r is None:
raise ValueError("Rotation cannot be empty.")
if not isinstance(r, int):
raise TypeError(
"Rotation must be an integer. {param_type} was given.".format(
param_type=type(r)
)
)
if r not in [0, 90, 180, 270]:
raise ValueError(
'Rotation must be "0", "90", "180" or "270". {param} was given.'.format(
param=r
)
)
self._rotation = r

def _compression_type_to_format(self, compression_type: str) -> str:
"""
Convert deprecated compression type to format.
Expand All @@ -305,7 +330,7 @@ def to_zpl(self) -> str:
pdf = PdfDocument(self._pdf_bytes)
graphic_fields = []
for page in pdf:
bitmap = page.render(scale=1, rotation=0)
bitmap = page.render(scale=1, rotation=self._rotation)
pil_image = bitmap.to_pil()
zebrafy_image = ZebrafyImage(
pil_image,
Expand Down

0 comments on commit df7336f

Please sign in to comment.