Accessing the Java Printing API using JRuby
If you ever have to print a document from a Ruby app you might want to consider using JRuby to access theĀ Java Printing API.Here is a small example accessing the API, looking up the printers and printing out their names to the console:
require 'java'
flavor = javax.print.DocFlavor::INPUT_STREAM::TEXT_PLAIN_HOST
aset = javax.print.attribute.HashPrintRequestAttributeSet.new
aset.add(javax.print.attribute.standard.MediaSizeName::ISO_A4)
aset.add(javax.print.attribute.standard.Copies.new(1))
services = javax.print.PrintServiceLookup.lookupPrintServices(flavor, aset);
puts("Anzahl der Drucker: " + services.length.to_s)
services.each do |service|
puts service.name
end
The output looks like this:
Anzahl der Drucker: 1HP Photosmart 8000 series