1
0
mirror of https://github.com/python-escpos/python-escpos synced 2025-09-13 09:09:58 +00:00

22 Commits

Author SHA1 Message Date
Manuel F Martinez
7e3b6ce586 Updated README and documentation 2015-10-27 13:45:15 -07:00
manpaz
4bb94c2662 Merge pull request #59 from zouppen/vertical-length-fix
Support for images vertically longer than 256 pixels
2015-10-27 13:36:21 -07:00
manpaz
ef84a5150f Merge pull request #61 from HarkonenBade/support-for-libusb0
Prevent crash when using libusb0 printers
2015-10-07 23:19:46 -07:00
Hark
cd5969e843 Prevent crash when using libusb0 printers 2015-09-04 22:48:58 +01:00
Joel Lehtonen
3f6528da07 Support for images vertically longer than 256 pixels 2015-08-25 12:17:11 +03:00
Manuel F Martinez
5eaa6f26d0 Added donation message 2015-08-22 13:52:06 -07:00
Manuel F Martinez
37d7f34241 Updated project version 2015-08-22 13:10:13 -07:00
Manuel F Martinez
280000d6ed Fixed issues with transparent images 2015-08-22 12:43:09 -07:00
manpaz
95082067e4 Merge pull request #53 from ldos/master
Extended params for serial printers
2015-07-06 14:44:17 -07:00
ldos
f0f84e1215 Extended params for serial printers
Update printer.Serial() constructor and its open() method to extend serial params.
Backwards compatible, no client modifications needed.
2015-07-04 17:23:31 +02:00
Manuel F Martinez
cae02976a3 Updated accordingly to the wiki 2015-06-30 15:09:08 -07:00
Manuel F Martinez
9550ad1068 Fixed License version mismatch 2015-06-10 16:28:27 -07:00
Manuel F Martinez
e623799fd7 Updated setup URLs 2015-06-04 15:55:14 -07:00
Manuel F Martinez
47aa4a96c9 Updated documentation URL to local wiki 2015-06-04 15:20:15 -07:00
Manuel F Martinez
bb329b093b Updated URL for the documentation 2015-06-04 14:44:02 -07:00
manpaz
d93e76e904 Merge pull request #45 from Krispy2009/master
Raising the right error when wrong charcode is used
2015-04-21 14:07:36 -07:00
Kristi
f3933d5d20 Merge pull request #1 from Krispy2009/Krispy2009-patch-1
Raising the right error when wrong charcode is used
2015-04-20 10:58:49 +01:00
Kristi
517435efad Raising the right error when wrong charcode is used 2015-04-20 10:58:04 +01:00
Manuel F Martinez
122ff9a363 Updated version 2014-05-20 23:51:39 -07:00
Manuel F Martinez
708f7e97d1 fixed code tabulators 2014-05-20 23:50:06 -07:00
Manuel F Martinez
8a3850ea64 Added quad support 2014-05-20 22:43:40 -07:00
Manuel F Martinez
34f562d64a Added density support 2014-05-20 22:31:49 -07:00
6 changed files with 115 additions and 74 deletions

View File

@@ -18,5 +18,26 @@ CHANGELOG
- Added exception for PIL import - Added exception for PIL import
* 2014-05-20 - Version 1.0.4 * 2014-05-20 - Version 1.0.4
- Issue #20: Added Density support (Sent by thomas.erbacher@ragapack.de)
- Added charcode tables - Added charcode tables
- Fixed Horizontal Tab - Fixed Horizontal Tab
- Fixed code tabulators
* 2015-04-21 - Version 1.0.5
- Merge pull request #45 from Krispy2009/master
. Raising the right error when wrong charcode is used
. Sent by Kristi <Krispy2009@gmail.com>
* 2015-07-06 - Version 1.0.6
- Merge pull request #53 from ldos/master
. Extended params for serial printers
. Sent by ldos <cafeteria.ldosalzira@gmail.com>
* 2015-08-22 - Version 1.0.7
- Issue #57: Fixed transparent images
* 2015-10-27 - Version 1.0.8
- Merge pull request #59 from zouppen/master
. Support for images vertically longer than 256 pixels
. Sent by Joel Lehtonen <joel.lehtonen@koodilehto.fi>
- Updated README

66
README
View File

