Search code examples
python-3.xprintingthermal-printerescpos

ESC/POS printing barcode aligned vertically and horizontally in the middle?


My code is currently printing a barcode and hri text easily, but I want everything to be printed dead centre on the label. Can someone please tell me when I should declare the alignment command?
I tried declaring it at the beginning and just before the barcode print command, but the barcode is still printing in the top left corner.
Here is my align command:

    def align_esc(self,esc,n):
        '''
        ESC|a|n
        0<=n<=2,48<=n<=50
        0,48 = Left; 1,49 = Mid; 2,50 = Right
        '''
        align = [esc,n]
        align_prnt = bytearray(align)
        return align

The reason I am not using escpos python library is because I am printing from an Android device.
Edit:
The above function is missing the following character on line 7:

align = [esc,97,n]

Solution

  • In the ESC a command description of EPSON, there are the following notes.
    Are these notices on the ESC a command for your printer?

    • When Standard mode is selected, this command is enabled only when processed at the beginning of the line in Standard mode.
    • The justification has no effect in Page mode.
    • This command executes justification in the print area set by GS L and GS W.
    • This command justifies printing data (such as characters, all graphics, barcodes, and two-dimensional code) and space area set by HT, ESC $, and ESC \.
    • Settings of this command are effective until ESC @ is executed, the printer is reset, or the power is turned off.

    Often, the ESC a command is specified not after the beginning of the line but after printing a few characters or spaces.

    For example, if you print with plain text instead of a barcode and it is not centered, then ESC a is not specified at the beginning of the line.
    Change the ESC a command so that it is specified at the beginning of the line.

    However, if the plain text is printed centered, the feature may not work for barcodes.
    In that case, contact the printer vendor to determine if barcode alignment is available and how to do so.
    Or there is no automatic function, it may be a method to adjust the print position with ESC \ etc.


    The print position in the horizontal direction can be specified by either ESC $ or ESC \.
    Only the starting point of the calculation is different. ESC $ is the left edge of the paper, and ESC \ is the print position at that time.
    You won't need to use GS P.