diff --git a/src/escpos/escpos.py b/src/escpos/escpos.py index 42c7305..606a52c 100644 --- a/src/escpos/escpos.py +++ b/src/escpos/escpos.py @@ -34,6 +34,7 @@ from .constants import TXT_STYLE from .exceptions import BarcodeTypeError, BarcodeSizeError, TabPosError from .exceptions import CashDrawerError, SetVariableError, BarcodeCodeError +from .exceptions import ImageWidthError from .magicencode import MagicEncode @@ -99,6 +100,17 @@ class Escpos(object): """ im = EscposImage(img_source) + try: + max_width = int(self.profile.profile_data['media']['width']['pixels']) + if im.width > max_width: + raise ImageWidthError('{} > {}'.format(im.width, max_width)) + except KeyError: + # If the printer's pixel width is not known, print anyways... + pass + except ValueError: + # If the max_width cannot be converted to an int, print anyways... + pass + if im.height > fragment_height: fragments = im.split(fragment_height) for fragment in fragments: diff --git a/src/escpos/exceptions.py b/src/escpos/exceptions.py index 8c574ff..0662792 100644 --- a/src/escpos/exceptions.py +++ b/src/escpos/exceptions.py @@ -8,6 +8,7 @@ Result/Exit codes: - `20` = Barcode size values are out of range :py:exc:`~escpos.exceptions.BarcodeSizeError` - `30` = Barcode text not supplied :py:exc:`~escpos.exceptions.BarcodeCodeError` - `40` = Image height is too large :py:exc:`~escpos.exceptions.ImageSizeError` + - `41` = Image width is too large :py:exc:`~escpos.exceptions.ImageWidthError` - `50` = No string supplied to be printed :py:exc:`~escpos.exceptions.TextError` - `60` = Invalid pin to send Cash Drawer pulse :py:exc:`~escpos.exceptions.CashDrawerError` - `70` = Invalid number of tab positions :py:exc:`~escpos.exceptions.TabPosError` @@ -104,6 +105,20 @@ class ImageSizeError(Error): return "Image height is longer than 255px and can't be printed ({msg})".format(msg=self.msg) +class ImageWidthError(Error): + """ Image width is too large. + + The return code for this exception is `41`. + """ + def __init__(self, msg=""): + Error.__init__(self, msg) + self.msg = msg + self.resultcode = 41 + + def __str__(self): + return "Image width is too large ({msg})".format(msg=self.msg) + + class TextError(Error): """ Text string must be supplied to the `text()` method. diff --git a/test/test_function_image.py b/test/test_function_image.py index 27b4fd7..5aac41b 100644 --- a/test/test_function_image.py +++ b/test/test_function_image.py @@ -12,9 +12,13 @@ from __future__ import division from __future__ import print_function from __future__ import unicode_literals -import escpos.printer as printer +import pytest + from PIL import Image +import escpos.printer as printer +from escpos.exceptions import ImageWidthError + # Raster format print def test_bit_image_black(): @@ -139,3 +143,22 @@ def test_large_graphics(): instance = printer.Dummy() instance.image('test/resources/black_white.png', impl="bitImageRaster", fragment_height=1) assert(instance.output == b'\x1dv0\x00\x01\x00\x01\x00\xc0\x1dv0\x00\x01\x00\x01\x00\x00') + + +def test_width_too_large(): + """ + Test printing an image that is too large in width. + """ + instance = printer.Dummy() + instance.profile.profile_data = { + 'media': { + 'width': { + 'pixels': 384 + } + } + } + + with pytest.raises(ImageWidthError): + instance.image(Image.new("RGB", (385, 200))) + + instance.image(Image.new("RGB", (384, 200))) \ No newline at end of file