ESCPOS-ThermalPrinter-Android

repository·master·Indexed 23 days ago

https://github.com/dantsu/escpos-thermalprinter-android

An Android library for printing to ESC/POS thermal printers via Bluetooth, TCP, or USB connections. It supports formatted text with alignment tags, font size and color modifications, bold and underline styles, as well as printing images, barcodes, and QR codes.

Tokens
4.9K
Snippets
5
Records
18
Agent score
31%

What's inside escpos-thermalprinter-android

  1. Format text with font size and color

    master

    Use the <font> tag to modify text size and color. The default is normal size and black color.

    Size Options:

    • <font size='normal'>...</font>: Default size
    • <font size='wide'>...</font>: Double width
    • <font size='tall'>...</font>: Double height
    • <font size='big'>...</font>: Double width and height
    • <font size='big-2'>...</font>: 3x width and height
    • <font size='big-3'>...</font>: 4x width and height
    • <font size='big-4'>...</font>: 5x width and height
    • <font size='big-5'>...</font>: 6x width and height
    • <font size='big-6'>...</font>: 7x width and height

    Color Options:

    • <font color='black'>...</font>: Black text, white background
    • <font color='bg-black'>...</font>: White text, black background
    • <font color='red'>...</font>: Red text, white background (Hardware dependent)
    • <font color='bg-red'>...</font>: White text, red background (Hardware dependent)
  2. Use text alignment and column separation tags

    master

    You can control text alignment and create columns by adding alignment tags within a line of text. Adding an alignment tag implicitly creates a new column.

    Alignment Tags:

    • [L]: Left alignment
    • [C]: Center alignment
    • [R]: Right alignment

    Examples:

    • [L]Some text: One column aligned to the left.
    • [L]Some text[R]Some other text: Two columns; the first is left-aligned, the second is right-aligned at the edge of the paper.
    • [L][R]text[R]here: Three columns where the first column is empty but occupies one-third of the space.
  3. Install ESCPOS-ThermalPrinter-Android via JitPack

    master

    To use this library in your Android project, follow these two steps:

    1. Add the JitPack repository to your root /build.gradle file inside the allprojects block:

    2. Add the library dependency to your /app/build.gradle file:

    Note: The current version used in these examples is 3.3.0.

    // Step 1: Root build.gradle
    allprojects {
        repositories {
            ...
            maven { url 'https://jitpack.io' }
        }
    }
    
    // Step 2: App build.gradle
    dependencies {
        ...
        implementation 'com.github.DantSu:ESCPOS-ThermalPrinter-Android:3.3.0'
    }
  4. Print images using hexadecimal strings

    master

    To print an image, use the <img> tag containing a hexadecimal string representation of the image. Use PrinterTextParserImg.bitmapToHexadecimalString to convert a Drawable, BitmapDrawable, or Bitmap into the required format.

    Syntax:

    • <img>hexadecimal_string</img>

    Constraints:

    • A line containing an <img> tag can have only one alignment tag, which must be at the beginning of the line.
    • <img must be directly preceded by nothing or an alignment tag ([L], [C], or [R]).
    • </img> must be directly followed by a newline \n.
    • You cannot write text on the same line as an <img> tag.
    • Maximum image height is 256px.
  5. Print barcodes

    master

    Use the <barcode> tag to print various barcode types. The content inside the tag is the data to be encoded.

    Supported Types and Examples:

    • EAN13 (Default): <barcode>451278452159</barcode> (12 numbers)
    • EAN8: <barcode type='ean8'>4512784</barcode> (7 numbers)
    • UPC-A: <barcode type='upca' height='20'>4512784521</barcode> (11 numbers)
    • UPC-E: <barcode type='upce' height='25' width='50' text='none'>512789</barcode> (6 numbers, text hidden)
    • Code 128: <barcode type='128' width='40' text='above'>DantSu</barcode> (String, text displayed above)

    Constraints:

    • A line containing a <barcode> tag can have only one alignment tag, which must be at the beginning of the line.
    • <barcode> must be directly preceded by nothing or an alignment tag ([L], [C], or [R]).
    • </barcode> must be directly followed by a newline \n.
    • You cannot write text on the same line as a <barcode> tag.
  6. Configure Bluetooth permissions for thermal printing

    master

    To print via Bluetooth, you must declare the following permissions in your AndroidManifest.xml:

    • android.permission.BLUETOOTH
    • android.permission.BLUETOOTH_ADMIN
    • android.permission.BLUETOOTH_CONNECT (Required for Android S and above)
    • android.permission.BLUETOOTH_SCAN (Required for Android S and above)

    Additionally, you must handle runtime permission requests for these permissions, especially for BLUETOOTH_CONNECT and BLUETOOTH_SCAN on devices running Android 12 (API level 31) or higher.

    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
  7. Print QR codes

    master

    Use the <qrcode> tag to print QR codes. The content inside the tag is the data to be encoded.

    Examples:

    • Default size: <qrcode>https://dantsu.com/</qrcode> (20mm x 20mm)
    • Custom size: <qrcode size='25'>123456789</qrcode> (25mm x 25mm)

    Constraints:

    • A line containing a <qrcode> tag can have only one alignment tag, which must be at the beginning of the line.
    • <qrcode> must be directly preceded by nothing or an alignment tag ([L], [C], or [R]).
    • </qrcode> must be directly followed by a newline \n.
    • You cannot write text on the same line as a <qrcode> tag.
  8. Print via TCP/IP connection

    master

    To print over a network using TCP, ensure you have the <uses-permission android:name="android.permission.INTERNET"/> permission in your AndroidManifest.xml.

    Because network operations should not be performed on the main UI thread, wrap the printing logic in a new Thread. Use the TcpConnection class with the printer's IP address and port.

    Example usage:

    new Thread(new Runnable() {
        public void run() {
            try {
                EscPosPrinter printer = new EscPosPrinter(new TcpConnection("192.168.1.3", 9300, 15), 203, 48f, 32);
                printer.printFormattedText("...");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }).start();
    new Thread(new Runnable() {
        public void run() {
            try {
                EscPosPrinter printer = new EscPosPrinter(new TcpConnection("192.168.1.3", 9300, 15), 203, 48f, 32);
                printer
                    .printFormattedText(
                        "[C]<img>" + PrinterTextParserImg.bitmapToHexadecimalString(printer, getApplicationContext().getResources().getDrawableForDensity(R.drawable.logo, DisplayMetrics.DENSITY_MEDIUM)) + "</img>\n" +
                        "[L]\n" +
                        "[C]<u><font size='big'>ORDER N°045</font></u>\n" +
                        "[L]\n" +
                        "[C]================================\n" +
                        "[L]\n" +
                        "[L]<b>BEAUTIFUL SHIRT</b>[R]9.99e\n" +
                        "[L]  + Size : S\n" +
                        "[L]\n" +
                        "[L]<b>AWESOME HAT</b>[R]24.99e\n" +
                        "[L]  + Size : 57/58\n" +
                        "[L]\n" +
                        "[C]--------------------------------\n" +
                        "[R]TOTAL PRICE :[R]34.98e\n" +
                        "[R]TAX :[R]4.23e\n" +
                        "[L]\n" +
                        "[C]================================\n" +
                        "[L]\n" +
                        "[L]<font size='tall'>Customer :</font>\n" +
                        "[L]Raymond DUPONT\n" +
                        "[L]5 rue des girafes\n" +
                        "[L]31547 PERPETES\n" +
                        "[L]Tel : +33801201456\n" +
                        "[L]\n" +
                        "[C]<barcode type='ean13' height='10'>831254784551</barcode>\n" +
                        "[C]<qrcode size='20'>https://dantsu.com/</qrcode>"
                    );
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }).start();
  9. Print via Bluetooth using EscPosPrinter

    master

    To print using a Bluetooth connection, use BluetoothPrintersConnections.selectFirstPaired() to obtain a connection. The EscPosPrinter constructor requires the connection, printer density (DPI), paper width (mm), and character size.

    Example usage:

    EscPosPrinter printer = new EscPosPrinter(BluetoothPrintersConnections.selectFirstPaired(), 203, 48f, 32);
    printer.printFormattedText("[C]<img...>...</img>\n...");
    EscPosPrinter printer = new EscPosPrinter(BluetoothPrintersConnections.selectFirstPaired(), 203, 48f, 32);
    printer
        .printFormattedText(
            "[C]<img>" + PrinterTextParserImg.bitmapToHexadecimalString(printer, this.getApplicationContext().getResources().getDrawableForDensity(R.drawable.logo, DisplayMetrics.DENSITY_MEDIUM))+"</img>\n" +
            "[L]\n" +
            "[C]<u><font size='big'>ORDER N°045</font></u>\n" +
            "[L]\n" +
            "[C]================================\n" +
            "[L]\n" +
            "[L]<b>BEAUTIFUL SHIRT</b>[R]9.99e\n" +
            "[L]  + Size : S\n" +
            "[L]\n" +
            "[L]<b>AWESOME HAT</b>[R]24.99e\n" +
            "[L]  + Size : 57/58\n" +
            "[L]\n" +
            "[C]--------------------------------\n" +
            "[R]TOTAL PRICE :[R]34.98e\n" +
            "[R]TAX :[R]4.23e\n" +
            "[L]\n" +
            "[C]================================\n" +
            "[L]\n" +
            "[L]<font size='tall'>Customer :</font>\n" +
            "[L]Raymond DUPONT\n" +
            "[L]5 rue des girafes\n" +
            "[L]31547 PERPETES\n" +
            "[L]Tel : +33801201456\n" +
            "[L]\n" +
            "[C]<barcode type='ean13' height='10'>831254784551</barcode>\n" +
            "[C]<qrcode size='20'>https://dantsu.com/</qrcode>"
        );
  10. Print via USB connection

    master

    To print via USB, you must declare <uses-feature android:name="android.hardware.usb.host" /> in your AndroidManifest.xml.

    USB printing requires handling runtime permission requests via a BroadcastReceiver. Once permission is granted, you can use UsbPrintersConnections.selectFirstConnected(this) to find the device and initialize the UsbConnection.

    Example usage:

    EscPosPrinter printer = new EscPosPrinter(new UsbConnection(usbManager, usbDevice), 203, 48f, 32);
    printer.printFormattedText("...");
    EscPosPrinter printer = new EscPosPrinter(new UsbConnection(usbManager, usbDevice), 203, 48f, 32);
    printer
        .printFormattedText(
            "[C]<img>" + PrinterTextParserImg.bitmapToHexadecimalString(printer, this.getApplicationContext().getResources().getDrawableForDensity(R.drawable.logo, DisplayMetrics.DENSITY_MEDIUM))+"</img>\n" +
            "[L]\n" +
            "[C]<u><font size='big'>ORDER N°045</font></u>\n" +
            "[L]\n" +
            "[C]================================\n" +
            "[L]\n" +
            "[L]<b>BEAUTIFUL SHIRT</b>[R]9.99e\n" +
            "[L]  + Size : S\n" +
            "[L]\n" +
            "[L]<b>AWESOME HAT</b>[R]24.99e\n" +
            "[L]  + Size : 57/58\n" +
            "[L]\n" +
            "[C]--------------------------------\n" +
            "[R]TOTAL PRICE :[R]34.98e\n" +
            "[R]TAX :[R]4.23e\n" +
            "[L]\n" +
            "[C]================================\n" +
            "[L]\n" +
            "[L]<font size='tall'>Customer :</font>\n" +
            "[L]Raymond DUPONT\n" +
            "[L]5 rue des girafes\n" +
            "[L]31547 PERPETES\n" +
            "[L]Tel : +33801201456\n" +
            "[L]\n" +
            "[C]<barcode type='ean13' height='10'>831254784551</barcode>\n" +
            "[C]<qrcode size='20'>https://dantsu.com/</qrcode>"
        );
  11. Configure Charset Encoding

    master

    If your printer requires a specific character set (e.g., for special symbols or different languages), use the EscPosCharsetEncoding class.

    Constructor: EscPosCharsetEncoding(String charsetName, int escPosCharsetId)

    • charsetName: The name of the charset (e.g., "ISO-8859-1").
    • escPosCharsetId: The specific ID required by your printer hardware (e.g., 6).