首页 > 解决方案 > pos打印机CUP服务器Ubuntu 16的打印问题

问题描述

我在 Ubuntu 16 上安装了 cups 服务器,安装了 POS 打印机后,经过测试页面和 lpr 命令行命令的测试,它工作正常;在使用PrintJob的java中,java找到打印机,但没有任何打印机......

解决这个问题的一些想法?

标签: javaubuntucups

解决方案


前段时间我也遇到了同样的问题,我做了这个,也许可以帮助你。让我知道。

public static boolean imprimirDocto(String documentPath, String printerName) {
        File f = new File(documentPath);
        try {
            PDDocument doc = PDDocument.load(f);

            PrinterJob pj = PrinterJob.getPrinterJob();

            PrintService[] ps = PrintServiceLookup.lookupPrintServices(null, null);
            PrintService printService = null;
            if (ps.length > 0) {
                //This searchs for all the printers, and looks for the 'printerName'
                for (int i = 0; i < ps.length; i++) {
                    System.out.println("Printer name: " + ps[i]);
                    if (ps[i].getName().toLowerCase().contains(printerName.toLowerCase())
                            || ps[i].getName().contains(printerName)) {

                        printService = ps[i];

                        System.out.println("Setting the printer...");
                        pj.setPrintService(printService);
                        i = ps.length;
                    }
                }

                pj.setPageable(new PDFPageable(doc));
                pj.print();

                return true;

            } else {
                return false;
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }
    }

推荐阅读