Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.

public class EscPosPrinter

  1. Object
  2. EscPosPrinter

A receipt printer that speaks ESC/POS – Epson, Star in ESC/POS mode, Bixolon, Xprinter and most thermal printers – over the network (port 9100), a serial line or USB.

Calls build a job in memory; print() sends it.

EscPosPrinter p = new EscPosPrinter(new TcpLink("192.168.1.60", 9100));
p.align(EscPosPrinter.CENTER).bold(true).size(2, 2).line("CAFE")
 .size(1, 1).bold(false).align(EscPosPrinter.LEFT)
 .line("Latte          3.50")
 .qr("https://example.com/r/1042")
 .feed(3).cut().openDrawer();
p.print().except(err -> Dialog.show("Printer", err.getMessage(), "OK", null));

Text goes out one byte per character in the printer’s current code page (code page 0, PC437, after reset()); characters above 255 print as ?.

Fields

public static final int LEFT = 0Left alignment, for align(int).
public static final int CENTER = 1Centered, for align(int).
public static final int RIGHT = 2Right alignment, for align(int).

Constructors

public EscPosPrinter(ByteLink link)

Methods

public final EscPosPrinter reset()Restores the printer’s defaults (ESC @).
public EscPosPrinter text(String text)Appends text without a line break.
public EscPosPrinter line(String text)Appends a line of text.
public EscPosPrinter bold(boolean on)Bold on or off (ESC E).
public EscPosPrinter underline(boolean on)Underline on or off (ESC -).
public EscPosPrinter align(int alignment)Alignment of the following lines (ESC a).
public EscPosPrinter size(int width, int height)Character size as multiples of normal (GS !).
public EscPosPrinter feed(int lines)Feeds blank lines (ESC d).
public EscPosPrinter cut()Cuts the paper, partially where the printer supports it (GS V 1).
public EscPosPrinter openDrawer()Kicks the cash drawer on the printer’s drawer port (ESC p), pin 2.
public EscPosPrinter openDrawer(int pin)Kicks a cash drawer (ESC p).
public EscPosPrinter qr(String data)A QR code (GS ( k, model 2, error correction M).
public EscPosPrinter qr(String data, int moduleSize)A QR code with a chosen module size.
public EscPosPrinter image(Image image)A raster image (GS v 0), thresholded to black and white.
public EscPosPrinter bytes(byte[] bytes)Appends raw bytes, for commands this class does not cover.
public byte[] getJob()The job built so far.
public void send() throws IOExceptionSends the job on the calling thread, which must not be the EDT, and starts a new one.
public AsyncResource<Boolean> print()Sends the job on a thread of its own and starts a new one.

Inherited methods

Field details

LEFT

public static final int LEFT = 0
Left alignment, for align(int).

CENTER

public static final int CENTER = 1
Centered, for align(int).

Constructor details

EscPosPrinter

public EscPosPrinter(ByteLink link)

Parameters

link ByteLink
the connection to the printer; opened when printing

Method details

reset

public final EscPosPrinter reset()
Restores the printer’s defaults (ESC @).

Returns

this printer

text

public EscPosPrinter text(String text)
Appends text without a line break.

Parameters

text String
the text

Returns

this printer

line

public EscPosPrinter line(String text)
Appends a line of text.

Parameters

text String
the text

Returns

this printer

bold

public EscPosPrinter bold(boolean on)
Bold on or off (ESC E).

Parameters

on boolean
true for bold

Returns

this printer

underline

public EscPosPrinter underline(boolean on)
Underline on or off (ESC -).

Parameters

on boolean
true to underline

Returns

this printer

align

public EscPosPrinter align(int alignment)
Alignment of the following lines (ESC a).

Parameters

alignment int
LEFT, CENTER or RIGHT

Returns

this printer

size

public EscPosPrinter size(int width, int height)
Character size as multiples of normal (GS !).

Parameters

width int
1 to 8
height int
1 to 8

Returns

this printer

feed

public EscPosPrinter feed(int lines)
Feeds blank lines (ESC d).

Parameters

lines int
how many

Returns

this printer

cut

public EscPosPrinter cut()
Cuts the paper, partially where the printer supports it (GS V 1).

Returns

this printer

openDrawer

public EscPosPrinter openDrawer()
Kicks the cash drawer on the printer’s drawer port (ESC p), pin 2.

Returns

this printer

openDrawer

public EscPosPrinter openDrawer(int pin)
Kicks a cash drawer (ESC p).

Parameters

pin int
0 for connector pin 2, 1 for pin 5 (a second drawer)

Returns

this printer

qr

public EscPosPrinter qr(String data)
A QR code (GS ( k, model 2, error correction M).

Parameters

data String
what it encodes, ASCII

Returns

this printer

qr

public EscPosPrinter qr(String data, int moduleSize)
A QR code with a chosen module size.

Parameters

data String
what it encodes, ASCII
moduleSize int
dots per module, 1 to 16

Returns

this printer

image

public EscPosPrinter image(Image image)
A raster image (GS v 0), thresholded to black and white. Scale it to the paper’s width in dots first – 384 for 58mm paper, 576 for 80mm.

Parameters

image Image
the image

Returns

this printer

bytes

public EscPosPrinter bytes(byte[] bytes)
Appends raw bytes, for commands this class does not cover.

Parameters

bytes byte[]
the bytes

Returns

this printer

getJob

public byte[] getJob()
The job built so far.

Returns

the bytes print() would send

send

public void send() throws IOException
Sends the job on the calling thread, which must not be the EDT, and starts a new one.

Throws

IOException
when the printer cannot be reached

print

public AsyncResource<Boolean> print()
Sends the job on a thread of its own and starts a new one.

Returns

completes on the EDT once the bytes are handed to the printer