Ubuntu中的Java打印 [英] Java Printing in Ubuntu

查看:136
本文介绍了Ubuntu中的Java打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm developing a small app to print data in Ubuntu,the problem is my app works fine in windows using:

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打印的特殊配置?或者我错过了其他什么?

However in Ubuntu, it just doesnt print. Is there any special config for Linux printing for the printing API I am using? Or am I missing something else?

推荐答案

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

I think, that your printer installed as not default in OS. Check what is your "service". Also you can choose printer from print dialog, like this:

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,只支持图像。您也可以使用以下操作系统本机方法打印html文件:

Some printers doesn't support text DocFlavors, only images. Also you can simply printing html files using OS native methods like this:

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打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