Class 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
?.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intCentered, foralign(int).static final intLeft alignment, foralign(int).static final intRight alignment, foralign(int). -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionalign(int alignment) Alignment of the following lines (ESC a).bold(boolean on) Bold on or off (ESC E).bytes(byte[] bytes) Appends raw bytes, for commands this class does not cover.cut()Cuts the paper, partially where the printer supports it (GS V 1).feed(int lines) Feeds blank lines (ESC d).byte[]getJob()The job built so far.A raster image (GS v 0), thresholded to black and white.Appends a line of text.Kicks the cash drawer on the printer's drawer port (ESC p), pin 2.openDrawer(int pin) Kicks a cash drawer (ESC p).print()Sends the job on a thread of its own and starts a new one.A QR code (GS ( k, model 2, error correction M).A QR code with a chosen module size.final EscPosPrinterreset()Restores the printer's defaults (ESC @).voidsend()Sends the job on the calling thread, which must not be the EDT, and starts a new one.size(int width, int height) Character size as multiples of normal (GS !).Appends text without a line break.underline(boolean on) Underline on or off (ESC -).
-
Field Details
-
LEFT
-
CENTER
-
RIGHT
-
-
Constructor Details
-
EscPosPrinter
Parameters
link: the connection to the printer; opened when printing
-
-
Method Details
-
reset
Restores the printer's defaults (ESC @).
Returns
this printer
-
text
Appends text without a line break.
Parameters
text: the text
Returns
this printer
-
line
Appends a line of text.
Parameters
text: the text
Returns
this printer
-
bold
Bold on or off (ESC E).
Parameters
on: true for bold
Returns
this printer
-
underline
Underline on or off (ESC -).
Parameters
on: true to underline
Returns
this printer
-
align
-
size
Character size as multiples of normal (GS !).
Parameters
-
width: 1 to 8 -
height: 1 to 8
Returns
this printer
-
-
feed
Feeds blank lines (ESC d).
Parameters
lines: how many
Returns
this printer
-
cut
Cuts the paper, partially where the printer supports it (GS V 1).
Returns
this printer
-
openDrawer
Kicks the cash drawer on the printer's drawer port (ESC p), pin 2.
Returns
this printer
-
openDrawer
Kicks a cash drawer (ESC p).
Parameters
pin: 0 for connector pin 2, 1 for pin 5 (a second drawer)
Returns
this printer
-
qr
A QR code (GS ( k, model 2, error correction M).
Parameters
data: what it encodes, ASCII
Returns
this printer
-
qr
A QR code with a chosen module size.
Parameters
-
data: what it encodes, ASCII -
moduleSize: dots per module, 1 to 16
Returns
this printer
-
-
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: the image
Returns
this printer
-
bytes
Appends raw bytes, for commands this class does not cover.
Parameters
bytes: the bytes
Returns
this printer
-
getJob
public byte[] getJob() -
send
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
- Throws:
IOException
-
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
-