Java Print Service Frustrations
Join the DZone community and get the full member experience.
Join For FreeThis is probably not the most exciting "toy" to play with for the
holidays, but I wanted to explore some use-cases that involve printing
through Java back-end services. Basically, in an enterprise environment
you may run into requirements, where the documents you receive or
generate also are required to be printed out for whatever reasons. In
all honesty, I never had to deal with Java's print Apis and thus started
from scratch.
It turns out, that there are 2 approaches (reminds me of the multitude
of Date implementations...). One approach dates back to the 1990s and is
called the Java 2 Print API and is tied more to User Interfaces (UI) triggering the printing process. With Java 1.4, the Java Print Service API (JPS) was introduced (JSR 6) which serves as a super-set of the the Java 2 Print Api.
The JPS Api is certainly workable but I ran into some serious trouble.
For my initial use-case, I may receive simple text documents, e.g. XML
that I want to send to a definable printer as plain text.
I was unable to get that to work on my Mac using Mac OS X 10.6.8.
However, on my Windows JVM it worked flawlessly and my text files were
printed out without any complaints. On my Mac, though, my little Java
test application finishes without any errors but my printer queue
states:
Error: pstopdffilter/pstocupsraster failed with err number -31000
Googling did not help much either. There seemed to be some anecdotal evidence that other people might have had trouble with text printing as well but I was unable to uncover definite answers (Many of the information is years old). The question I have is whether this is an issue with:
- My Printer, which is a Brother HL-4070CDW
- The Mac JVM (Mine is: build 1.6.0_29-b11-402-10M3527)
- CUPS
- something else
While searching for explanations, I stumbled across the following thread on stackoverflow.com: http://stackoverflow.com/questions/1655297/print-to-specific-printer-ipp-uri-in-java. There they mentioned the following CUPS specific libraries:
- CUPS4J (http://www.cups4j.org/)
- jipsi (http://sourceforge.net/projects/jipsi/)
As I need a generic solution that works not only on Mac using CUPS but also on Windows I have not experimented with any of the libraries above. Nevertheless I wanted to mention them.
I have also tried various other DocFlavors and settings but to no avail. What is interesting, though, is that the printing of PDF files works fine. Printing image files such as PNG or JPG fails as well.
Here is the trivial application that I am using:
Furthermore, I looked at the follwing Java-based Text editors to see how they accomplish printing (Which works fine on my Mac as well):
- RTEXT (http://rtext.fifesoft.com)
- jEdit (http://www.jedit.org)
From http://hillert.blogspot.com/2011/12/java-print-service-frustrations.html
Opinions expressed by DZone contributors are their own.
Comments