You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CUPS 2.0.2下用Cups4j无法获取纸张颜色,能否通过IPP协议获取?

Directly Using IPP Protocol to Fetch Paper Color in CUPS 2.0.2

Absolutely, you can bypass Cups4j's limitations and directly use the IPP (Internet Printing Protocol) to retrieve the paper color information you need. This is a reliable workaround because the CUPS 2.0.2 web interface clearly shows the color data—meaning the information is already exposed via IPP, but Cups4j isn't parsing it fully.

Why Cups4j Isn't Showing Paper Colors

The key difference between CUPS 1.2.4 and 2.0.2 here is how media attributes are structured:

  • In CUPS 1.2.4, mediaSupported used simple keyword values that combined size and color (e.g., white_iso_b4_250x352mm), which Cups4j could parse easily.
  • In CUPS 2.x, mediaSupported uses media-col structured attributes—each entry is a collection that splits out size, color, type, etc., into separate sub-attributes. Cups4j likely only extracts the size component from these collections, missing the color data.

How to Fetch Paper Colors via IPP

You'll need to send a Get-Printer-Attributes IPP request to your printer, then parse the structured media-col responses to extract the media-color sub-attribute. Here's how to do this practically:

1. Test with ipptool (CUPS Built-in Tool)

CUPS includes ipptool, which makes it easy to send raw IPP requests. Create a file named get-media-attributes.ipp with this content:

{
  VERSION 2.0
  OPERATION Get-Printer-Attributes
  GROUP operation-attributes-tag
  ATTR charset attributes-charset utf-8
  ATTR language attributes-natural-language en
  ATTR uri printer-uri uri:ipp://localhost:631/printers/[YOUR_PRINTER_NAME]
  GROUP printer-attributes-tag
  ATTR keyword requested-attributes mediaSupported
}

Run it with:

ipptool -t ipp://localhost:631/printers/[YOUR_PRINTER_NAME] get-media-attributes.ipp

In the output, look for entries like this:

mediaSupported: media-col (media-size=iso_b4_250x352mm, media-color=blue, media-type=plain)

The media-color value is exactly what you need.

2. Implement This in Java

Since you're using Java, you can send the IPP request directly via HTTP (IPP runs over HTTP/HTTPS) and parse the response. Here's a high-level approach:

  • Use HttpURLConnection or a library like OkHttp to POST to your printer's CUPS endpoint (e.g., http://printer-ip:631/printers/your-printer).
  • Set the Content-Type header to application/ipp.
  • Construct the binary IPP request payload (you can reference the IPP 2.0 specification for the exact format, or use the ipptool output as a guide).
  • Parse the binary response to extract the media-col structures and their media-color sub-attributes.

If building the binary IPP payload feels too low-level, you could also use a lightweight IPP client library that handles structured attribute parsing (avoiding Cups4j's limitations).

Key Takeaways

  • CUPS 2.0.2 exposes paper color data via IPP's structured media-col attributes.
  • Cups4j's current implementation doesn't parse these structured attributes fully, so direct IPP access is the solution.
  • Using ipptool first lets you confirm the data exists, then you can replicate the request in your Java application.

内容的提问来源于stack exchange,提问作者Bahnstreik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 09:12:02