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

查看:41
本文介绍了在 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.

对于背景,我的 web 应用程序需要根据当前用户将条形码打印到特定打印机.每个用户都与一个打印机 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);

我不得不承认我很失望不得不使用 3rd 方库来做一些看似简单的事情,比如打印到特定的打印机.

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 有一个新的 home 和一个新名称.

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

Cups4J 是一个不错的选择,但顾名思义,如果目的地不正确,它可能无法正常工作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天全站免登陆