question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Printer not parsing ESCPOS from python correctly

See original GitHub issue

I have:

  • searched open and closed issues for duplicates

Bug description

My printer doesn’t seem to be accepting the ESCPOS commands being sent by python-escpos in the correct manner. Don’t get me wrong, it seems to be having a valiant go at it, but it’s not making much headway. For instance, Epson.barcode('1324354657687','EAN13',64,2,'','') prints aw1324354657687 on the printer, and any of the .set() commands simply make the text tiny and occasionally miss some letters. It’s a bit of a weird one - any ideas?

Device info

Printer: Generic 58mm Thermal Reciept Printer POS-5890K set up on /dev/usb/lpt0

python-escpos version: 2.1.3

python version: 3.4.2

operating system: Raspbian Jessie Lite

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:15 (14 by maintainers)

github_iconTop GitHub Comments

2reactions
mashedkeyboardcommented, Aug 23, 2016

p.control("CR") doesn’t work either - for instance, the script

from escpos import *
p = printer.Usb(0x0416,0x5011)
p.text("Hello,")
p.control("CR")
p.text("Curtis")
p.cut()

actually outputted ello,tis exactly like that (no carriage return whatsoever).

And now my desk is full of tiny sheets of thermal paper!

0reactions
blutmecommented, Apr 3, 2020

Hey,

I’ve just ported an example from this project’s PHP sibling which includes a similar set of features (images, some basic formatting, and line breaks). Maybe you can start by printing this, then refactoring to match your expected output? The picture below is output from a ZJ 5890T.

Note that the command for “bold” that we send here appears to be ignored by this printer, but will work on other ESC/POS printers. I haven’t dug into it, but double-width is a good substitute that should get you on track.

Image:

2016-08-zj5890t-example

Code:

#!/usr/bin/env python
from escpos import printer

# A wrapper to organise item names & prices into columns
class item:
    def __init__(self, name="", price="", dollarSign=False):
        self.name=name
        self.price=price
        self.dollarSign=dollarSign

    def encode(self):
        rightCols = 6
        leftCols = 24
        if(self.dollarSign):
            leftCols = leftCols / 2 - rightCols / 2
        left = self.name.ljust(leftCols)
        sign = "$" if self.dollarSign else ""
        right = (sign + self.price).rjust(rightCols)
        return (left + right + "\n").encode()

# Information for the receipt
items = [
    item(name="Example item #1", price="4.00"),
    item(name="Another thing", price="3.50"),
    item(name="Something else", price="1.00"),
    item(name="A final item", price="4.45")
]
subtotal = item(name="Subtotal", price="12.95")
tax = item(name="A local tax", price="1.30");
total = item(name="Total", price="14.25", dollarSign=True);

# Date is kept the same for testing
date = "Monday 6th of April 2015 02:56:25 PM";

# Start the printer
p = printer.File("/dev/usb/lp0")

# Print top logo
p.set(align="center")
p.image("escpos-php.png", impl="bitImageColumn")

# Name of shop
p.set(align="center", width=2)
p.text("ExampleMart Ltd.\n");
p.set(align="center")
p.text("Shop No. 42.\n")
p.text("\n")

# Title of receipt
p.set(align="center", text_type="B")
p.text("SALES INVOICE\n");

# Items
p.set(align="left", text_type="B")
p.text(item(name="", price="$"));
p.set(align="left")
for item in items:
    p.text(item);
p.set(text_type="B")
p.text(subtotal);
p.text("\n")

# Tax and total
p.set()
p.text(tax);
p.set(width=2);
p.text(total);

# Footer
p.text("\n\n")
p.set(align="center")
p.text("Thank you for shopping at\nExampleMart.\n");
p.text("For trading hours, please visit example.com\n");
p.text("\n\n")
p.text(date + "\n");

# Cut the paper
p.cut()

Binary blob: receipt-with-logo-zj5890t-pythonescpos.zip

for python 3.7+ and 3nStar RPT008 THERMAL PRINTER:

you must add usb library:

from escpos.printer import Usb

and change the line 38 for this:

p = Usb(0x1fc9, 0x2016, 0) “Vendor ID” and “Product ID”, you found with: >lsusb

and 16 for this:

leftCols = leftCols / 2 - rightCols / 2 leftCols = int(leftCols)

Read more comments on GitHub >

github_iconTop Results From Across the Web

python-escpos QS Printer not printing - Stack Overflow
My PC is running on Windows 10, have Python 2.7 and python-escpos installed. I tried this but it's not working. >>>from escpos.printer import ......
Read more >
Printing with Python and Epson POS printer. | by Kakar Nyori
I am trying to print using a python package on windows, called python-escpos. This library works fine in both mac and Unix by...
Read more >
python-escpos Documentation - Read the Docs
Python ESC/POS is a library which lets the user have access to all those printers handled by ESC/POS commands, as.
Read more >
TODO — python-escpos 2.1.1 documentation
python -escpos is the initial idea, from here we can start to build a robust library to get most of the ESC/POS printers...
Read more >
Writing a printer 'driver' for a thermal printer - Lucas Vogel
So of course there was no driver for that. My next thought was to use an awesome library called python-escpos written in python....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found