diff --git a/src/escpos/escpos.py b/src/escpos/escpos.py index c238259..58bd81d 100644 --- a/src/escpos/escpos.py +++ b/src/escpos/escpos.py @@ -34,6 +34,7 @@ from .constants import CTL_VT, CTL_HT, CTL_CR, CTL_FF, CTL_LF, CTL_SET_HT, PANEL from .exceptions import BarcodeTypeError, BarcodeSizeError, TabPosError from .exceptions import CashDrawerError, SetVariableError, BarcodeCodeError +from .exceptions import ImageWidthError from .magicencode import MagicEncode @@ -99,6 +100,14 @@ class Escpos(object): """ im = EscposImage(img_source) + try: + max_width = 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 + 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..15c5ab6 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.