Java library for ESC/POS printer
https://github.com/anastaciocintra/escpos-coffee
Java library for ESC/POS printer commands. Can send text, images and barcodes to the printer. All commands are send to one OutputStream, then you can redirect to printer, file or network.
sending “hello world” 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();
}
}
<dependency>
<groupId>com.github.anastaciocintra</groupId>
<artifactId>escpos-coffee</artifactId>
<version>4.1.0</version>
</dependency>
Step 1. Add the repository to your build file
repositories {
mavenCentral()
}
Step 2. Add the dependency
dependencies {
implementation 'com.github.anastaciocintra:escpos-coffee:4.1.0'
}
Download code from the last release of escpos-coffee.
The project can be compiled with the below command:
mvn clean package
Then the jar file will be generated inside the ‘target/’ folder, just add the jar file to your classpath.
You can find all samples codes on https://github.com/anastaciocintra/escpos-coffee-samples
getstart - Send info of the library to the printer.
textstyle - Shows how to construnct one simple receipt.
Also this sample show how simple is to create diferent text styles, like title, subtitle, bold, etc.
Style title = new Style()
.setFontSize(Style.FontSize._3, Style.FontSize._3)
.setJustification(EscPosConst.Justification.Center);
Shows how to work with ImageWrapper.
Then you will see things like how to print on center-justified one image, like this:
escpos.writeLF("print on Center");
imageWrapper.setJustification(EscPosConst.Justification.Center);
escpos.write(imageWrapper, escposImage);
dithering - Shows how to work with BitonalThreshold and BitonalOrderedDither.
Bellow, we can see how to use ordered dither class.
algorithm = new BitonalOrderedDither();
EscPosImage escposImage = new EscPosImage(new CoffeeImageImpl(imageBufferedImage), algorithm);
escpos.write(imageWrapper, escposImage);
barcode - Shows barcode, PDF417 and qrcode.
Bellow, code to send barcode to the printer
BarCode barcode = new BarCode();
escpos.write(barcode, "hello barcode");
charcode - Shows how to send texts from different languages.
escpos.setCharacterCodeTable(CharacterCodeTable.CP863_Canadian_French);
escpos.writeLF("Liberté et Fraternité.");
Android - How to use this lib on Android Studio project.
BarcodeGen - How to generate barcode image and print it
PdfPrinting - How to print pdf files
CoffeeBitmap - How to construct html/css receipts
Using SemVer for versioning.
Lastest release here.
Contributors are welcome, but before you do it its important to read and agree with CODE_OF_CONDUCT.md and CONTRIBUTING.md.
I would like to thanks Michael Billington and contributors for the great work on the mike42/escpos-php project that inspired me to start this project.