CUPS 2.0.2下用Cups4j无法获取纸张颜色,能否通过IPP协议获取?
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,
mediaSupportedused simple keyword values that combined size and color (e.g.,white_iso_b4_250x352mm), which Cups4j could parse easily. - In CUPS 2.x,
mediaSupportedusesmedia-colstructured 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
HttpURLConnectionor a library like OkHttp to POST to your printer's CUPS endpoint (e.g.,http://printer-ip:631/printers/your-printer). - Set the
Content-Typeheader toapplication/ipp. - Construct the binary IPP request payload (you can reference the IPP 2.0 specification for the exact format, or use the
ipptooloutput as a guide). - Parse the binary response to extract the
media-colstructures and theirmedia-colorsub-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-colattributes. - Cups4j's current implementation doesn't parse these structured attributes fully, so direct IPP access is the solution.
- Using
ipptoolfirst lets you confirm the data exists, then you can replicate the request in your Java application.
内容的提问来源于stack exchange,提问作者Bahnstreik

