views:

283

answers:

4

We have a number of systems that produce PDFs that need to be printed. These are stored on a central document store. A message then goes onto a JMS queue that the document needs printing. A service, written in Java , picks these up and then invokes a native command. This is to call Adobe Reader with the /t flag. This causes the document to print without the GUI showing.

However since a power cut this no longer works. In the interim we are having to manually print hundreds of documents. We originally tried using Java printing, but the PDFs came out malformed.

What is a better solution to this?

+1  A: 

Show us the code. I remember printing PDF with no issues using Java Print API. Below is the code, might need some modification, but should run as it is,

        InputStream in = new FileInputStream(file);
        DocFlavor flavor = DocFlavor.INPUT_STREAM.PDF;

        // find the printing service
        AttributeSet attributeSet = new HashAttributeSet();
        attributeSet.add(new PrinterName("FX", null));
        attributeSet.add(new Copies(1));

        PrintService[] services = PrintServiceLookup.lookupPrintServices(
                DocFlavor.INPUT_STREAM.PDF, attributeSet);

        //create document
        Doc doc = new SimpleDoc(in, flavor, null);

        // create the print job
        PrintService service = services[0];
        DocPrintJob job = service.createPrintJob();

        // monitor print job events
        PrintJobWatcher watcher = new PrintJobWatcher(job);

        System.out.println("Printing...");
        job.print(doc, null);

        // wait for the job to be done
        watcher.waitForDone();
        System.out.println("Job Completed!!");

Note:

  • Flavor is not needed in 2 places, 1 place should be enough. You find that out.
  • PrintJobWatcher is a nested class, to add a PrintJobListener.
Adeel Ansari
This is pretty much what we had. This works fine for simple layouts but anything more elaborate with differing fonts and layouts that it starts to fail. That's why we fell back to using Adobe Reader.
pledge
So, why not duplicate the service locally? Is there any problem with that.
Adeel Ansari
@Vinegar: This actually require the underlying (OS specific) printing system to support PDF and in case the OS printing support for PDF is limited, your code will fail. The code will not work at all under Windows and I suppose most Linux print spoolers are built around ghostscript, which has quite a few limitations in the PDF support.
jarnbjo
A: 

Try using ICEpdf. Here's an example

Lalith
+2  A: 

This code only works if the printer supports PDF. Otherwise you need to use a native printer or a Java library. There is a blog article on this at http://pdf.jpedal.org/java-pdf-blog/bid/25566/Printing-PDF-files-from-Java

mark stephens
Thanks. This has got it very close to working with option 1. The printer does say that it supports "Direct PDF Printing". It is a HP CM8050. Just got a couple of issues with fonts now, but no layout issues like before.
pledge
A: 

Despite the name, PDFNet SDK is also available for JAVA. There is an online sample showing how to print a PDF from JAVA: http://www.pdftron.com/pdfnet/samplecode.html#PDFPrint

Lary