@@ -4,16 +4,7 @@ ESCPOS
Python library to manipulate ESC/POS Printers. Python library to manipulate ESC/POS Printers.
------------------------------------------------------------------ ------------------------------------------------------------------
1. Dependencies 1. Description
In order to start getting access to your printer, you must ensure
you have previously installed the following python modules:
* pyusb (python-usb)
* PIL (Python Image Library)
------------------------------------------------------------------
2. Description
Python ESC/POS is a library which lets the user have access to all Python ESC/POS is a library which lets the user have access to all
those printers handled by ESC/POS commands, as defined by Epson, those printers handled by ESC/POS commands, as defined by Epson,
@@ -33,57 +24,22 @@ paper, carrier return, printer reset and others concerned to the
carriage alignment. carriage alignment.
------------------------------------------------------------------ ------------------------------------------------------------------
3. Define your printer 2. Documentation
Before start create your Python ESC/POS printer instance, you must Please visit project documentation at:
see at your system for the printer parameters. This is done with https://github.com/manpaz/python-escpos/wiki
the 'lsusb' command.
First run the command to look for the "Vendor ID" and "Product ID",
then write down the values, these values are displayed just before
the name of the device with the following format:
xxxx:xxxx
Example:
Bus 002 Device 001: ID 1a2b:1a2b Device name
Write down the the values in question, then issue the following
command so you can get the "Interface" number and "End Point"
lsusb -vvv -d xxxx:xxxx | grep iInterface
lsusb -vvv -d xxxx:xxxx | grep bEndpointAddress | grep OUT
The first command will yields the "Interface" number that must
be handy to have and the second yields the "Output Endpoint"
address.
By default the "Interface" number is "0" and the "Output Endpoint"
address is "0x82", if you have other values then you can define
with your instance.
------------------------------------------------------------------ ------------------------------------------------------------------
4. Define your instance 3. Donations
The following example shows how to initialize the Epson TM-TI88IV There are some different prints I'd like to acquire, but unfortunately
*** NOTE: Always finish the sequence with Epson.cut() otherwise not all, even used, are cheaper and easy to get.
you will endup with weird chars being printed.
from escpos import * If you want to help funding money to get more printers or just want to
donate because you like the project, please be in touch and I'll be
sending my PayPal info so you can donate.
""" Seiko Epson Corp. Receipt Printer M129 Definitions (EPSON TM-T88IV) """ Thank you!
Epson = escpos.Escpos(0x04b8,0x0202,0)
Epson.text("Hello World")
Epson.image("logo.gif")
Epson.barcode
Epson.barcode('1324354657687','EAN13',64,2,'','')
Epson.cut()
------------------------------------------------------------------
5. Links
Please visit project homepage at:
http://repo.bashlinux.com/projects/escpos.html
Manuel F Martinez <manpaz@bashlinux.com> Manuel F Martinez <manpaz@bashlinux.com>

View File

@@ -21,6 +21,7 @@ PAPER_PART_CUT = '\x1d\x56\x01' # Partial cut paper
TXT_NORMAL = '\x1b\x21\x00' # Normal text TXT_NORMAL = '\x1b\x21\x00' # Normal text
TXT_2HEIGHT = '\x1b\x21\x10' # Double height text TXT_2HEIGHT = '\x1b\x21\x10' # Double height text
TXT_2WIDTH = '\x1b\x21\x20' # Double width text TXT_2WIDTH = '\x1b\x21\x20' # Double width text
TXT_4SQUARE = '\x1b\x21\x30' # Quad area text
TXT_UNDERL_OFF = '\x1b\x2d\x00' # Underline font OFF TXT_UNDERL_OFF = '\x1b\x2d\x00' # Underline font OFF
TXT_UNDERL_ON = '\x1b\x2d\x01' # Underline font 1-dot ON TXT_UNDERL_ON = '\x1b\x2d\x01' # Underline font 1-dot ON
TXT_UNDERL2_ON = '\x1b\x2d\x02' # Underline font 2-dot ON TXT_UNDERL2_ON = '\x1b\x2d\x02' # Underline font 2-dot ON
@@ -73,3 +74,13 @@ S_RASTER_N = '\x1d\x76\x30\x00' # Set raster image normal size
S_RASTER_2W = '\x1d\x76\x30\x01' # Set raster image double width S_RASTER_2W = '\x1d\x76\x30\x01' # Set raster image double width
S_RASTER_2H = '\x1d\x76\x30\x02' # Set raster image double height S_RASTER_2H = '\x1d\x76\x30\x02' # Set raster image double height
S_RASTER_Q = '\x1d\x76\x30\x03' # Set raster image quadruple S_RASTER_Q = '\x1d\x76\x30\x03' # Set raster image quadruple
# Printing Density
PD_N50 = '\x1d\x7c\x00' # Printing Density -50%
PD_N37 = '\x1d\x7c\x01' # Printing Density -37.5%
PD_N25 = '\x1d\x7c\x02' # Printing Density -25%
PD_N12 = '\x1d\x7c\x03' # Printing Density -12.5%
PD_0 = '\x1d\x7c\x04' # Printing Density 0%
PD_P50 = '\x1d\x7c\x08' # Printing Density +50%
PD_P37 = '\x1d\x7c\x07' # Printing Density +37.5%
PD_P25 = '\x1d\x7c\x06' # Printing Density +25%
PD_P12 = '\x1d\x7c\x05' # Printing Density +12.5%

