Class EscPosPrinter

java.lang.Object
com.codename1.pos.EscPosPrinter

public class EscPosPrinter extends Object

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 Details

  • Constructor Details

    • EscPosPrinter

      public EscPosPrinter(ByteLink link)
      Parameters
      • link: 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: the text
      Returns

      this printer

    • line

      public EscPosPrinter line(String text)

      Appends a line of text.

      Parameters
      • text: the text
      Returns

      this printer

    • bold

      public EscPosPrinter bold(boolean on)

      Bold on or off (ESC E).

      Parameters
      • on: true for bold
      Returns

      this printer

    • underline

      public EscPosPrinter underline(boolean on)

      Underline on or off (ESC -).

      Parameters
      • on: true to underline
      Returns

      this printer

    • align

      public EscPosPrinter align(int alignment)

      Alignment of the following lines (ESC a).

      Parameters
      Returns

      this printer

    • size

      public EscPosPrinter size(int width, int height)

      Character size as multiples of normal (GS !).

      Parameters
      • width: 1 to 8

      • height: 1 to 8

      Returns

      this printer

    • feed

      public EscPosPrinter feed(int lines)

      Feeds blank lines (ESC d).

      Parameters
      • lines: 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: 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: 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: what it encodes, ASCII

      • moduleSize: 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: the image
      Returns

      this printer

    • bytes

      public EscPosPrinter bytes(byte[] bytes)

      Appends raw bytes, for commands this class does not cover.

      Parameters
      • bytes: 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
      Throws:
      IOException
    • 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