escpos-coffee

repository·master·Indexed 43 days ago

https://github.com/anastaciocintra/escpos-coffee

A Java library for generating ESC/POS printer commands. It enables developers to send text, images, barcodes, and QR codes to thermal printers via an OutputStream, supporting redirection to physical printers, files, or network sockets. Version 4.1.0 provides features such as text style configuration, dithering algorithms for images, and support for various character code tables.

Tokens
1.4K
Snippets
9
Records
9
Agent score
6%

What's inside escpos-coffee

  1. Install escpos-coffee manually

    master

    If you are not using Maven or Gradle:

    1. Download the source code from the latest release on GitHub.
    2. Compile the project using mvn clean package.
    3. Locate the generated .jar file in the target/ folder and add it to your project's classpath.
    mvn clean package
  2. Install escpos-coffee via Gradle

    master

    To use the library in a Gradle project, follow these two steps:

    1. Add mavenCentral() to your repositories block.
    2. Add the dependency to your dependencies block.
    repositories {
        mavenCentral()
    }
    
    dependencies {
        implementation 'com.github.anastaciocintra:escpos-coffee:4.1.0'
    }
  3. Print text using different character code tables

    master

    To support different languages, use setCharacterCodeTable to set the appropriate character encoding before writing text.

    escpos.setCharacterCodeTable(CharacterCodeTable.CP863_Canadian_French);
    escpos.writeLF("Liberté et Fraternité.");
  4. Print images with justification

    master

    When printing images using ImageWrapper, you can set the justification (e.g., Center) before writing the image to the printer.

    escpos.writeLF("print on Center");
    imageWrapper.setJustification(EscPosConst.Justification.Center);
    escpos.write(imageWrapper, escposImage);
  5. Print images with dithering algorithms

    master

    To improve image quality on thermal printers, you can use dithering algorithms like BitonalOrderedDither when creating an EscPosImage.

    algorithm = new BitonalOrderedDither();
    EscPosImage escposImage = new EscPosImage(new CoffeeImageImpl(imageBufferedImage), algorithm);     
    escpos.write(imageWrapper, escposImage);
  6. Send 'Hello World' to a printer

    master

    This example demonstrates how to find available print services, initialize a PrinterOutputStream with a specific service, and use the EscPos class to send text and commands to the printer.

    import com.github.anastaciocintra.escpos.EscPos;
    import com.github.anastaciocintra.output.PrinterOutputStream;
    
    import javax.print.PrintService;
    import java.io.IOException;
    
    public class HelloWorld {
        public static void main(String[] args) throws IOException {
            if(args.length!=1){
                System.out.println("Usage: java -jar escpos-simple.jar ("printer name")");
                System.out.println("Printer list to use:");
                String[] printServicesNames = PrinterOutputStream.getListPrintServicesNames();
                for(String printServiceName: printServicesNames){
                    System.out.println(printServiceName);
                }
    
                System.exit(0);
            }
    
            PrintService printService = PrinterOutputStream.getPrintServiceByName(args[0]);
            PrinterOutputStream printerOutputStream = new PrinterOutputStream(printService);
            EscPos escpos = new EscPos(printerOutputStream);
            escpos.writeLF("Hello world");
            escpos.feed(5).cut(EscPos.CutMode.FULL);
            escpos.close();
        }
    }
  7. Configure text styles

    master

    Use the Style class to define text formatting such as font size and justification. This is useful for creating receipts with titles and subtitles.

    Style title = new Style()
            .setFontSize(Style.FontSize._3, Style.FontSize._3)
            .setJustification(EscPosConst.Justification.Center);