Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass data from java to cups filter

I'm working on the printing system, and need to add arbitrary text to each printed document (like author, document hash, some sysvars and else). We use Java Printing Service (javax.print) as printing client and Cups as server.

Cups has some procedures of document postprocessing called "filters".

Filter - is a program, that will be launched by Cups. Cups passes filter some params - "job attributes" among them.

So, I decided to pass custom attributes from java to cups and add attributes as text to document in filter. Everything works without exception, document is printed, but I don't get my attributes in filter. They are not even passed to Cups (saw that in packet sniffer).

I already used getSupportedAttributeCategories() to see the list of supported (by printer?) attributes. Maybe I should somehow add mine to that list, but I don't understand how.

I'm not limited to "attributes", I just need to pass arbitary data from java to Cups filter. How can I do it?

My java code is like:

MyAttrSet attrs = new MyAttrSet();
attrs.add(new MyAttr(42));
attrs.add(new Copies(18));  

PrintService service = PrintServiceLookup.lookupDefaultPrintService();
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
Doc doc = new SimpleDoc(is, flavor, null);
DocPrintJob job = service.createPrintJob();

job.print(doc, attrs);

Filter is a simple bash script, that prints everything passed to it:

#!/bin/bash
echo "All args:" > /tmp/f1.log
for var in "$@"
do
        echo "$var, " >> /tmp/f1.log
done

/tmp/f1.log looks like:

All args:
87, 
oroboros, 
 Java Printing, 
18, <- number of pages is passed! But not MyAttr
some useless crap like job uuid and else...

MyAttr:

class MyAttr extends IntegerSyntax implements PrintRequestAttribute {

    protected MyAttr(int value) {
        super(value);
    }

    public Class<? extends Attribute> getCategory() {
        // TODO Auto-generated method stub
        return MyAttr.class;
    }

    public String getName() {
        // TODO Auto-generated method stub
        return "somemycustop5";
    }

}
like image 251
Oroboros102 Avatar asked Nov 13 '12 15:11

Oroboros102


1 Answers

You can use cups4j

Map attributes = new HashMap();
attributes.put("job-attributes", "print-quality:enum:3#media:keyword:iso_a5_148x210mm");
printJob.setAttributes(attributes);

See the following article of how CUPS upgrade on Linux broke Java libraries and CUPS libraries: http://www.cups.org/articles.php?L433+TFAQ+Q

Now, since you can print I assume its not an issue. But I am not sure what MyAttrSet is, it should be a HashPrintRequestAttributeSet.

try:

PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(new Copies(2));

See if this attribute is read in, then try adding yours to such a set, does it work or not. You might want to try implementing other Attributes:

The Sides attribute looks like this:

public class Sides
  extends EnumSyntax
  implements DocAttribute, PrintRequestAttribute, PrintJobAttribute
  {
  public final Object getCategory()
    {
    return Sides.class;
    }
  ...
  }

Similarly Copies:

public final class Copies
extends IntegerSyntax
implements PrintRequestAttribute, PrintJobAttribute

perhaps try adding the additional implements of PrintJobAttribute

like image 120
msj121 Avatar answered Sep 30 '22 02:09

msj121