View File

@@ -3,7 +3,7 @@
@author: Manuel F Martinez <manpaz@bashlinux.com> @author: Manuel F Martinez <manpaz@bashlinux.com>
@organization: Bashlinux @organization: Bashlinux
@copyright: Copyright (c) 2012 Bashlinux @copyright: Copyright (c) 2012 Bashlinux
@license: GPL @license: GNU GPL v3
""" """
try: try:
@@ -41,7 +41,7 @@ class Escpos:
buffer = "" buffer = ""
self._raw(S_RASTER_N) self._raw(S_RASTER_N)
buffer = "%02X%02X%02X%02X" % (((size[0]/size[1])/8), 0, size[1], 0) buffer = "%02X%02X%02X%02X" % (((size[0]/size[1])/8), 0, size[1]&0xff, size[1]>>8)
self._raw(buffer.decode('hex')) self._raw(buffer.decode('hex'))
buffer = "" buffer = ""
@@ -68,7 +68,7 @@ class Escpos:
if im.size[0] > 512: if im.size[0] > 512:
print ("WARNING: Image is wider than 512 and could be truncated at print time ") print ("WARNING: Image is wider than 512 and could be truncated at print time ")
if im.size[1] > 255: if im.size[1] > 0xffff:
raise ImageSizeError() raise ImageSizeError()
im_border = self._check_image_size(im.size[0]) im_border = self._check_image_size(im.size[0])
@@ -107,7 +107,15 @@ class Escpos:
def image(self,path_img): def image(self,path_img):
""" Open image file """ """ Open image file """
im_open = Image.open(path_img) im_open = Image.open(path_img)
im = im_open.convert("RGB")
# Remove the alpha channel on transparent images
if im_open.mode == 'RGBA':
im_open.load()
im = Image.new("RGB", im_open.size, (255, 255, 255))
im.paste(im_open, mask=im_open.split()[3])
else:
im = im_open.convert("RGB")
# Convert the RGB image in printable image # Convert the RGB image in printable image
self._convert_image(im) self._convert_image(im)
@@ -119,6 +127,7 @@ class Escpos:
qr_code.make(fit=True) qr_code.make(fit=True)
qr_img = qr_code.make_image() qr_img = qr_code.make_image()
im = qr_img._img.convert("RGB") im = qr_img._img.convert("RGB")
# Convert the RGB image in printable image # Convert the RGB image in printable image
self._convert_image(im) self._convert_image(im)
@@ -168,7 +177,7 @@ class Escpos:
elif code.upper() == "THAI18": elif code.upper() == "THAI18":
self._raw(CHARCODE_THAI18) self._raw(CHARCODE_THAI18)
else: else:
raise CharCode_error() raise CharCodeError()
def barcode(self, code, bc, width, height, pos, font): def barcode(self, code, bc, width, height, pos, font):
""" Print Barcode """ """ Print Barcode """
@@ -230,12 +239,12 @@ class Escpos:
raise TextError() raise TextError()
def set(self, align='left', font='a', type='normal', width=1, height=1): def set(self, align='left', font='a', type='normal', width=1, height=1, density=9):
""" Set text properties """ """ Set text properties """
# Width # Width
if height == 2 and width == 2: if height == 2 and width == 2:
self._raw(TXT_2WIDTH) self._raw(TXT_NORMAL)
self._raw(TXT_2HEIGHT) self._raw(TXT_4SQUARE)
elif height == 2 and width != 2: elif height == 2 and width != 2:
self._raw(TXT_NORMAL) self._raw(TXT_NORMAL)
self._raw(TXT_2HEIGHT) self._raw(TXT_2HEIGHT)
@@ -254,7 +263,6 @@ class Escpos:
elif type.upper() == "U2": elif type.upper() == "U2":
self._raw(TXT_BOLD_OFF) self._raw(TXT_BOLD_OFF)
self._raw(TXT_UNDERL2_ON) self._raw(TXT_UNDERL2_ON)
self._raw(TXT_ITALIC_OFF)
elif type.upper() == "BU": elif type.upper() == "BU":
self._raw(TXT_BOLD_ON) self._raw(TXT_BOLD_ON)
self._raw(TXT_UNDERL_ON) self._raw(TXT_UNDERL_ON)
@@ -276,6 +284,27 @@ class Escpos:
self._raw(TXT_ALIGN_RT) self._raw(TXT_ALIGN_RT)
elif align.upper() == "LEFT": elif align.upper() == "LEFT":
self._raw(TXT_ALIGN_LT) self._raw(TXT_ALIGN_LT)
# Density
if density == 0:
self._raw(PD_N50)
elif density == 1:
self._raw(PD_N37)
elif density == 2:
self._raw(PD_N25)
elif density == 3:
self._raw(PD_N12)
elif density == 4:
self._raw(PD_0)
elif density == 5:
self._raw(PD_P12)
elif density == 6:
self._raw(PD_P25)
elif density == 7:
self._raw(PD_P37)
elif density == 8:
self._raw(PD_P50)
else:# DEFAULT: DOES NOTHING
pass
def cut(self, mode=''): def cut(self, mode=''):

