Ubuntu 中的 Java 打印

标签 java ubuntu printing

我正在开发一个小应用程序来在 Ubuntu 中打印数据,问题是我的应用程序在 Windows 中运行良好,使用:

PrintService service = PrintServiceLookup.lookupDefaultPrintService();

FileInputStream fis = new FileInputStream(myfile);

DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
DocPrintJob job = service.createPrintJob();

Doc doc = new SimpleDoc(fis, flavor, null);
job.print(doc, null);
fis.close();

但是在 Ubuntu 中,它只是不打印。对于我正在使用的打印 API,Linux 打印是否有任何特殊配置?还是我还漏掉了其他东西?

最佳答案

我认为您的打印机安装在操作系统中不是默认的。检查一下您的“服务”是什么。 您也可以从打印对话框中选择打印机,如下所示:

PrintRequestAttributeSet pras =
                new HashPrintRequestAttributeSet();
        DocFlavor flavor = DocFlavor.INPUT_STREAM.TEXT_PLAIN_UTF_8;
        PrintRequestAttributeSet aset =
                new HashPrintRequestAttributeSet();
        aset.add(MediaSizeName.ISO_A4);
        aset.add(new Copies(1));
        aset.add(Sides.ONE_SIDED);
        aset.add(Finishings.STAPLE);

        PrintService printService[] =
                PrintServiceLookup.lookupPrintServices(flavor, pras);
        PrintService defaultService =
                PrintServiceLookup.lookupDefaultPrintService();
        PrintService service = ServiceUI.printDialog(null, 200, 200,
                printService, defaultService, flavor, pras);
        if (service != null) {
            try {
                FileInputStream fis = new FileInputStream("c://test.txt");
                DocAttributeSet das = new HashDocAttributeSet();
                Doc doc1 = new SimpleDoc(fis, flavor, das);

                DocPrintJob job1 = service.createPrintJob();

                try {
                    job1.print(doc1, pras);
                } catch (PrintException e) {
                    e.printStackTrace();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

某些打印机不支持文本 DocFlavors,仅支持图像。您还可以使用操作系统 native 方法简单地打印 html 文件,如下所示:

if (Desktop.isDesktopSupported()){
    Desktop desktop = Desktop.getDesktop();
    if (desktop.isSupported(Desktop.Action.PRINT))
    {
        try {
            File html1 = new File("c://file1.html");
            desktop.print(html1);
            desktop.print(html2);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

关于Ubuntu 中的 Java 打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10107862/

相关文章:

java - 使用 do-while 循环将用户输入添加到 ArrayList

java - 将 float 传递到仅接受 int 的构造函数 - 无法修改构造函数

java - 第二次传递给 java 中的函数时 FileInputStream 被关闭

Git克隆试图像对待目录一样对待文件?

docker - 允许 docker swarm 服务绑定(bind)主机 ip 接口(interface)

javascript - Angular js - 有条件地打印 View 数据

用于打印的 Python 字典键格式不适用于数字字符串

java - 正则表达式从字符串中查找特定模式字符串

Python子进程模块将路径作为字符串发送

C++ 程序显然打印内存地址而不是数组