I have problem with cutting paper. I have Epson TM-T20 Receipt, and trying to cut paper after printing is done. I found somewhere that this is code for cutting byte[] bytes = { 27, 100, 3 }
, but it's not working.
Below is code I'm using for printing.
public static void printer(String printerData, Boolean bill) throws IOException {
try {
PrintService[] printServices = PrinterJob.lookupPrintServices();
String testData = printerData + "\r";
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] bytes = { 27, 100, 3 }; //Code for cutting
outputStream.write(testData.getBytes(Charset.forName("UTF-8")));
outputStream.write(bytes);
InputStream is = new ByteArrayInputStream(outputStream.toByteArray());
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
System.out.println(service);
DocPrintJob job = service.createPrintJob();
Doc doc = new SimpleDoc(is, flavor, null);
PrintJobWatcher pjDone = new PrintJobWatcher(job);
job.print(doc, null);
pjDone.waitForDone();
is.close();
} catch (PrintException e) {
Writer fw = new OutputStreamWriter(new FileOutputStream("log.txt", true), StandardCharsets.UTF_8);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter writer = new PrintWriter(bw);
e.printStackTrace(writer);
writer.close();
} catch (IOException e) {
Writer fw = new OutputStreamWriter(new FileOutputStream("log.txt", true), StandardCharsets.UTF_8);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter writer = new PrintWriter(bw);
e.printStackTrace(writer);
writer.close();
}
}
Problem with this was fact that just string was sent to printer, not information about printing status. So, class and method is completely changed.
public class Printing implements Printable {
private String stringToPrint;
public Printing(String stringToPrint) {
this.stringToPrint = stringToPrint;
}
@Override
public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
if (pageIndex >= 1) {
return Printable.NO_SUCH_PAGE;
}
g.setColor(Color.black);
g.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 10));
g.translate(0, 0);
int x = 0;
int y = 100;
//
for (String line : stringToPrint.split("\n")) {
g.drawString(line, x, y += g.getFontMetrics().getHeight());
}
return Printable.PAGE_EXISTS;
}
public static void printer(String printerData, String designatedPrinter)
throws IOException, PrinterException {
try {
PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
PrintService designatedService = null;
PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
AttributeSet aset = new HashAttributeSet();
aset = new HashAttributeSet();
aset.add(ColorSupported.NOT_SUPPORTED);
String printers = "";
for (int i = 0; i < printServices.length; i++) {
printers += " service found " + printServices[i].getName() + "\n";
}
for (int i = 0; i < printServices.length; i++) {
System.out.println(" service found " + printServices[i].getName());
if (printServices[i].getName().equalsIgnoreCase(designatedPrinter)) {
System.out.println("I want this one: " + printServices[i].getName());
designatedService = printServices[i];
break;
}
}
Writer fw = new OutputStreamWriter(new FileOutputStream("printing.txt"), StandardCharsets.UTF_8);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter writer = new PrintWriter(bw);
writer.print(printers);
writer.close();
PrinterJob pj = PrinterJob.getPrinterJob();
pj.setPrintService(designatedService);
Printable painter;
// Specify the painter
painter = new Printing(printerData);
pj.setPrintable(painter);
pj.print();
} catch (PrinterException e) {
Writer fw = new OutputStreamWriter(new FileOutputStream("log.txt", true), StandardCharsets.UTF_8);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter writer = new PrintWriter(bw);
e.printStackTrace(writer);
writer.close();
}
}
}