如何在 Cups4J 中创建原始(-o 原始)PrintJob?

How to create a raw (-o raw) PrintJob in Cups4J?

目前我在使用 cups4j 时遇到了一些问题。例如,在 linux 下,您可以像这样在 cups 打印机上打印:

echo -e "test Text\r" | lp -o raw -h <IP-ADRESS> -d <PRINTER-NAME>

对我来说重要的是“-o raw”标志。

我的问题是在用 cups4j 打印时,我不知道如何设置这个标志。截取的下一个正常工作代码显示了我在 CupsPrinter 上打印 cups4j PrintJob 的方法。

private PrintRequestResult print(CupsPrinter printer, PrintJob job) throws Exception {
    return printer.print(job);
}

-o 原始选项是 described here pretty well:

The -o raw option allows you to send files directly to a printer without filtering. This is sometimes required when printing from applications that provide their own "printer drivers" for your printer:

就像我说的,打印本身就像一个魅力,但我不知道如何添加这个特定的标志。也许有人可以告诉我怎么做。

在分析了从终端和 java 程序通过 wireshark(我就是喜欢它)发送到 CUPS 的包后,我发现以下对我有用:

CupsClient cupsClient = new CupsClient(IP, port);
URL url = new URL(printerName);
CupsPrinter cupsPrinter = cupsClient.getPrinter(url);
HashMap<String, String> map = new HashMap<>();
map.put("document-format", "application/vnd.cups-raw");
PrintJob printJob = new PrintJob.Builder(bytes).attributes(map).build();
PrintRequestResult printRequestResult = cupsPrinter.print(printJob);