Question

We are looking into silent printing of PDF documents from within Java. The printing will be invoked from the desktop and not through a browser so we cannot use JavaScript. PDF Renderer is an operational solution but their rendering quality is not acceptable. iText does not seem to be pluggable with the Java print service. There are some commercial Java libraries, jPDFPrint by Qoppa, JPedal, and ICEpdf which we have not tried out yet.

Does anybody have any experience with PDF silent printing from Java?

Was it helpful?

Solution

Apache PDFBox. It is currently in incubation, but the PDF printing functionality has been around before that. Internally, it uses the Java Print Services to create a print job, and it also supports silent printing.

Do note that it requires Fontbox as well, and the current (upcoming 0.8.0 release) has included graceful fallback for documents with Type 0 fonts. Type 1 fonts are printed correctly; however in 0.7.3, attempts to print documents with Type 0 fonts will result in an exception being thrown.

OTHER TIPS

Maybe I'm misunderstanding, but why not just use the Print Service API directly? The following works for me (assumes you have the PDF document as a byte array):

DocFlavor flavor = DocFlavor.BYTE_ARRAY.PDF;
PrintService[] services = PrintServiceLookup.lookupPrintServices(flavor, null);
if (services.length > 0)
{
    DocPrintJob printJob = services[0].createPrintJob();
    Doc document = new SimpleDoc(pdfBytes, flavor, null)
    printJob.print(document, null);
}
else
{
    System.out.println("No PDF printer available.");
}

This works for me:

public void print() {
    DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
    PrintService[] services = PrintServiceLookup.lookupPrintServices(flavor, null);
    FileInputStream psStream = null;  
    try {  
        psStream = new FileInputStream("c:\\test.pdf");  
        } catch (FileNotFoundException ffne) {  
          ffne.printStackTrace();  
        }  
    if (psStream == null) {  
        return;  
    }       
    if (services.length > 0)
    {
        PrintService myService = null;
        for(PrintService service : services) {
            System.out.println(service.getName());
            if(service.getName().contains("my printer")) {
                myService = service;
                break;
            }
        }
        DocPrintJob printJob = myService.createPrintJob();
        Doc document = new SimpleDoc(psStream, flavor, null);
        try {
            printJob.print(document, null);
        } catch (PrintException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    else
    {
        System.out.println("No PDF printer available.");
    }       
}

Have a look at www.pdflib.com. Its comercial but PDFlib Lite is available for free for open source projects. It has bindings for java.

There is an example using JPedal at http://www.jpedal.org/support_egSP.php

You will need the commercial version of IcePdf if you want full font support.

I have experience with making Acrobat (Reader or Full) do the printing, but it's anything but silent (it is unattended, though - just depends on how 'silent' the silent requirement is). If there's interest, I can shoot you the native code that makes the required DDE calls.

iText is intended for creating PDF files (per a post I saw from the author), and thus probably isn't what you want.

I've used Qoppa's jPDFPrint quite successfully for exactly this purpose, but it's not cheap. If you can afford it, it's the most robust solution I've found thus far. I've also been very impressed with the level of support; they even generated some custom sample code for me.

I tried PDFBox, but found that it doesn't support the "Shrink to printable area" page scaling that you get with Acrobat. Not everyone will care about this feature, but it's essential for me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top