Print a ticket from Java using a printer EPSON TM-U220D
.
The code is very similar to that used in all printers with Java, the operating system is responsible for registering the drivers and the device and then references to the port and write it using a Java stream. Unlike the console, here if you must put the characters of carriage return along with the line break. Looking a bit I found this code in javamexico , for a similar model and as a way to do it from scratch , although I'm pretty sure it will work. If you need the specific functions to get the most out of it, you can find them here . The latest epson software you can find here .
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class MainClass {
public static void main(String[] args) {
try {
FileWriter fw = new FileWriter("COM4:");
PrintWriter pw = new PrintWriter(fw);
String s = "PROBANDO ";
int i, len = s.length();
for (i = 0; len > 80; i += 80) {
pw.print(s.substring(i, i + 80));
pw.print("\r\n");
len -= 80;
}
if (len > 0) {
pw.print(s.substring(i));
pw.print("\r\n");
}
pw.close();
} catch (IOException e) {
System.out.println(e);
}
}
}
Another way a little more suitable for modern printers is through the class PrintServiceLookup
, as shown by the code from my friend Piratlax.
In linux, if you have a thermal printer connected via usb then you must replace the line:
FileWriter fw = new FileWriter("COM4:");
for
FileWriter fw = new FileWriter("/dev/usb/lp0");