Added ImageWidthError and its implementation

This commit is contained in:
Romain Porte 2017-05-22 21:00:35 +02:00
parent df33945458
commit a8ab2da3cf
2 changed files with 24 additions and 0 deletions

View File

@ -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 BarcodeTypeError, BarcodeSizeError, TabPosError
from .exceptions import CashDrawerError, SetVariableError, BarcodeCodeError from .exceptions import CashDrawerError, SetVariableError, BarcodeCodeError
from .exceptions import ImageWidthError
from .magicencode import MagicEncode from .magicencode import MagicEncode
@ -99,6 +100,14 @@ class Escpos(object):
""" """
im = EscposImage(img_source) 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: if im.height > fragment_height:
fragments = im.split(fragment_height) fragments = im.split(fragment_height)
for fragment in fragments: for fragment in fragments:

View File

@ -8,6 +8,7 @@ Result/Exit codes:
- `20` = Barcode size values are out of range :py:exc:`~escpos.exceptions.BarcodeSizeError` - `20` = Barcode size values are out of range :py:exc:`~escpos.exceptions.BarcodeSizeError`
- `30` = Barcode text not supplied :py:exc:`~escpos.exceptions.BarcodeCodeError` - `30` = Barcode text not supplied :py:exc:`~escpos.exceptions.BarcodeCodeError`
- `40` = Image height is too large :py:exc:`~escpos.exceptions.ImageSizeError` - `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` - `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` - `60` = Invalid pin to send Cash Drawer pulse :py:exc:`~escpos.exceptions.CashDrawerError`
- `70` = Invalid number of tab positions :py:exc:`~escpos.exceptions.TabPosError` - `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) 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): class TextError(Error):
""" Text string must be supplied to the `text()` method. """ Text string must be supplied to the `text()` method.