1

I have a java application to print and auto cut the receipt using a thermal printer (Bixolon srp 350 plus)

Initially I was having problem with auto cutting the receipt but after many trials and google search, I somehow manage to auto cut the receipt. But the problem is that when I deploy the war application in my test machine, it prints fine but it is not cutting the paper at the end. I even deployed the war file into my development machine's tomcat and it is auto cutting fine.

Both the development machine and the test machine are using windows 7 - ultimate , the same apache-tomcat-6.0.18 , and JDK6/JRE6 .

Initially the Test machine had jre6 installed and after auto cutting was unsuccessful . I installed jdk6 which i was using in my development machine to no success.

The two machines are of different brands with different hardware configuration. Can anyone please help me out on this? Is this something to do with the previous JRE6 installed and was not properly removed from windows registry?

I am using grails 1.3.7 along with mysql 5.5.

My code is below :

public void printBill(String printData) throws Exception {
    PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();    
    pras.add(new Copies(5));    
    pras.add(new PrinterResolution(180,180,PrinterResolution.DPI)); 


    PrintService pss[] = PrintServiceLookup.lookupPrintServices(null,pras);    
    if (pss.length == 0)    {
        throw new RuntimeException("No printer services available."); 
    }

    if(printData == null) {
        throw new Exception("nothing to print");
    }

    PrintService ps = pss[0];    

    DocPrintJob job = ps.createPrintJob();
    DocAttributeSet das = new HashDocAttributeSet();    
    das.add(new PrinterResolution(180,180,PrinterResolution.DPI));

    byte[] desc = printData.getBytes();
    Doc doc = new SimpleDoc(desc, DocFlavor.BYTE_ARRAY.AUTOSENSE, das);    

    try {
        job.print(doc, pras);
        cutPaper();
    } catch (Exception e) {
        e.printStackTrace();
    } 
}

/*
 *  TODO improvision to auto cut bill, need to find a proper way to cut
 */
private  void cutPaper() throws Exception{
    TempPageCutter pageCutter = new RestaurantPrinter().new TempPageCutter();

    pageCutter.cutReceipt();

}

private class TempPageCutter implements Printable {

    @Override
    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
            throws PrinterException {
        if(pageIndex > 0)
            return NO_SUCH_PAGE;

        System.out.println("Cutting");
        graphics.drawString("", 0, 0);

        return PAGE_EXISTS;
    }

    public void cutReceipt() throws PrinterException {
        System.out.println("cutReceipt");
        PrintService[]  printService =  PrinterJob.lookupPrintServices();

        if(printService == null || printService.length < 1) {
            return;
        }
        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPrintable(this);
        job.print();
    }


}

If anyone can help me out with a better way to implement the auto cutting functionality it would be a great help.

Bob
  • 22,810
  • 38
  • 143
  • 225
  • I have never worked with Bixolon printers with JAVA before. Can you give me a headstart? A simple example how to print using JAVA maybe. – yuva Dec 04 '14 at 08:43

1 Answers1

0

I was able to resolve the auto cutting issue by setting the bixolon srp 350 plus printer as the default printer in the windows 7 printer setting page. Still its a bit weird. If anyone can help me with a better way to implement auto cutting functionality , it will still be a great help. Cheers!

  • Do u need a jar file to printer using a bixolon printer? I have got one and want to use java to print to it.. – Gillardo Oct 01 '14 at 18:24
  • Not necessary. But if you are using to print from a web browser , I use QZ print and its pretty good . But if you want to print from java application than you have to align the details to the print size and simply call the java api. I use PrintService with DocPrintJob and other Java classes – dave yendrembam Oct 06 '14 at 08:50
  • How did you cut the paper? I am using the D420 and print seems to work, but the paper just keeps feeding and feeding for about 1 metre, then stops with error. I think i need to force a cut, if you have an example, that would be great. – Gillardo Oct 23 '14 at 18:46
  • I use the above code to cut the paper but as i said i got some issue with cutting but was able to solve it by setting the printer as default printer. – dave yendrembam Nov 05 '14 at 13:45