开发者

Printing via cups using javax.print

I have a file that works just fine if I use the command lp filename.

The file is an ESC/P file for a receipt impact printer. That has linux native CUPS drivers and all that works.

Im trying to use the javax.print API so that I can have a finer grained control over the printing and hopefully keep it cross-platform compatible, though Linux is the target platform.

I've tried every DocFlavor combination known to man and every type of data type (InputStream, byte[], Reader etc.)

It either just ig开发者_运维百科nores the print() command all together or just flips out a blank sheet of paper. Running lp filename prints it perfect, so how do I get javax.print to just do the functional equivalent of lp filename?

I'm not set on using javax.print I can use other "things" and might start investigating cups4J but it seems it would limit me to Linux/*nix only, which is OK for now but would rather have a cross platform solution.

I could just issue the lp system command on the file but, I need finer grained control. These aren't receipts we're printing, they are tickets and the tickets range from $5.00 to to thousands of dollars. Currently if we detect a printing issue, we void the transaction and if anything printed, its invalid, we don't take reprints lightly and most of the time charge to print a new copy if the customer looses his copy. Oh the reason for doing this is we're changing the POS system from Windows to Linux and the printers from direct access over serial ports to CUPS managed over USB. Here's my code that doesn't work. Any help is appreciated.

try {
    // Find the default service
    DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;

    PrintService service = PrintServiceLookup.lookupDefaultPrintService();

    // Create the print job
    DocPrintJob job = service.createPrintJob();
    InputStream in = new FileInputStream("/home/bart/real.escp");
    Doc docNew = new SimpleDoc(in,flavor,null);

    // Monitor print job events; for the implementation of PrintJobWatcher,
    // see Determining When a Print Job Has Finished
    PrintJobWatcher pjDone = new PrintJobWatcher(job);

    // Print it
    job.print(docNew, null);

    // Wait for the print job to be done
    pjDone.waitForDone();

    // It is now safe to close the input stream
    in.close();
} catch (PrintException e) {
} catch (IOException e) {
}


I am fine with cups4j. First get your printer.

try { 
        CupsClient client = new CupsClient("addressOfTheCupsServer", 631);
        List<CupsPrinter> printers = client.getPrinters();
        if (printers.size() == 0) {
            throw new RuntimeException("Cant list Printer");
        }
        for (CupsPrinter cupsPrinter : printers) {
            if (cupsPrinter.getName().equals("NameOfPrinter")) {
                selectedPrinter = cupsPrinter;
            }
        }
    }catch (Exception ignored){
        ignored.printStackTrace();
    }
}

Then create a printjob and send it to the printer

 PrintJob printJob = new PrintJob.Builder(inputStream).jobName("Jobname").build();
    PrintRequestResult result = selectedPrinter.print(printJob);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