How to print a PDF file from Java


A customer of mine have a solution which creates Invoices in a PDF format.


They want a solution where for example twenty PDF Invoices are created, but they only want to print them not work with the PDF files themselves.

So a solution could be to create the PDF files and then send them to a printer in Java.

However since the PDF format is complicated stuff, so you really want use something that works no matter how complex the PDF file is.

That really rules out a lot of possibilities. There are java PDF librairies out there like Apache PDFBOX

but my advice is to use the PDF viewer "Sumatra PDF" as the solution. Forget about Adobe Reader if you are only viewing files, it is very bloated, go for the Sumatra PDF viewer.

I have used it for many years and it is really excellent.

If your users don't have Sumatra PDF installed, just use the Portable version instead.




To print is really easy via command line options :

This will print to the default printer.

String[] params = new String [3];
params[0] = "C:\\Program Files (x86)\\SumatraPDF\\SumatraPDF.exe";
params[1] = "-print-to-default";
params[2] = "c:\\test\\test.pdf";
Runtime.getRuntime().exec(params);

If you want a named printer use

params[1] = "-print-to <printer-name>"

A printer dialog

String[] params = new String [4];
params[0] = "C:\\Program Files (x86)\\SumatraPDF\\SumatraPDF.exe";
params[1] ="-print-dialog";
params[2]="-exit-when-done";
params[3] = "c:\\test\\test.pdf";
Runtime.getRuntime().exec(params);

See other command line options here https://github.com/sumatrapdfreader/sumatrapdf/wiki/Command-line-arguments


Posted on 04/28/2015 11:22:06 AM CEDT