Search code examples
pythonthermal-printerescpos

Python send escpos command to thermal printer character size issue


I need to send escpos to a thermal receipt printer. I am running into issues with specifying character size, which is described [https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=34]. In Python I write this command as

#ESC @ for initiate the printer
string = b'\x1b\x40'
#GS ! command in the doc corresponding to 4 times character height and width
string = string + b'\x1d' + b'\x21' + b'\x30' + b'\x03' 
string = string + bytes('hello world')

In the first line I initiated the printer corresponding to ESC @ In the second line I wanted to specify the character size to be 4x height and width (see the links to the doc). In the third line I print out the text.

The problem is that the text printed out has 4x width, but not 4 times height. I have also tried to write the character size as two commands

string = string + b'\x1d' + b'\x21' + b'\x30'
string = string + b'\x1d' + b'\x21' + b'\x03' 

In this case, my text is printed out with 4 times height but not 4 times width. I am pretty sure that I have misread the doc, but I don't know how else I should write the command in order to achieve both 4 times height and width.

Also examples exist for the GS ! syntax in escpos, and there it seems to be written as GS ! 0x11 to achieve both 2 times width and height. This doesn't seems to make sense from the table. I am aware that python-escpos exists, however it doesn't work on windows 10 for my usb printer.


Solution

  • From reading the docs, it seems to me that you would have to use

    b'\x1d' + b'\x21' + b'\x33' 
    

    to get 4 times magnification in height as well as width. The two '3' indicate the magnifications minus one. The first is width, the second is height.

    So the problem seems to be that you split width and height into two bytes. They should be collected into one byte.

    So, in total:

    #ESC @ for initiate the printer
    string = b'\x1b\x40'
    
    #GS ! command in the doc corresponding to 4 times character height and width
    string = string + b'\x1d' + b'\x21' + b'\x33' 
    string = string + bytes('hello world')
    

    Or, in another way:

    def initialize():
        # Code for initialization of the printer.
        return b'\x1b\x40'
    
    def magnify(wm, hm):
        # Code for magnification of characters.
        # wm: Width magnification from 1 to 8. Normal width is 1, double is 2, etc.
        # hm: Height magnification from 1 to 8. Normal height is 1, double is 2, etc.
        return bytes([0x1d, 16*(wm-1) + (hm-1)])
    
    def text(t, encoding="ascii"):
        # Code for sending text.
        return bytes(t, encoding)
    
    string = initialize() + magnify(4, 4) + text('hello world')