Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Receipt Printer auto cutting

Tags:

java

printing

I am using the Java Print Service API to print to a receipt printer. It prints fine, but does not auto cut when finished. How to tell the printer to auto cut?

Here's the code I'm using to print:

       String defaultPrinter = 
                  PrintServiceLookup.lookupDefaultPrintService().getName();
                System.out.println("Default printer: " + defaultPrinter);
                PrintService service = PrintServiceLookup.lookupDefaultPrintService();

                InputStream is=null;
                try {
                    printString+="\f";
                    System.out.println(printString);
                    is = new ByteArrayInputStream(printString.getBytes("UTF8"));                    
                } catch (UnsupportedEncodingException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

                PrintRequestAttributeSet  pras = new HashPrintRequestAttributeSet();
                pras.add(new Copies(1));


                DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;

                Doc doc = new SimpleDoc(is, flavor, null);

                DocPrintJob job = service.createPrintJob();

                PrintJobWatcher pjw = new PrintJobWatcher(job);
                try {
                    job.print(doc, pras);
                } catch (PrintException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                pjw.waitForDone();
                try {
                    is.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
like image 294
James Harpe Avatar asked May 16 '13 15:05

James Harpe


1 Answers

I would imagine there's a proprietary command (in the form of a byte sequence) you need to send to the printer to cut it.

For the TSP100, this page seems to allude to 27, 100 and 3 being the bytes that you need.

Whatever the model, when you've found the command in the form of a sequence of bytes, you should be able to send it using a similar approach to above:

DocPrintJob job = PrintServiceLookup.lookupDefaultPrintService().createPrintJob();  
byte[] bytes = {27, 100, 3};
DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
Doc doc = new SimpleDoc(bytes, flavor, null);
job.print(doc, null);
like image 101
Michael Berry Avatar answered Oct 22 '22 06:10

Michael Berry