Printing 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 which 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 PDF library which vary.

This is the solution we use in our JPedal PDF library. There is a detailed 5 step tutorial showing how you can print (which will be relevant to any library using JPS at ), and more details about printing here.

All three methods have their pros and cons so try them to find out which one offers the best fit for your requirements. Try and see what meets your needs best. If you would like to see a more detailled article please let us know or post your comments here.

The JPedal Java PDF library is actively developed and can be used for printing, viewing, conversion, search, extraction and lots of other purposes. If you would like to learn more about the JPedal PDF Library click here.

This post is part of our “Printing Articles Index” in these articles, we aim to help you understand printing in Java and PDF’s.

The following two tabs change content below.

Mark Stephens

System Architect and Lead Developer at IDRSolutions
Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX. He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.

Related Posts:

Markee174

About Mark Stephens

Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX. He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.

3 thoughts on “Printing PDF files from Java

  1. Eric Shi

    Dear Jpedal supports:
    Nice to know you and your product, as i read an article from your pdfblog about
    there are 3 ways to print pdf files

    1. Use a printer which 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();

    fist question:
    we have an oce brand cutsheet printer , i think it can directly support pdf printing because this printer has PostScript License and PCL license, and i can use windows lpr command to send a postScript to this printer to print so is this mean this printer directly support pdf printing? if no how i can make sure my printer directly support pdf print.

    second question:
    if my printer directly support pdf printing is that mean i can use this script to print pdf?

    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();

    if yes, i write a small script to send pdf to my printer as fellows:

    File file=new File(“D:\\t22.pdf”);
    PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
    DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
    InputStream in= new FileInputStream(file);
    PrintService printService1[]= PrintServiceLookup.lookupPrintServices(flavor, pras);
    PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
    PrintService service = ServiceUI.printDialog(null, 200, 200,printService1, defaultService, flavor, pras);

    pras.add(new Copies(3));
    pras.add(PrintQuality.HIGH);
    pras.add(Sides.DUPLEX);
    pras.add(new JobName(“E2″,null));
    Doc doc1=new SimpleDoc(in,flavor,null);
    pj.print(doc1, pras);
    in.close();

    i can send the pdf to the printer and print out , but the PrintRequestAttribute seems not work, i set 3 copies but just print 1 copy and i set duplex but always simplex but the printer can get the JobName “E2″, so i don’t know why, what’s maybe the reason?

    third question:
    as said from your blog “Use a printer which directly supports PDF files and use JPS to send the data directly to it. All the work is done by the printer” say “all work” , what work is done by the printer?
    ————————————————
    4 question:
    i installed the oce printer driver to send files, but in oce printer driver all print job are need give a mediaName for example “t3″ , and in the printer there are 4 trays,and for example the first tray are assigned mediaName “t1″ and second tray are assigned mediaName “t2″
    the thrid tray are assigned mediaName “t3″ the fourth tray is assigned mediaName “t4″ , so if i send a file with printer driver and give mediaName “t3″, the job will print with tray 3, as i found “javax.print.attribute.standard.MediaName” can this class
    meet this requirement?

    hope you can give me some help. it would be better if you can reply to my email address

    Best Regards
    Eric

  2. You will need to check with the manufacturer what support they actually have.

    • Eric Shi

      Dear mark Stephens:
      First thanks your reply.
      from your reply said i need check with printer manufacturer right?
      And is it means that even thougth i do the DocAttribute , printJobAttribute, and printRequestAttribute settings like the copies , mediaName , mediaSize , all these methods maybe not valid for some brand printer drivers right?
      Thanks
      Eric

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>