Skip to content

Commit

Permalink
[IMP] split_pages refactored logic, tests, docs
Browse files Browse the repository at this point in the history
Add documentation and tests for split_pages attribute.
Refactored logic.
  • Loading branch information
miikanissi committed May 12, 2024
1 parent 333eee5 commit 5ad91e9
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 29 deletions.
8 changes: 8 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ If you want more control over the resulting ZPL data, **ZebrafyImage** and
| ``complete_zpl`` | Add ZPL header and footer or only get the ZPL graphic field output (``True`` or ``False``, default ``True``) |
+----------------------+--------------------------------------------------------------------------------------------------------------+

Additionally, **ZebrafyPDF** supports the following optional parameters:

+----------------------+--------------------------------------------------------------------------------------------------------------+
| Parameter | Description |
+======================+==============================================================================================================+
| ``split_pages`` | Split the PDF into separate ZPL labels for each page (``True`` or ``False``, default ``False``) |
+----------------------+--------------------------------------------------------------------------------------------------------------+

Getting Started
---------------

Expand Down
8 changes: 8 additions & 0 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ ZebrafyPDF and ZebrafyImage Parameters
| ``complete_zpl`` | Add ZPL header and footer or only get the ZPL graphic field output (``True`` or ``False``, default ``True``) |
+----------------------+--------------------------------------------------------------------------------------------------------------+

Additionally, **ZebrafyPDF** supports the following optional parameters:

+----------------------+--------------------------------------------------------------------------------------------------------------+
| Parameter | Description |
+======================+==============================================================================================================+
| ``split_pages`` | Split the PDF into separate ZPL labels for each page (``True`` or ``False``, default ``False``) |
+----------------------+--------------------------------------------------------------------------------------------------------------+

Conversions
-----------

Expand Down
6 changes: 6 additions & 0 deletions tests/static/test_pdf_split_pages.zpl

Large diffs are not rendered by default.

18 changes: 17 additions & 1 deletion tests/test_zebrafy.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,15 @@ def test_zebrafy_pdf_complete_zpl(self):
with self.assertRaises(TypeError):
zebrafy_pdf.complete_zpl = "123"

def test_zebrafy_pdf_split_pages(self):
"""Test ZebrafyPDF split pages"""
pdf = self._read_static_file("test_pdf.pdf")
zebrafy_pdf = ZebrafyPDF(pdf)
with self.assertRaises(ValueError):
zebrafy_pdf.split_pages = None
with self.assertRaises(TypeError):
zebrafy_pdf.split_pages = "123"

def test_pdf_to_default_zpl(self):
"""Test PDF to ZPL with default options."""
default_zpl = ZebrafyPDF(self._read_static_file("test_pdf.pdf")).to_zpl()
Expand Down Expand Up @@ -450,12 +459,19 @@ def test_pdf_to_zpl_threshold_high(self):
self.assertEqual(gf_zpl, self._read_static_file("test_pdf_high_threshold.zpl"))

def test_pdf_to_zpl_width_height(self):
"""Test PDF to ZPL without dithering the PDF and high threshold."""
"""Test PDF to ZPL with set width and height."""
gf_zpl = ZebrafyPDF(
self._read_static_file("test_pdf.pdf"), width=720, height=1280
).to_zpl()
self.assertEqual(gf_zpl, self._read_static_file("test_pdf_width_height.zpl"))

def test_pdf_to_zpl_split_pages(self):
"""Test PDF to ZPL with split pages."""
gf_zpl = ZebrafyPDF(
self._read_static_file("test_pdf.pdf"), split_pages=True
).to_zpl()
self.assertEqual(gf_zpl, self._read_static_file("test_pdf_split_pages.zpl"))

##################
# ZebrafyZPL Tests
##################
Expand Down
60 changes: 32 additions & 28 deletions zebrafy/zebrafy_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ class ZebrafyPDF:
PDF height, defaults to ``0``
:param pos_x: X position of the PDF on the resulting ZPL, defaults to ``0``
:param pos_y: Y position of the PDF on the resulting ZPL, defaults to ``0``
: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. \
Otherwise return only the graphic field, defaults to ``True``
Expand All @@ -79,8 +81,8 @@ def __init__(
height: int = None,
pos_x: int = None,
pos_y: int = None,
split_pages: bool = None,
complete_zpl: bool = None,
split_pages : bool = None,
):
self.pdf_bytes = pdf_bytes
if format is None:
Expand Down Expand Up @@ -110,12 +112,12 @@ def __init__(
if pos_y is None:
pos_y = 0
self.pos_y = pos_y
if complete_zpl is None:
complete_zpl = True
self.complete_zpl = complete_zpl
if split_pages is None:
split_pages = False
self.split_pages = split_pages
if complete_zpl is None:
complete_zpl = True
self.complete_zpl = complete_zpl

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

Expand Down Expand Up @@ -253,36 +255,33 @@ def pos_y(self, y):
)
self._pos_y = y

complete_zpl = property(operator.attrgetter("_complete_zpl"))

@complete_zpl.setter
def complete_zpl(self, c):
if c is None:
raise ValueError("Complete ZPL cannot be empty.")
if not isinstance(c, bool):
raise TypeError(
"Complete ZPL must be a boolean. {param_type} was given.".format(
param_type=type(c)
)
)
self._complete_zpl = c

split_pages = property(operator.attrgetter("_split_pages"))

@split_pages.setter
def split_pages(self, s):
if s is None:
raise ValueError("Split_pages cannot be empty.")
raise ValueError("Split pages cannot be empty.")
if not isinstance(s, bool):
raise TypeError(
"Invert must be a boolean. {param_type} was given.".format(
"Split pages must be a boolean. {param_type} was given.".format(
param_type=type(s)
)
)
self._split_pages = s


complete_zpl = property(operator.attrgetter("_complete_zpl"))

@complete_zpl.setter
def complete_zpl(self, c):
if c is None:
raise ValueError("Complete ZPL cannot be empty.")
if not isinstance(c, bool):
raise TypeError(
"Complete ZPL must be a boolean. {param_type} was given.".format(
param_type=type(c)
)
)
self._complete_zpl = c

def _compression_type_to_format(self, compression_type: str) -> str:
"""
Expand All @@ -304,7 +303,7 @@ def to_zpl(self) -> str:
"""
# Open and convert image to grayscale
pdf = PdfDocument(self._pdf_bytes)
graphic_fields = ""
graphic_fields = []
for page in pdf:
bitmap = page.render(scale=1, rotation=0)
pil_image = bitmap.to_pil()
Expand All @@ -323,11 +322,16 @@ def to_zpl(self) -> str:

page_zpl = zebrafy_image.to_zpl() + "\n"

if self._complete_zpl and self._split_pages:
graphic_fields += "^XA\n" + page_zpl + "^XZ\n"
graphic_fields.append(page_zpl)

zpl_string = ""
if self._complete_zpl:
if self._split_pages:
for graphic_field in graphic_fields:
zpl_string += "^XA\n" + graphic_field + "^XZ\n"
else:
graphic_fields += page_zpl
zpl_string = "^XA\n" + "".join(graphic_fields) + "^XZ\n"
else:
zpl_string = "".join(graphic_fields)

if self._complete_zpl and not self._split_pages:
return "^XA\n" + graphic_fields + "^XZ\n"
return graphic_fields
return zpl_string

0 comments on commit 5ad91e9

Please sign in to comment.