用Java打印到特定打印机(IPP URI) [英] Print to specific printer (IPP URI) in Java

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

问题描述

Java中有没有办法打印到特定的IPP打印机?我发现的所有示例代码和教程都集中在如何使用以下内容打印特定类型的文档:

Is there any way in Java to print to a specific IPP printer? All of the sample code and tutorials I've found focus on how to print a particular type of document, using something like the following:

DocFlavor flavor = DocFlavor.INPUT_STREAM.POSTSCRIPT;
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(MediaSizeName.ISO_A4);
PrintService[] pservices =
             PrintServiceLookup.lookupPrintServices(flavor, aset);
if (pservices.length > 0) {
    DocPrintJob pj = pservices[0].createPrintJob();
    try {
        FileInputStream fis = new FileInputStream("test.ps");
        Doc doc = new SimpleDoc(fis, flavor, null);
        pj.print(doc, aset);
    } catch (FileNotFoundException fe) {
    } catch (PrintException e) { 
    }
}

此代码段只打印到找到的能够打印文档的第一台打印机。在我的情况下,我想通过其URI查找打印机,但 PrintServiceLookup 似乎不支持这一点。我尝试使用 PrintServiceAttributeSet ,而不是 PrintRequestAttributeSet ,并添加 PrinterURI 属性,但不返回任何打印机。我怀疑查找服务正在寻找可以更改其目标URI的打印机,而不是查找具有该URI的打印机。

This snippet simply prints to the first printer found that is capable of printing the document. In my case, I want to lookup a printer by its URI, but PrintServiceLookup doesn't seem to support this. I've tried using a PrintServiceAttributeSet, instead of PrintRequestAttributeSet, and adding a PrinterURI attribute, but that doesn't return any printers. I suspect the lookup service is looking for a printer that can change its destination URI, rather than looking for the printer with that URI.

作为最后的手段,我想到了只是枚举 lookupPrintServices 返回的所有 PrintService ,但URI不在任何属性中。打印机名称在那里,但我需要URI。

As a last resort, I thought about just enumerating through all of the PrintServices returned by lookupPrintServices, but the URI is not in any of the attributes. The printer name is there, but I need the URI.

对于后台,我的webapp需要根据当前用户将条形码打印到特定的打印机。每个用户都与一个打印机URI相关联,该URI指向CUPS服务器上的打印机。打印机URI是我拥有的唯一信息,我不能限制打印机名称以匹配URI或URI的子字符串。

For background, my webapp needs to print a barcode to a specific printer, based on the current user. Each user is associated with a printer URI, which points to a printer on a CUPS server. The printer URI is the only information I have, and I can't constrain the printer name to match the URI or a substring of the URI.

编辑: 为了澄清一点,我不需要渲染数据,我只需要将blob复制到给定的打印机。我无法弄清楚的部分是如何通过其IPP URI识别打印机。

To clarify a bit, I don't need to render the data, I just need to copy a blob to a given printer. The part I can't figure out is how to identify a printer by its IPP URI.

推荐答案

我终于找到了一条路通过使用 jipsi 执行此操作:

I finally found a way to do this, by using jipsi:

URI printerURI = new URI("ipp://SERVER:631/printers/PRINTER_NAME");
IppPrintService svc = new IppPrintService(printerURI);
InputStream stream = new BufferedInputStream(new FileInputStream("image.epl"));
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
Doc myDoc = new SimpleDoc(stream, flavor, null);
DocPrintJob job = svc.createPrintJob();
job.print(myDoc, null);

我不得不承认我对使用第三方库做某事感到很失望打印到特定打印机看似简单。

I have to admit I'm disappointed at having to use a 3rd-party library to do something so seemingly simple as printing to a specific printer.

更新

DR指出在jipsi的评论中新的主页和新名称。

DR points out in the comments that jipsi has a new home, and a new name.

Cups4J 是一个不错的选择,但顾名思义它可能无法正常工作,如果destination不是CUPS服务器。我使用Cups4J直接打印到Zebra热敏打印机上取得了不错的效果。

Cups4J is a nice alternative, but as the name implies it may not work correctly if the destination is not a CUPS server. I have had good results using Cups4J to print directly to a Zebra thermal printer.

这篇关于用Java打印到特定打印机(IPP URI)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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