Adel
Adel

Reputation: 6258

Thermal Printing in Java "Printer is not accepting job"

I writ java code for Thermal Printing in java i have tested it on local machine only using microsoft xps document writer and work perfectly ,but when i use Xprinter XP-F900 printer i get the next error

Printer is not accepting job on

full code:

public class printThisBill {


    public static void printCard(final String bill) {

        Printable contentToPrint = new Printable() {            

            @Override
            public int print(Graphics graphics, PageFormat pageFormat, int page)
                    throws PrinterException {
                if (page > 0) {
                    return NO_SUCH_PAGE;
                }
                pageFormat.setOrientation(PageFormat.LANDSCAPE);
                Graphics2D g2d = (Graphics2D) graphics;
                g2d.translate(pageFormat.getImageableX(),
                        pageFormat.getImageableY());
                g2d.setPaint(Color.black);
                g2d.setFont(new Font("Arial", Font.BOLD, 10));

                int y = 15;
                Font f = new Font(Font.SANS_SERIF, Font.PLAIN, 8);
                graphics.setFont(f);
                for (int i = 0; i < Tbill.length; i++) {
                    graphics.drawString(Tbill[i], 5, y);
                    y = y + 15;
                }
                return PAGE_EXISTS;
            }
        };

        PrinterService ps = new PrinterService();
        PrintService pss = null;
        PrinterJob job = null;
        // get the printer service by printer name
        // first test if printer defind by the use search on db
        String query = "SELECT * FROM printer WHERE id_printer=1";
        conn = DBconnect.connectDB();
        if (db.TestIFex(query, conn)) {
            Statement sqlState = null;
            ResultSet rows = null;
            try {
                sqlState = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
                        ResultSet.CONCUR_READ_ONLY);
                rows = sqlState.executeQuery(query);
                String printer_name = rows.getString(2);
                pss = ps.getCheckPrintService(printer_name);
                job = PrinterJob.getPrinterJob();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                try {
                    sqlState.close();
                    rows.close();
                } catch (Exception e) {
                }
            }

        } else {
            job = PrinterJob.getPrinterJob();
            pss = job.getPrintService();
        }

        try {
            job.setPrintService(pss);

        } catch (PrinterException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        job.setPrintable(contentToPrint);
        try {
            job.print();
        } catch (PrinterException e) {
            System.err.println(e.getMessage());
        }    
    }
}

Upvotes: 0

Views: 1977

Answers (1)

Orvil Nordstr&#246;m
Orvil Nordstr&#246;m

Reputation: 325

My first guess is that the printer is not recognized by the computer. When i was working on thermal printing with Java I would have this problem only to realize that the printer wasn't connected to my computer or I was calling upon the incorrect printer in my code.

My second guess is that you need to use byte commands if you are attempting to print a Graphics2D object or an image. Thermal printers can be really tedious to use as there isn't really any standard and one will very likely have to look at the documentation to see how to print barcodes/cut/change font and so forth.

The name of the printer might be quite different from what you may think it is. So I Would recommend first testing to use System.out.println() to see what printer you get, i would also recommend setting the thermal printer you wish to use as the default one if you haven't done that.

        DocPrintJob job =  PrintServiceLookup.lookupDefaultPrintService().createPrintJob();
        System.out.println(job + " <- printer");

Start by seeing what you get out of this. It may be quite different from what you think.

Here is a quick example to see if you can print a String with your printer with a method i made that should be universal to thermal printers.

    public void PrintString(String s) throws Exception{
    DocPrintJob job = PrintServiceLookup.lookupDefaultPrintService().createPrintJob();
    //Get's the default printer so it must be set.
    System.out.println(job + " <- printer");
    DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
    byte[] b = s.getBytes("CP437");
    //Get's the bytes from the String(So that characters such as å ä ö may be printed).
    Doc doc = new SimpleDoc(b, flavor, null);
    //Includes the content to print(b) and what kind of content it is (flavor, in this case a String turned into a byte array).
    job.print(doc, null);          
}

Simply call upon it with whatever String you wish to use as the argument and see what the result is. Again you need to set the thermal printer you wish to use as your default(You could do this in the control panel if you are using windows and i can't imagine that it is very difficult do to on other operating systems).

If you are able to print the String but unable to print the graphics option. Then you need to go here and download the "Programmer manual latest version" or the manual that fits your needs. You may also want to go to the manufacturers website to read more. Developing with thermal printers is not easy at all and requires a whole lot of work if you want to print anything fancy.

Try printing the String though and say what results you got. You did not say weather or not you tested printing a simple String first before going over to advanced stuff such as Graphics2D.

In addition here are some other questions/answers on stackoverflow that may help you get the most out of your thermal printer.

(How to get the printer to print more swiftly)

How to improve speed with Receipt printer and ESC/POS commands in Java

(How to print a String that has been converted to a Graphics object)

Printing reciepts with thermal printer in java

Hope this gave you the help you needed..Sincerely..

//Orville Nordström

Upvotes: 2

Related Questions