Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.
public class EscPosPrinter
- Object
- 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 = 0 | Left alignment, for align(int). |
public static final int CENTER = 1 | Centered, for align(int). |
public static final int RIGHT = 2 | Right alignment, for align(int). |
Constructors
public EscPosPrinter(ByteLink link) |
Methods
Inherited methods
Field details
LEFT
public static final int LEFT = 0Left alignment, for
align(int).CENTER
public static final int CENTER = 1Centered, for
align(int).RIGHT
public static final int RIGHT = 2Right alignment, for
align(int).Constructor details
EscPosPrinter
public EscPosPrinter(ByteLink link)Parameters
linkByteLink- 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
textString- the text
Returns
this printer
line
public EscPosPrinter line(String text)Appends a line of text.
Parameters
textString- the text
Returns
this printer
bold
public EscPosPrinter bold(boolean on)Bold on or off (ESC E).
Parameters
onboolean- true for bold
Returns
this printer
underline
public EscPosPrinter underline(boolean on)Underline on or off (ESC -).
Parameters
onboolean- true to underline
Returns
this printer
align
public EscPosPrinter align(int alignment)Alignment of the following lines (ESC a).
Returns
this printer
size
public EscPosPrinter size(int width, int height)Character size as multiples of normal (GS !).
Parameters
widthint- 1 to 8
heightint- 1 to 8
Returns
this printer
feed
public EscPosPrinter feed(int lines)Feeds blank lines (ESC d).
Parameters
linesint- 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
pinint- 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
dataString- what it encodes, ASCII
Returns
this printer
qr
public EscPosPrinter qr(String data, int moduleSize)A QR code with a chosen module size.
Parameters
dataString- what it encodes, ASCII
moduleSizeint- 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
imageImage- the image
Returns
this printer
bytes
public EscPosPrinter bytes(byte[] bytes)Appends raw bytes, for commands this class does not cover.
Parameters
bytesbyte[]- the bytes
Returns
this printer
getJob
public byte[] getJob()The job built so far.
Returns
the bytes
print() would sendsend
public void send()
throws IOExceptionSends 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
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