### Configure Gradle for Snapshot Versions Source: https://github.com/anastaciocintra/escpos-coffee/wiki/Instalation This Gradle snippet configures your project to fetch snapshot versions of escpos-coffee. It adds both the mavenCentral repository and the Sonatype snapshots repository. This allows Gradle to resolve unstable snapshot dependencies. Avoid using snapshot versions in production. ```gradle repositories { mavenCentral() maven { url "https://oss.sonatype.org/content/repositories/snapshots" } } ``` -------------------------------- ### Add escpos-coffee Gradle Dependency Source: https://github.com/anastaciocintra/escpos-coffee/wiki/Instalation This snippet demonstrates how to include the escpos-coffee library in your Gradle project. It requires adding the mavenCentral repository and then declaring the dependency in your build file. Replace '4.1.0' with the desired version. ```gradle repositories { mavenCentral() } dependencies { implementation 'com.github.anastaciocintra:escpos-coffee:4.1.0' } ``` -------------------------------- ### Add escpos-coffee Maven Dependency Source: https://github.com/anastaciocintra/escpos-coffee/wiki/Instalation This snippet shows how to add the escpos-coffee library as a dependency to your Maven project. Ensure you are using the latest stable version available. This is a standard Maven dependency declaration. ```xml com.github.anastaciocintra escpos-coffee 4.1.0 ``` -------------------------------- ### Configure Maven for Snapshot Versions Source: https://github.com/anastaciocintra/escpos-coffee/wiki/Instalation This XML configuration enables your Maven project to access snapshot versions of the escpos-coffee library from the Sonatype snapshots repository. It specifically enables snapshot releases while disabling regular releases from this repository. Use with caution, as snapshot versions are unstable. ```xml oss.sonatype.org-snapshot http://oss.sonatype.org/content/repositories/snapshots false true ``` -------------------------------- ### Use PrintModeStyle for Basic Text Formatting with Java Source: https://github.com/anastaciocintra/escpos-coffee/wiki/Text-Styles Explains the use of PrintModeStyle as an alternative for printers that may not support advanced Style commands. It demonstrates basic formatting like bold and justification for text output. ```java PrintModeStyle normal = new PrintModeStyle(); escpos.writeLF(normal,"hello normal PrintModeStyle"); PrintModeStyle rightBold = new PrintModeStyle().setBold(true).setJustification(EscPosConst.Justification.Right); escpos.writeLF(rightBold,"Right bold"); escpos.feed(5).cut(EscPos.CutMode.FULL); escpos.close(); ``` -------------------------------- ### Basic Printer Output Setup with ESC/POS Coffee (Java) Source: https://context7.com/anastaciocintra/escpos-coffee/llms.txt Demonstrates how to initialize a printer connection using the ESC/POS Coffee Java library, list available printers, and send basic text output with line feeds, feeding, and cutting. This requires Java 8+ and the escpos-coffee library. It outputs to a Java OutputStream connected to a selected print service. ```java import com.github.anastaciocintra.escpos.EscPos; import com.github.anastaciocintra.output.PrinterOutputStream; import javax.print.PrintService; import java.io.IOException; public class BasicPrinting { public static void main(String[] args) { try { // List all available printers String[] printers = PrinterOutputStream.getListPrintServicesNames(); System.out.println("Available printers:"); for (String printer : printers) { System.out.println(" - " + printer); } // Get printer by name PrintService printService = PrinterOutputStream.getPrintServiceByName("Receipt Printer"); PrinterOutputStream printerOutputStream = new PrinterOutputStream(printService); // Create EscPos instance EscPos escpos = new EscPos(printerOutputStream); // Write text with line feed escpos.writeLF("Welcome to ESC/POS Coffee"); escpos.writeLF("Transaction #12345"); escpos.feed(3); escpos.cut(EscPos.CutMode.FULL); // Always close to flush data to printer escpos.close(); } catch (IOException e) { System.err.println("Printer error: " + e.getMessage()); e.printStackTrace(); } } } ``` -------------------------------- ### Choose ImageWrapper Implementation for Printing Source: https://github.com/anastaciocintra/escpos-coffee/wiki/Printing-Pdf-Content This example lists different `ImageWrapper` implementations in Java. The choice of implementation may affect printing speed and compatibility with your printer. It's recommended to test different wrappers. ```java BitImageWrapper imageWrapper = new BitImageWrapper(); RasterBitImageWrapper imageWrapper = new RasterBitImageWrapper(); GraphicsImageWrapper imageWrapper = new GraphicsImageWrapper(); ``` -------------------------------- ### Java: Send 'Hello World' to Printer Source: https://github.com/anastaciocintra/escpos-coffee/blob/master/README.md This example demonstrates how to send the simple text 'Hello World' to a printer using the escpos-coffee library. It requires specifying the printer service name as a command-line argument. The code initializes an EscPos object with a PrinterOutputStream and then writes the text, followed by feeding the paper and cutting it. ```java 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(); } } ``` -------------------------------- ### Maven Project Setup for ESC/POS Coffee Library Source: https://context7.com/anastaciocintra/escpos-coffee/llms.txt This XML snippet shows how to add the ESC/POS Coffee library as a dependency to a Maven project. It includes the necessary group ID, artifact ID, and version for the library. This setup allows the project to compile and run code that utilizes the ESC/POS Coffee library for printer control. ```xml 4.0.0 com.example receipt-printer 1.0.0 1.8 1.8 UTF-8 com.github.anastaciocintra escpos-coffee 4.1.0 org.apache.maven.plugins maven-compiler-plugin 3.8.1 ``` -------------------------------- ### Create and Apply Custom Text Styles with Java Source: https://github.com/anastaciocintra/escpos-coffee/wiki/Text-Styles Demonstrates how to define custom text styles using the Style class, including setting font size, justification, bold, and underline. These custom styles can then be applied to text written to the ESC/POS printer. ```java Style title = new Style() .setFontSize(Style.FontSize._3, Style.FontSize._3) .setJustification(EscPosConst.Justification.Center); Style subtitle = new Style(escpos.getStyle()) .setBold(true) .setUnderline(Style.Underline.OneDotThick); Style bold = new Style(escpos.getStyle()) .setBold(true); escpos.writeLF(title,"My Market") .feed(3) .write("Client: ") .writeLF(subtitle, "John Doe") .feed(3) .writeLF("Cup of coffee $1.00") .writeLF("Botle of water $0.50") .writeLF("----------------------------------------") .feed(2) .writeLF(bold, "TOTAL $1.50") .writeLF("----------------------------------------") .feed(8) .cut(EscPos.CutMode.FULL); ``` -------------------------------- ### Gradle: escpos-coffee Dependency Installation Source: https://github.com/anastaciocintra/escpos-coffee/blob/master/README.md This code demonstrates how to include the escpos-coffee library in a Gradle project. It involves adding the mavenCentral() repository and then specifying the implementation dependency with the library's coordinates and version. ```gradle repositories { mavenCentral() } dependencies { implementation 'com.github.anastaciocintra:escpos-coffee:4.1.0' } ``` -------------------------------- ### Java: Apply Ordered Dithering to Image for Printing Source: https://github.com/anastaciocintra/escpos-coffee/blob/master/README.md This example illustrates how to apply ordered dithering to an image before printing it using escpos-coffee. It involves creating an EscPosImage with a CoffeeImageImpl and a BitonalOrderedDither algorithm, then writing the processed image to the printer. ```java algorithm = new BitonalOrderedDither(); EscPosImage escposImage = new EscPosImage(new CoffeeImageImpl(imageBufferedImage), algorithm); escpos.write(imageWrapper, escposImage); ``` -------------------------------- ### Use TcpIpOutputStream with Escpos-Coffee (Java) Source: https://github.com/anastaciocintra/escpos-coffee/wiki/OutputStreams Shows how to connect to an Ethernet printer using TcpIpOutputStream with Escpos-Coffee. This requires the printer's host and port. The example sends a greeting message. ```java TcpIpOutputStream outputStream = new TcpIpOutputStream(host,port) EscPos escpos = new EscPos(outputStream); escpos.writeLF("Hello TcpIpOutputStream"); escpos.close(); ``` -------------------------------- ### List Installed Printers with PrinterOutputStream (Java) Source: https://github.com/anastaciocintra/escpos-coffee/wiki/OutputStreams Retrieves and prints the names of all installed printer services on the system. This is useful for identifying the correct printer name to use with PrinterOutputStream. ```java String[] printServicesNames = PrinterOutputStream.getListPrintServicesNames(); for (String printServiceName : printServicesNames) { System.out.println(printServiceName); } ``` -------------------------------- ### Maven: escpos-coffee Dependency Installation Source: https://github.com/anastaciocintra/escpos-coffee/blob/master/README.md This XML snippet shows how to add the escpos-coffee library as a dependency to a Maven project. It specifies the group ID, artifact ID, and version of the library required for inclusion in the project's build. ```xml com.github.anastaciocintra escpos-coffee 4.1.0 ``` -------------------------------- ### Manually Configure Printer Character Table for Portuguese Source: https://github.com/anastaciocintra/escpos-coffee/wiki/Character-Code-Table This code configures the printer's character table specifically for Portuguese by setting the table to code 3. This is the first step in manual character encoding setup. ```java escpos.setPrinterCharacterTable(3); ``` -------------------------------- ### Modify and Use Default Text Style with Java Source: https://github.com/anastaciocintra/escpos-coffee/wiki/Text-Styles Shows how to retrieve and modify the default style of an EscPos object to apply formatting like bold and underline directly to text. It also demonstrates resetting styles and closing the printer connection. ```java Style style = escpos.getStyle(); style.setBold(true); escpos.writeLF("hello bold"); escpos.getStyle().setBold(false).setUnderline(Style.Underline.OneDotThick); escpos.writeLF("Hello Underline"); escpos.feed(5).cut(EscPos.CutMode.FULL); escpos.close(); ``` -------------------------------- ### Network Printer Connection via TCP/IP with ESC/POS Coffee (Java) Source: https://context7.com/anastaciocintra/escpos-coffee/llms.txt Shows how to connect to thermal printers over a network using TCP/IP sockets with the ESC/POS Coffee Java library. This example sends simple text data to a printer at a specified IP address and port. It requires the escpos-coffee library and a network-accessible ESC/POS printer. ```java import com.github.anastaciocintra.escpos.EscPos; import com.github.anastaciocintra.output.TcpIpOutputStream; import java.io.IOException; public class NetworkPrinting { public static void main(String[] args) { String printerIP = "192.168.1.100"; int printerPort = 9100; // Standard ESC/POS port try { // Connect to network printer TcpIpOutputStream tcpOutputStream = new TcpIpOutputStream(printerIP, printerPort); EscPos escpos = new EscPos(tcpOutputStream); // Send data escpos.writeLF("Network Printer Test"); escpos.writeLF("IP: " + printerIP); escpos.writeLF("Port: " + printerPort); escpos.feed(5); escpos.cut(EscPos.CutMode.FULL); escpos.close(); System.out.println("Print job sent successfully"); } catch (IOException e) { System.err.println("Network connection failed: " + e.getMessage()); e.printStackTrace(); } } } ``` -------------------------------- ### Complete Manual Configuration for Portuguese Characters Source: https://github.com/anastaciocintra/escpos-coffee/wiki/Character-Code-Table This code provides a complete example of manually configuring the printer and Java environment to print Portuguese special characters. It sets the printer character table to 3 and the charset name to 'cp860'. ```java escpos.setPrinterCharacterTable(3); escpos.setCharsetName("cp860"); escpos.writeLF("Some Portuguese special characters: çÇÃãõà"); ``` -------------------------------- ### Generate Complete Receipt with Text, Styles, and Barcodes in Java Source: https://context7.com/anastaciocintra/escpos-coffee/llms.txt This Java example generates a detailed receipt including styled headers, transaction details, item listings, totals, payment information, and both linear (CODE128) and QR barcodes. It requires the escpos-coffee library and a configured printer service. The output is formatted for thermal receipt printers. ```java import com.github.anastaciocintra.escpos.EscPos; import com.github.anastaciocintra.escpos.EscPosConst; import com.github.anastaciocintra.escpos.Style; import com.github.anastaciocintra.escpos.barcode.BarCode; import com.github.anastaciocintra.escpos.barcode.QRCode; import com.github.anastaciocintra.output.PrinterOutputStream; import javax.print.PrintService; import java.io.IOException; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class CompleteReceipt { public static void main(String[] args) throws IOException { PrintService printService = PrinterOutputStream.getDefaultPrintService(); PrinterOutputStream printerOutputStream = new PrinterOutputStream(printService); EscPos escpos = new EscPos(printerOutputStream); // Styles Style header = new Style() .setFontSize(Style.FontSize._2, Style.FontSize._2) .setBold(true) .setJustification(EscPosConst.Justification.Center); Style normal = new Style() .setJustification(EscPosConst.Justification.Left_Default); Style bold = new Style().setBold(true); Style rightAlign = new Style() .setJustification(EscPosConst.Justification.Right); // Header escpos.writeLF(header, "ACME Store"); escpos.setStyle(normal); escpos.writeLF("123 Main Street"); escpos.writeLF("New York, NY 10001"); escpos.writeLF("Tel: (555) 123-4567"); escpos.feed(1); // Transaction info String dateTime = LocalDateTime.now() .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); escpos.writeLF("Date: " + dateTime); escpos.writeLF("Receipt #: 2024-0001"); escpos.writeLF("Cashier: John Doe"); escpos.writeLF("--------------------------------"); // Items escpos.writeLF(bold, "Item Qty Price"); escpos.writeLF("--------------------------------"); escpos.write(normal, "Coffee Beans 1kg "); escpos.writeLF(rightAlign, "1 $12.99"); escpos.write(normal, "Espresso Machine "); escpos.writeLF(rightAlign, "1 $299.00"); escpos.write(normal, "Milk Frother "); escpos.writeLF(rightAlign, "2 $39.98"); escpos.writeLF("--------------------------------"); // Totals escpos.write(bold, "Subtotal: "); escpos.writeLF(rightAlign, "$351.97"); escpos.write(normal, "Tax (8.5%): "); escpos.writeLF(rightAlign, "$29.92"); escpos.writeLF(bold, "================================"); escpos.write(bold, "TOTAL: "); Style totalStyle = new Style() .setBold(true) .setFontSize(Style.FontSize._2, Style.FontSize._2) .setJustification(EscPosConst.Justification.Right); escpos.writeLF(totalStyle, "$381.89"); escpos.setStyle(normal); escpos.writeLF("================================"); escpos.feed(1); // Payment escpos.writeLF("Payment: Visa ending in 1234"); escpos.writeLF("Approved"); escpos.feed(1); // Barcode BarCode barcode = new BarCode(); barcode.setSystem(BarCode.BarCodeSystem.CODE128) .setBarCodeSize(2, 80) .setHRIPosition(BarCode.BarCodeHRIPosition.BelowBarCode) .setJustification(EscPosConst.Justification.Center); escpos.write(barcode, "{B20240001"); escpos.feed(2); // QR Code for online receipt QRCode qrCode = new QRCode(); qrCode.setSize(4) .setJustification(EscPosConst.Justification.Center); escpos.writeLF("View receipt online:"); escpos.write(qrCode, "https://store.example.com/receipt/20240001"); escpos.feed(2); // Footer escpos.setStyle(normal); escpos.writeLF("Thank you for your purchase!"); escpos.writeLF("Return policy: 30 days"); escpos.feed(5); // Cut paper escpos.cut(EscPos.CutMode.FULL); escpos.close(); } } ``` -------------------------------- ### Set Java Charset Name for Portuguese (cp860) Source: https://github.com/anastaciocintra/escpos-coffee/wiki/Character-Code-Table This snippet shows how to set the Java charset name to 'cp860', which is an alias for the Portuguese encoding. This is the second step in manual character encoding setup and should be used in conjunction with `setPrinterCharacterTable`. ```java escpos.setCharsetName("cp860"); ``` -------------------------------- ### Java ESC/POS Receipt Printing with Error Handling Source: https://context7.com/anastaciocintra/escpos-coffee/llms.txt Prints a receipt using ESC/POS commands, demonstrating comprehensive error handling for printer discovery, encoding issues, and I/O operations. It includes examples of both traditional try-catch-finally and try-with-resources for resource management. ```java import com.github.anastaciocintra.escpos.EscPos; import com.github.anastaciocintra.output.PrinterOutputStream; import javax.print.PrintService; import java.io.IOException; import java.io.UnsupportedEncodingException; public class ErrorHandling { public static void printReceipt(String printerName, String content) { PrinterOutputStream printerOutputStream = null; EscPos escpos = null; try { // Get printer service with error checking PrintService printService = PrinterOutputStream.getPrintServiceByName(printerName); if (printService == null) { System.err.println("Printer not found: " + printerName); return; } // Create output stream printerOutputStream = new PrinterOutputStream(printService); escpos = new EscPos(printerOutputStream); // Print content escpos.writeLF(content); escpos.feed(5); escpos.cut(EscPos.CutMode.FULL); } catch (IllegalArgumentException e) { System.err.println("Invalid argument: " + e.getMessage()); // Printer not found or invalid parameter } catch (UnsupportedEncodingException e) { System.err.println("Encoding error: " + e.getMessage()); // Character encoding not supported } catch (IOException e) { System.err.println("I/O error: " + e.getMessage()); e.printStackTrace(); // Printer communication error } finally { // Always close resources if (escpos != null) { try { escpos.close(); } catch (IOException e) { System.err.println("Error closing printer: " + e.getMessage()); } } } } public static void main(String[] args) { // Try-with-resources for automatic cleanup try (PrinterOutputStream pos = new PrinterOutputStream(); EscPos escpos = new EscPos(pos)) { escpos.writeLF("Using try-with-resources"); escpos.feed(3); escpos.cut(EscPos.CutMode.FULL); } catch (IOException e) { System.err.println("Printing failed: " + e.getMessage()); } // Traditional method call printReceipt("Receipt Printer", "Test Receipt"); } } ``` -------------------------------- ### Initializing ImageHelper Source: https://github.com/anastaciocintra/escpos-coffee/wiki/HTML-CSS Create an ImageHelper instance with the specified width (matching the rendered image width) and height. This helper is used to manage the image data for printing. ```java ImageHelper imageHelper = new ImageHelper(576,48); ``` -------------------------------- ### Choose Bitonal Implementation Algorithm in Java Source: https://github.com/anastaciocintra/escpos-coffee/wiki/Printing-Pdf-Content This Java code illustrates selecting a bitonal algorithm for image processing. Options include `BitonalThreshold` and `BitonalOrderedDither`. The best choice depends on how well your printer handles different dithering methods. ```java Bitonal algorithm = new BitonalThreshold(); // or Bitonal algorithm = new BitonalOrderedDither(); ``` -------------------------------- ### Choosing an Image Wrapper Implementation Source: https://github.com/anastaciocintra/escpos-coffee/wiki/HTML-CSS Select an appropriate ImageWrapper implementation for converting the BufferedImage into a format suitable for the escpos printer. Different wrappers may yield better printing results or speed. ```java BitImageWrapper imageWrapper = new BitImageWrapper(); // or RasterBitImageWrapper imageWrapper = new RasterBitImageWrapper(); // or GraphicsImageWrapper imageWrapper = new GraphicsImageWrapper(); ``` -------------------------------- ### Obtaining File URL and Base URL Source: https://github.com/anastaciocintra/escpos-coffee/wiki/HTML-CSS After creating the temporary HTML file, this Java code obtains its URL and the parent directory's URL. These are needed for the Flying Saucer renderer to resolve relative paths. ```java String tmpFileURL = temp.toURI().toURL().toExternalForm(); String tmpBaseURL = temp.getParent(); ``` -------------------------------- ### Load PDF Document and Create Renderer in Java Source: https://github.com/anastaciocintra/escpos-coffee/wiki/Printing-Pdf-Content This Java code demonstrates how to load a PDF file and create a PDF renderer object using PDFBox. It requires an input file path to the PDF document. ```java File input = new File("C:\\Users\\macin\\desenv\\Document.pdf"); PDDocument document = PDDocument.load(input); PDFRenderer pdfRenderer = new PDFRenderer(document); ``` -------------------------------- ### Creating HTML Content for Rendering Source: https://github.com/anastaciocintra/escpos-coffee/wiki/HTML-CSS This Java code demonstrates how to create an HTML string that can be rendered into an image. You can also load HTML content directly from a file. ```java String html = "\n" + "\n" + "\n" + "\n" + "

