Site iconJava PDF Blog

How to print PDF files from Java

Printing PDF files from Java is something that raises a lot of general questions, so this short article is a general guide to the options available. Java itself contains a built-in print system (JPS). JPS itself does not internally support the PDF file format.

There are 3 ways to print PDF files in Java:-

1. Use a printer that directly supports PDF files and use JPS to send the data directly to it.

All the work is done by the printer, often in hardware so this is a brilliant solution if you can precisely define the printers used but does not provide a generic solution. If you want to try this, here is some generic code

FileInputStream fis = new FileInputStream(“C:/mypdf.pdf”);

Doc pdfDoc = new SimpleDoc(fis, null, null);

DocPrintJob printJob = printService.createPrintJob();

printJob.print(pdfDoc, new HashPrintRequestAttributeSet());

fis.close();

2. Print from Java using a non-java application

Java allows you to access non-java code so that you can access Acrobat, Ghostscript, CUPS, or any other solution. You can do this with the Java command

Runtime.getRuntime().exec(“commands”);

Again, this works if you have control of the exact platforms and software available but does not provide a generic solution.

3. Print using JPS

JPS does not include PDF support, but it does have hooks to allow any Java program to print content to any printer. A number of Java PDF libraries offer printing – including ours! They essentially convert the PDF into a rendered page which JPS then prints. This provides a generic solution but the files tend to be larger and it relies on the capabilities of the Java PDF library which vary.

This is the solution we use in our JPedal Java PDF library. There is a detailed 5 step tutorial showing how you can print (which will be relevant to any library using JPS).

All three methods have their pros and cons so try them to find out which one offers the best fit for your requirements.

How to Print in Java using JPedal
Our JPedal Java PDF library implements JPS so here is the code you would use to print a PDF file.

  1. Create a DocPrintJob. In my code I just had to add
    PrintService[] service=PrinterJob.lookupPrintServices(); //list of printers
    
    printJob= service[i].createPrintJob(); //is whichever printer you use
  2. Wrap your Pageable or Printable object inside a doc, specifying which interface to use in the call
    Doc doc=new SimpleDoc(decode_pdf, DocFlavor.SERVICE_FORMATTED.PAGEABLE,null);
  3. Call the print method.
    printJob.print(doc,null);
  4. Finally, you can now add a listener to track what happens.
  5. Here is a simple example of the Listener class
    printJob.addPrintJobListener(new PDFPrintJobListener());
    
    private class PDFPrintJobListener implements PrintJobListener {
    public void printDataTransferCompleted(PrintJobEvent printJobEvent) {
    System.out.println("printDataTransferCompleted="+printJobEvent.toString());
    }
    
    public void printJobCompleted(PrintJobEvent printJobEvent) {
    System.out.println("printJobCompleted="+printJobEvent.toString());
    }
    
    public void printJobFailed(PrintJobEvent printJobEvent) {
    System.out.println("printJobEvent="+printJobEvent.toString());
    }
    
    public void printJobCanceled(PrintJobEvent printJobEvent) {
    System.out.println("printJobFailed="+printJobEvent.toString());
    }
    
    public void printJobNoMoreEvents(PrintJobEvent printJobEvent) {
    System.out.println("printJobNoMoreEvents="+printJobEvent.toString());
    }
    
    public void printJobRequiresAttention(PrintJobEvent printJobEvent) {
    System.out.println("printJobRequiresAttention="+printJobEvent.toString());
    }
    }
    

How do I print directly from a PDF?

  1. Open the PDF in your default reader, access the “Print” option via the file menu.
  2. In the print dialog, select your printer, adjust settings like number of copies, page range, orientation, papersize, and print quality.
  3. Preview the document, then click “Print” to start printing.
  4. Ensure your printer is on, connected, has enough paper and ink, and resolve any printer errors.

How do I print data from PDF?

There are many ways to do that. You can use JPedal, converting PDF to printable format and printing via  printJob.print(doc,null);