4

I need to print PDF file using Java printing API. I understand that if I need to print formats that not supported by printer natively, I need to use 2DGraphics etc.. But if printer supports format natively, so I don't need to use 2DGraphics. I need simply send file to print:

Doc myDoc = new SimpleDoc(stream, myFormat, dset);
...
job.print(myDoc, aset);

So when I print using Java print API I get exception: sun.print.PrintJobFlavorException: invalid flavor But when I print using Adobe Reader all prints FINE. Why so?

2
  • You could use PdfRenderer, I used it and was successful in printing a pdf file. juixe.com/techknow/index.php/2008/01/17/…
    – likeToCode
    Aug 20, 2012 at 12:44
  • @likeToCode it buggy. It require jbig2 decoder, but jbig2 decoder is very buggy (free version of it, I don't try not free version (2.0))
    – WelcomeTo
    Aug 21, 2012 at 5:11

1 Answer 1

0

Obviously the PDF is not a native format. It might very well be that PostScript would be covered, and PDF has its ancient roots in PostScript. So there might lie the misconception.

You can let the for PDF registered application (Adobe Reader?) print (or open) the PDF file. This is almost a one-liner.

public static void print(File file) {
    if (!Desktop.isDesktopSupported()) {
        throw new IllegalStateException(
                "Must not be run in headless server mode.");
    }
    try {
        Desktop.getDesktop().print(file);
    } catch (UnsupportedOperationException e) {
        throw new IllegalStateException(
                "Please install a PDF reader.");
    }
}

Pre-requisite is that there must be a PDF application installed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.