View File

@@ -3,7 +3,7 @@
@author: Manuel F Martinez <manpaz@bashlinux.com> @author: Manuel F Martinez <manpaz@bashlinux.com>
@organization: Bashlinux @organization: Bashlinux
@copyright: Copyright (c) 2012 Bashlinux @copyright: Copyright (c) 2012 Bashlinux
@license: GPL @license: GNU GPL v3
""" """
import usb.core import usb.core
@@ -31,7 +31,7 @@ class Usb(Escpos):
self.interface = interface self.interface = interface
self.in_ep = in_ep self.in_ep = in_ep
self.out_ep = out_ep self.out_ep = out_ep
self.open() self.open()
def open(self): def open(self):
@@ -40,11 +40,19 @@ class Usb(Escpos):
if self.device is None: if self.device is None:
print "Cable isn't plugged in" print "Cable isn't plugged in"
if self.device.is_kernel_driver_active(0): check_driver = None
try:
check_driver = self.device.is_kernel_driver_active(0)
except NotImplementedError:
pass
if check_driver is None or check_driver:
try: try:
self.device.detach_kernel_driver(0) self.device.detach_kernel_driver(0)
except usb.core.USBError as e: except usb.core.USBError as e:
print "Could not detatch kernel driver: %s" % str(e) if check_driver is not None:
print "Could not detatch kernel driver: %s" % str(e)
try: try:
self.device.set_configuration() self.device.set_configuration()
@@ -69,23 +77,39 @@ class Usb(Escpos):
class Serial(Escpos): class Serial(Escpos):
""" Define Serial printer """ """ Define Serial printer """
def __init__(self, devfile="/dev/ttyS0", baudrate=9600, bytesize=8, timeout=1): def __init__(self, devfile="/dev/ttyS0", baudrate=9600, bytesize=8, timeout=1,
parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE,
xonxoff=False , dsrdtr=True):
""" """
@param devfile : Device file under dev filesystem @param devfile : Device file under dev filesystem
@param baudrate : Baud rate for serial transmission @param baudrate : Baud rate for serial transmission
@param bytesize : Serial buffer size @param bytesize : Serial buffer size
@param timeout : Read/Write timeout @param timeout : Read/Write timeout
@param parity : Parity checking
@param stopbits : Number of stop bits
@param xonxoff : Software flow control
@param dsrdtr : Hardware flow control (False to enable RTS/CTS)
""" """
self.devfile = devfile self.devfile = devfile
self.baudrate = baudrate self.baudrate = baudrate
self.bytesize = bytesize self.bytesize = bytesize
self.timeout = timeout self.timeout = timeout
self.parity = parity
self.stopbits = stopbits
self.xonxoff = xonxoff
self.dsrdtr = dsrdtr
self.open() self.open()
def open(self): def open(self):
""" Setup serial port and set is as escpos device """ """ Setup serial port and set is as escpos device """
self.device = serial.Serial(port=self.devfile, baudrate=self.baudrate, bytesize=self.bytesize, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, timeout=self.timeout, dsrdtr=True) self.device = serial.Serial(port=self.devfile, baudrate=self.baudrate,
bytesize=self.bytesize, parity=self.parity,
stopbits=self.stopbits, timeout=self.timeout,
xonxoff=self.xonxoff, dsrdtr=self.dsrdtr)
if self.device is not None: if self.device is not None:
print "Serial printer enabled" print "Serial printer enabled"
@@ -151,7 +175,7 @@ class File(Escpos):
def open(self): def open(self):
""" Open system file """ """ Open system file """
self.device = open(self.devfile, "wb") self.device = open(self.devfile, "wb")
if self.device is None: if self.device is None:
print "Could not open the specified file %s" % self.devfile print "Could not open the specified file %s" % self.devfile

View File

@@ -4,9 +4,9 @@ from distutils.core import setup
setup( setup(
name='escpos', name='escpos',
version='1.0.4', version='1.0.8',
url='http://code.google.com/p/python-escpos', url='https://github.com/manpaz/python-escpos',
download_url='http://python-escpos.googlecode.com/files/python-escpos-1.0.zip', download_url='https://github.com/manpaz/python-escpos.git',
description='Python library to manipulate ESC/POS Printers', description='Python library to manipulate ESC/POS Printers',
license='GNU GPL v3', license='GNU GPL v3',
long_description=open('README').read(), long_description=open('README').read(),