Printing PDF files in Java

A

Andrew

Does anyone have experience printing PDFs from within a Java program?

I naively thought that, given that Java and PDFs normally play so well
together, that this would be a very straight-forward thing to do. Certainly
there are lots of available tools to create, edit, and read PDFs in Java, but
printing support seems to be sadly lacking.

I've looked at the javax.print API, but my conclusion from all I have read on
the web/forums/mailing lists and that I have tried myself, it doesn't yet
support printing PDFs. (If this is no longer true, can someone please explain
how to achieve it?!). As an additional note, I'm looking ideally for a solution
that is cross-platform, but for now, I'd even settle for something that will
work on WinXP.

I've tried using an external call to Acrobat, but my installed version (6.0)
doesn't seem to support the command line switches that are mentioned in a few
places on the web.

As some context, I produce my PDF file using Apache FOP (start with an XML
file, use an XSLT stylesheet to create XHTML, then another stylesheet to
generate XSL:FO, then use FOP to generate my PDF file). This is all working
just fine. I can view and print my PDFs perfectly in Acrobat and Acrobat
Reader. I need the layout specification ability of XSL:FO and FOP, as well as
the intermediate formats for other reasons, so please don't tell me to use
iText/Jasper Reports/<insert other PDF generation tool here> instead of FOP. My
problem is with printing PDFs, not generating them. :)

In the absence of java PDF printing support, I'm currently using XPDF's pdftops
tool (making an external Runtime.exec call) to convert my PDF to PostScript,
and then printing the postscript file using the javax.print APIs.

This works OK, but not perfectly (I have some issues with page margins when
converting to postscript), and I feel like there MUST be a better way. I have
tried using FOP to produce PostScript from the XSL:FO files, but this doesn't
seem to work for my data (it just processes for ever without creating any
useful output).

I have also looked to some commercial tools - Smart JPrint almost works, but
does strange things with JPEG files (it inverts the colours, so that the images
are printed looking like photo negatives, rather than photo images). ActiveTree
(the makers of Smart JPrint) are currently looking into the issue to see
whether it's a specific problem with JPrint, or an issue with my PDFs.

Other commercial tools like JPedal are simply too expensive for my needs.

So, what I'm most interested in is: Has anyone found a good solution for
printing PDFs in Java. I need a solution that silently prints PDFs, without any
associated GUI components, buttons to click etc. that minimally works on WinXP,
but ideally would work anywhere Java does.

Thanks for any help you can offer! (Feel free to either post answers/clarifications here, or to send me email: andrew *at* bebox *dot* nu)

Cheers,
Andrew
 
M

Murray

Andrew said:
Does anyone have experience printing PDFs from within a Java program? ....

As some context, I produce my PDF file using Apache FOP (start with an XML
file, use an XSLT stylesheet to create XHTML, then another stylesheet to
generate XSL:FO, then use FOP to generate my PDF file).

....

Do you need:
a) the ability to print *any* PDF, e.g. you somehow provide a PDF file
to your program and it prints it, or
b) do you want to print the PDF documents that you're generating via
FOP?

If b), then couldn't you use the FOP AWTRenderer instead of PDFRenderer? It
implements Printable and Pageable so it should be easy to plug it into the
printing API.

If a) then the GPL jPedal might do the trick. Though you mention jPedal is
too expensive, so I assume GPL won't work for you ...
 
A

Andrew


Hi Murray,

Thanks for the reply.
Do you need:
a) the ability to print *any* PDF, e.g. you somehow provide a PDF file
to your program and it prints it, or

Ideally yes. For now, the PDFs are coming from FOP, but I'd like to be able to
print pre-existing PDF files too.
b) do you want to print the PDF documents that you're generating via
FOP?

Yes as well, and this would be a useful start.
If b), then couldn't you use the FOP AWTRenderer instead of PDFRenderer? It
implements Printable and Pageable so it should be easy to plug it into the
printing API.

I haven't tried the AWTRenderer yet. Do you have any feel for how mature it is?
If it's like the PDFRenderer, great, but if it's like the PS renderer, then i
doubt it's worth the investigation time (at this point in time - I'm really
looking forward to the day that active development on FOP becomes visible
again!)
If a) then the GPL jPedal might do the trick. Though you mention jPedal is
too expensive, so I assume GPL won't work for you ...

I've looked at the GPL version of jPedal, but the problem is that only the
enterprise version seems to support printing (at least that's what I understand
from reading their website: http://www.jpedal.org/versions.html

I must confess, though, that I haven't actually downloaded jPedal and tried for
myself. If anyone out there has experience and can tell me definitively whether
the GPL version supports PDF printing, please shout! :)

Thanks again for the suggestions, Murray.

Cheers,
Andrew
 
Joined
May 11, 2008
Messages
7
Reaction score
0
Pure Java Print PDF API

Recently Sun introduced a new open source called "PDF Renderer". Using which you can print your PDF documents. Find sample code here:
venkatsadasivam.wordpress.com/2008/11/03/java-print-pdf/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top