Hello Html

\n" + "\n" + "\n" + "\n"; ``` -------------------------------- ### Maven Dependency for Flying Saucer Source: https://github.com/anastaciocintra/escpos-coffee/wiki/HTML-CSS Add this Maven dependency to your project to include the Flying Saucer library, which is essential for rendering HTML/CSS content into images. ```xml org.xhtmlrenderer flying-saucer-core 9.1.20 ``` -------------------------------- ### Printing Extensive Images with ImageHelper (Java) Source: https://github.com/anastaciocintra/escpos-coffee/wiki/Images This Java code demonstrates how to print large images using the ImageHelper class in conjunction with BitonalThreshold for dithering and GraphicsImageWrapper for image handling. It concludes with feeding the printer and performing a full cut. ```java ImageHelper helper = new ImageHelper(); Bitonal algorithm = new BitonalThreshold(); GraphicsImageWrapper imageWrapper = new GraphicsImageWrapper(); helper.write(escPos, new CoffeeImageImpl(image),imageWrapper,algorithm); escPos.feed(5).cut(EscPos.CutMode.FULL); escPos.close(); ``` -------------------------------- ### Copyright Header for Code Files Source: https://github.com/anastaciocintra/escpos-coffee/blob/master/CONTRIBUTING.md All code files must include this specific MIT license header. This ensures proper attribution and compliance with the project's licensing terms. It should be placed at the beginning of each source file. ```javascript /* * Use of this source code is governed by the MIT license that can be * found in the LICENSE file. */ ``` -------------------------------- ### Print Image using escpos-coffee in Java Source: https://github.com/anastaciocintra/escpos-coffee/wiki/Printing-Pdf-Content This Java code snippet demonstrates the final step of printing an image using escpos-coffee. It utilizes the previously prepared image, selected `ImageWrapper`, and `Bitonal` algorithm to send the data to the printer. ```java imageHelper.write(escpos, new CoffeeImageImpl(image),imageWrapper,algorithm); ``` -------------------------------- ### Render First Page of PDF to BufferedImage in Java Source: https://github.com/anastaciocintra/escpos-coffee/wiki/Printing-Pdf-Content This Java snippet shows how to render the first page (index 0) of a loaded PDF document into a BufferedImage object. This image can then be processed for printing. ```java BufferedImage image = pdfRenderer.renderImage(0); ``` -------------------------------- ### Command Line: Compile escpos-coffee Project Source: https://github.com/anastaciocintra/escpos-coffee/blob/master/README.md This command is used to compile the escpos-coffee project using Maven. Executing this command will clean the project, build it, and package it, typically generating a JAR file in the 'target/' directory. ```bash mvn clean package ``` -------------------------------- ### Apply Text Styling and Formatting with Java ESC/POS Source: https://context7.com/anastaciocintra/escpos-coffee/llms.txt Demonstrates how to apply various text styles like font size, justification, color, and underline to the output. It requires the ESC/POS Java library and a configured printer service. The output is styled text printed to the receipt. ```Java import com.github.anastaciocintra.escpos.EscPos; import com.github.anastaciocintra.escpos.EscPosConst; import com.github.anastaciocintra.escpos.Style; import com.github.anastaciocintra.output.PrinterOutputStream; import javax.print.PrintService; import java.io.IOException; public class TextStyling { public static void main(String[] args) throws IOException { PrintService printService = PrinterOutputStream.getDefaultPrintService(); PrinterOutputStream printerOutputStream = new PrinterOutputStream(printService); EscPos escpos = new EscPos(printerOutputStream); // Create title style: large, centered, white on black Style title = new Style() .setFontSize(Style.FontSize._3, Style.FontSize._3) .setJustification(EscPosConst.Justification.Center) .setColorMode(Style.ColorMode.WhiteOnBlack); // Create subtitle style: medium, bold, centered Style subtitle = new Style() .setFontSize(Style.FontSize._2, Style.FontSize._2) .setBold(true) .setJustification(EscPosConst.Justification.Center); // Create normal style with underline Style normalUnderline = new Style() .setUnderline(Style.Underline.OneDotThick) .setJustification(EscPosConst.Justification.Left_Default); // Create right-aligned style Style rightAlign = new Style() .setJustification(EscPosConst.Justification.Right); // Print with different styles escpos.writeLF(title, "RECEIPT"); escpos.feed(2); escpos.writeLF(subtitle, "Coffee Shop"); escpos.feed(2); escpos.writeLF(normalUnderline, "Items:"); escpos.writeLF("Espresso $3.50"); escpos.writeLF("Cappuccino $4.50"); escpos.feed(1); escpos.writeLF(rightAlign, "Total: $8.00"); escpos.feed(5); escpos.cut(EscPos.CutMode.FULL); escpos.close(); } } ``` -------------------------------- ### Print Images with Thresholding using ESC/POS Coffee (Java) Source: https://context7.com/anastaciocintra/escpos-coffee/llms.txt This Java code snippet illustrates how to print images to an ESC/POS printer using the ESC/POS Coffee library. It demonstrates loading an image, converting it to a CoffeeImage, and applying different threshold values using the BitonalThreshold algorithm for varying image darkness. Requires the ESC/POS Coffee library, Java's ImageIO, and a compatible printer. ```java import com.github.anastaciocintra.escpos.EscPos; import com.github.anastaciocintra.escpos.EscPosConst; import com.github.anastaciocintra.escpos.image.*; import com.github.anastaciocintra.output.PrinterOutputStream; import javax.imageio.ImageIO; import javax.print.PrintService; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; public class ImagePrintingThreshold { public static void main(String[] args) throws IOException { PrintService printService = PrinterOutputStream.getDefaultPrintService(); PrinterOutputStream printerOutputStream = new PrinterOutputStream(printService); EscPos escpos = new EscPos(printerOutputStream); // Load image from file BufferedImage image = ImageIO.read(new File("logo.png")); // Convert to CoffeeImage CoffeeImage coffeeImage = new CoffeeImageImpl(image); // Apply threshold algorithm (default 127) Bitonal thresholdDefault = new BitonalThreshold(); EscPosImage escposImage = new EscPosImage(coffeeImage, thresholdDefault); // Print with RasterBitImage (high quality) RasterBitImageWrapper imageWrapper = new RasterBitImageWrapper(); imageWrapper.setJustification(EscPosConst.Justification.Center); escpos.writeLF("Logo (Default Threshold):"); escpos.write(imageWrapper, escposImage); escpos.feed(2); // Try with darker threshold Bitonal thresholdDark = new BitonalThreshold(100); EscPosImage escposImageDark = new EscPosImage(coffeeImage, thresholdDark); imageWrapper.setJustification(EscPosConst.Justification.Center); escpos.writeLF("Logo (Dark Threshold 100):"); escpos.write(imageWrapper, escposImageDark); escpos.feed(2); // Try with lighter threshold Bitonal thresholdLight = new BitonalThreshold(160); EscPosImage escposImageLight = new EscPosImage(coffeeImage, thresholdLight); imageWrapper.setJustification(EscPosConst.Justification.Center); escpos.writeLF("Logo (Light Threshold 160):"); escpos.write(imageWrapper, escposImageLight); escpos.feed(5); escpos.cut(EscPos.CutMode.FULL); escpos.close(); } } ``` -------------------------------- ### Configure Maven Dependency for PDFBox Source: https://github.com/anastaciocintra/escpos-coffee/wiki/Printing-Pdf-Content This snippet shows the Maven dependency required to include the PDFBox library in your project. PDFBox is essential for loading and rendering PDF documents. ```xml org.apache.pdfbox pdfbox 2.0.19 ``` -------------------------------- ### RasterBitImageWrapper Implementation (Java) Source: https://github.com/anastaciocintra/escpos-coffee/wiki/Images This Java code initializes the RasterBitImageWrapper, an implementation of ImageWrapperInterface. This wrapper utilizes the ESC/POS sequence 'GS "v" "0"' for image printing. ```java RasterBitImageWrapper imageWrapper = new RasterBitImageWrapper(); ``` -------------------------------- ### Writing HTML Content to a Temporary File Source: https://github.com/anastaciocintra/escpos-coffee/wiki/HTML-CSS This Java snippet shows how to write the generated HTML content to a temporary file, which will be used by the Flying Saucer renderer. ```java tmpStreamxhtml.write(html.getBytes("utf-8")); tmpStreamxhtml.close(); ``` -------------------------------- ### Bitonal Threshold Dithering Implementation (Java) Source: https://github.com/anastaciocintra/escpos-coffee/wiki/Images This snippet demonstrates the usage of the BitonalThreshold class for dithering images. It is a built-in implementation for defining the dithering algorithm in the ESC/POS Coffee project. ```java // using bitonal threshold for dithering Bitonal algorithm = new BitonalThreshold(); ``` -------------------------------- ### Use PrinterOutputStream with Escpos-Coffee (Java) Source: https://github.com/anastaciocintra/escpos-coffee/wiki/OutputStreams Demonstrates how to initialize Escpos-Coffee with a PrinterOutputStream and send a simple text message to the printer. Remember to close the Escpos instance when done. ```java escpos = new EscPos(new PrinterOutputStream("your printer name obtained above")); escpos.writeLF("Hello PrinterOutputStream");; // do not forget to close... escpos.close(); ``` -------------------------------- ### Java Image Printing with Ordered Dithering Source: https://context7.com/anastaciocintra/escpos-coffee/llms.txt This Java code snippet demonstrates how to print images using the escpos-coffee library with ordered dithering for improved grayscale output. It requires the escpos-coffee library and standard Java ImageIO for image loading. The input is a BufferedImage, and the output is printed to a connected ESC/POS printer. ```java import com.github.anastaciocintra.escpos.EscPos; import com.github.anastaciocintra.escpos.EscPosConst; import com.github.anastaciocintra.escpos.image.*; import com.github.anastaciocintra.output.PrinterOutputStream; import javax.imageio.ImageIO; import javax.print.PrintService; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; public class ImageDithering { public static void main(String[] args) throws IOException { PrintService printService = PrinterOutputStream.getDefaultPrintService(); PrinterOutputStream printerOutputStream = new PrinterOutputStream(printService); EscPos escpos = new EscPos(printerOutputStream); // Load grayscale or color photo BufferedImage photo = ImageIO.read(new File("photo.jpg")); CoffeeImage coffeePhoto = new CoffeeImageImpl(photo); // Apply ordered dithering algorithm Bitonal orderedDither = new BitonalOrderedDither(); EscPosImage escposImage = new EscPosImage(coffeePhoto, orderedDither); // Print with GraphicsImageWrapper (modern command) GraphicsImageWrapper graphicsWrapper = new GraphicsImageWrapper(); graphicsWrapper.setJustification(EscPosConst.Justification.Center); graphicsWrapper.setGraphicsImageBxBy( GraphicsImageWrapper.GraphicsImageBxBy.Normal_Default ); escpos.writeLF("Photo with Dithering:"); escpos.write(graphicsWrapper, escposImage); escpos.feed(2); // Print double width version graphicsWrapper.setGraphicsImageBxBy( GraphicsImageWrapper.GraphicsImageBxBy.DoubleWidth ); escpos.writeLF("Double Width:"); escpos.write(graphicsWrapper, escposImage); escpos.feed(2); // Print quadruple size graphicsWrapper.setGraphicsImageBxBy( GraphicsImageWrapper.GraphicsImageBxBy.Quadruple ); escpos.writeLF("Quadruple Size:"); escpos.write(graphicsWrapper, escposImage); escpos.feed(5); escpos.cut(EscPos.CutMode.FULL); escpos.close(); } } ``` -------------------------------- ### Bitonal Ordered Dither Implementation (Java) Source: https://github.com/anastaciocintra/escpos-coffee/wiki/Images This snippet shows how to use the BitonalOrderedDither class for image dithering. It's another built-in option for defining the dithering algorithm within the project. ```java Bitonal algorithm = new BitonalOrderedDither(); ``` -------------------------------- ### Control Printer Hardware with ESC/POS Java Commands Source: https://context7.com/anastaciocintra/escpos-coffee/llms.txt This Java code snippet demonstrates how to control ESC/POS printer hardware features such as paper feeding and cutting. It initializes the printer, writes text with different line feeds, performs partial and full cuts, and pulses the cash drawer pin. This requires the escpos-coffee library and a configured print service. ```java import com.github.anastaciocintra.escpos.EscPos; import com.github.anastaciocintra.output.PrinterOutputStream; import javax.print.PrintService; import java.io.IOException; public class PrinterControl { public static void main(String[] args) throws IOException { PrintService printService = PrinterOutputStream.getDefaultPrintService(); PrinterOutputStream printerOutputStream = new PrinterOutputStream(printService); EscPos escpos = new EscPos(printerOutputStream); // Initialize printer (reset to defaults) escpos.initializePrinter(); // Print with different feed amounts escpos.writeLF("Text with 1 line feed"); escpos.feed(1); escpos.writeLF("Text with 3 line feeds"); escpos.feed(3); escpos.writeLF("Text with 5 line feeds"); escpos.feed(5); // Partial cut (leaves small connection) escpos.writeLF("This will have partial cut"); escpos.feed(3); escpos.cut(EscPos.CutMode.PART); // Full cut (complete separation) escpos.writeLF("This will have full cut"); escpos.feed(3); escpos.cut(EscPos.CutMode.FULL); // Pulse pin for cash drawer opening // Pin 2 is typically connected to cash drawer // Pulse ON for 100ms (50 * 2ms), OFF for 200ms (100 * 2ms) escpos.pulsePin(EscPos.PinConnector.Pin_2, 50, 100); escpos.close(); } } ``` -------------------------------- ### BitImageWrapper Implementation (Java) Source: https://github.com/anastaciocintra/escpos-coffee/wiki/Images This Java code initializes the BitImageWrapper, which implements ImageWrapperInterface. This wrapper uses the ESC/POS sequence 'ESC *' for image transmission. ```java BitImageWrapper imageWrapper = new BitImageWrapper(); ``` -------------------------------- ### Print Barcode using escpos-coffee Source: https://github.com/anastaciocintra/escpos-coffee/blob/master/README.md This snippet demonstrates how to generate and print a barcode using the escpos-coffee library. It requires an initialized escpos object and the BarCode class. The output is a barcode representation of the input string 'hello barcode'. ```java BarCode barcode = new BarCode(); escpos.write(barcode, "hello barcode"); ``` -------------------------------- ### Rendering HTML to BufferedImage Source: https://github.com/anastaciocintra/escpos-coffee/wiki/HTML-CSS This Java code uses the Java2D renderer from Flying Saucer to convert the HTML content (specified by its URL) into a BufferedImage. The printer area width (e.g., 576) is crucial for correct scaling. ```java Java2DRenderer render = new Java2DRenderer(tmpFileURL, tmpBaseURL, 576); BufferedImage image = render.getImage(); ``` -------------------------------- ### GraphicsImageWrapper Implementation (Java) Source: https://github.com/anastaciocintra/escpos-coffee/wiki/Images This snippet shows the initialization of GraphicsImageWrapper, an ImageWrapperInterface implementation. It employs the ESC/POS sequence 'GS(L' for handling image data. ```java GraphicsImageWrapper imageWrapper = new GraphicsImageWrapper(); ``` -------------------------------- ### Generate and Print QR Codes Source: https://context7.com/anastaciocintra/escpos-coffee/llms.txt Generates and prints 2D QR codes with configurable error correction levels, size, and justification. This function supports creating QR codes for URLs, payment information, and WiFi configurations. It requires the escpos-coffee library and a printer service. ```java import com.github.anastaciocintra.escpos.EscPos; import com.github.anastaciocintra.escpos.EscPosConst; import com.github.anastaciocintra.escpos.barcode.QRCode; import com.github.anastaciocintra.output.PrinterOutputStream; import javax.print.PrintService; import java.io.IOException; public class QRCodePrinting { public static void main(String[] args) throws IOException { PrintService printService = PrinterOutputStream.getDefaultPrintService(); PrinterOutputStream printerOutputStream = new PrinterOutputStream(printService); EscPos escpos = new EscPos(printerOutputStream); // Small QR code for URLs QRCode qrCodeSmall = new QRCode(); qrCodeSmall.setSize(3) .setErrorCorrectionLevel(QRCode.QRErrorCorrectionLevel.QR_ECLEVEL_M_Default) .setJustification(EscPosConst.Justification.Center); escpos.writeLF("Visit our website:"); escpos.write(qrCodeSmall, "https://github.com/anastaciocintra/escpos-coffee"); escpos.feed(2); // Large QR code with high error correction QRCode qrCodeLarge = new QRCode(); qrCodeLarge.setSize(8) .setModel(QRCode.QRModel._2) .setErrorCorrectionLevel(QRCode.QRErrorCorrectionLevel.QR_ECLEVEL_H) .setJustification(EscPosConst.Justification.Center); escpos.writeLF("Payment QR Code:"); escpos.write(qrCodeLarge, "PAY:12345:USD:150.00:MERCHANT_ID"); escpos.feed(2); // WiFi QR code QRCode qrCodeWifi = new QRCode(); qrCodeWifi.setSize(5) .setJustification(EscPosConst.Justification.Center); String wifiConfig = "WIFI:T:WPA;S:NetworkName;P:password123;;"; escpos.writeLF("WiFi Access:"); escpos.write(qrCodeWifi, wifiConfig); escpos.feed(5); escpos.cut(EscPos.CutMode.FULL); escpos.close(); } } ``` -------------------------------- ### Alternative Manual Configuration for Portuguese (using CP037) Source: https://github.com/anastaciocintra/escpos-coffee/wiki/Character-Code-Table This code demonstrates an alternative manual configuration for Portuguese characters, using printer character table code 64 and Java charset 'cp037'. This option can be tested if the primary configuration does not yield correct results. ```java escpos.setPrinterCharacterTable(64); escpos.setCharsetName("cp037"); escpos.writeLF("Some Portuguese special characters: çÇÃãõà"); ``` -------------------------------- ### Java: Print Centered Image using ImageWrapper Source: https://github.com/anastaciocintra/escpos-coffee/blob/master/README.md This code snippet shows how to print an image that is center-justified on the receipt using the ImageWrapper class. It involves creating an EscPosImage object and then using the ImageWrapper to set the justification before writing the image to the printer output stream. ```java escpos.writeLF("print on Center"); imageWrapper.setJustification(EscPosConst.Justification.Center); escpos.write(imageWrapper, escposImage); ``` -------------------------------- ### Generate PDF417 Barcodes with ESC/POS Coffee (Java) Source: https://context7.com/anastaciocintra/escpos-coffee/llms.txt Demonstrates how to generate PDF417 2D barcodes using the ESC/POS Coffee library in Java. It covers basic generation, custom configurations like dimensions and error correction levels, and truncated barcodes for compact printing. Requires the ESC/POS Coffee library and a compatible printer. ```java import com.github.anastaciocintra.escpos.EscPos; import com.github.anastaciocintra.escpos.EscPosConst; import com.github.anastaciocintra.escpos.barcode.PDF417; import com.github.anastaciocintra.output.PrinterOutputStream; import javax.print.PrintService; import java.io.IOException; public class PDF417Printing { public static void main(String[] args) throws IOException { PrintService printService = PrinterOutputStream.getDefaultPrintService(); PrinterOutputStream printerOutputStream = new PrinterOutputStream(printService); EscPos escpos = new EscPos(printerOutputStream); // Basic PDF417 with default settings PDF417 pdf417Simple = new PDF417(); pdf417Simple.setJustification(EscPosConst.Justification.Center); escpos.writeLF("Default PDF417:"); escpos.write(pdf417Simple, "PRODUCT-ID-12345"); escpos.feed(2); // PDF417 with custom configuration PDF417 pdf417Custom = new PDF417(); pdf417Custom.setJustification(EscPosConst.Justification.Center) .setNumberOfColumns(5) .setWidth(3) .setHeight(4) .setErrorLevel(PDF417.PDF417ErrorLevel._3); escpos.writeLF("Custom PDF417:"); escpos.write(pdf417Custom, "https://example.com/document/12345"); escpos.feed(2); // Truncated PDF417 for compact printing PDF417 pdf417Truncated = new PDF417(); pdf417Truncated.setJustification(EscPosConst.Justification.Center) .setOption(PDF417.PDF417Option.Truncated) .setWidth(2) .setHeight(3) .setErrorLevel(PDF417.PDF417ErrorLevel._5); escpos.writeLF("Truncated PDF417:"); escpos.write(pdf417Truncated, "COMPACT-DATA-ENCODING"); escpos.feed(2); // PDF417 for shipping labels PDF417 pdf417Shipping = new PDF417(); pdf417Shipping.setJustification(EscPosConst.Justification.Center) .setNumberOfColumns(10) .setNumberOfRows(20) .setWidth(2) .setHeight(3) .setErrorLevel(PDF417.PDF417ErrorLevel._4); escpos.writeLF("Shipping Label:"); escpos.write(pdf417Shipping, "SHIP-TO:123-MAIN-ST-NYC-10001-TRACKING:1Z999AA10123456784"); escpos.feed(5); escpos.cut(EscPos.CutMode.FULL); escpos.close(); } } ``` -------------------------------- ### Java: Define Text Style for Receipt Source: https://github.com/anastaciocintra/escpos-coffee/blob/master/README.md This Java code defines a text style for receipt printing, setting the font size to 3x3 and centering the text. This style can then be applied to text elements when constructing a receipt. ```java Style title = new Style() .setFontSize(Style.FontSize._3, Style.FontSize._3) .setJustification(EscPosConst.Justification.Center); ``` -------------------------------- ### Set Character Code Table with CP863 (Canadian French) Source: https://github.com/anastaciocintra/escpos-coffee/wiki/Character-Code-Table This snippet demonstrates how to set the character code table to CP863 (Canadian French) and write a French phrase to the printer. It uses the `setCharacterCodeTable` method from the escpos library. ```java escpos.setCharacterCodeTable(CharacterCodeTable.CP863_Canadian_French); escpos.writeLF("Liberté et Fraternité."); ```