mirror of
https://github.com/python-escpos/python-escpos
synced 2025-09-13 09:09:58 +00:00
Compare commits
22 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
7e3b6ce586 | ||
![]() |
4bb94c2662 | ||
![]() |
ef84a5150f | ||
![]() |
cd5969e843 | ||
![]() |
3f6528da07 | ||
![]() |
5eaa6f26d0 | ||
![]() |
37d7f34241 | ||
![]() |
280000d6ed | ||
![]() |
95082067e4 | ||
![]() |
f0f84e1215 | ||
![]() |
cae02976a3 | ||
![]() |
9550ad1068 | ||
![]() |
e623799fd7 | ||
![]() |
47aa4a96c9 | ||
![]() |
bb329b093b | ||
![]() |
d93e76e904 | ||
![]() |
f3933d5d20 | ||
![]() |
517435efad | ||
![]() |
122ff9a363 | ||
![]() |
708f7e97d1 | ||
![]() |
8a3850ea64 | ||
![]() |
34f562d64a |
21
CHANGELOG
21
CHANGELOG
@@ -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
66
README
@@ -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>
|
||||||
|
|
||||||
|
@@ -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%
|
||||||
|
@@ -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)
|
||||||
|
|
||||||
|
# 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")
|
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=''):
|
||||||
|
@@ -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
|
||||||
@@ -40,10 +40,18 @@ 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:
|
||||||
|
if check_driver is not None:
|
||||||
print "Could not detatch kernel driver: %s" % str(e)
|
print "Could not detatch kernel driver: %s" % str(e)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -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"
|
||||||
|
6
setup.py
6
setup.py
@@ -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(),
|
||||||
|
Reference in New Issue
Block a user