Generating PDFs in Java web application
I have a requirement to generate a (dynamic) PDF after a user has made a payment and attach it to an email and send it in my Java web application.
I was thinking of using iText but I really don't like how all the formatting and layout gets mixed in with the content of the PDF, as per the example in this link: http://www.coderanch.com/how-to/java/ItextExample
The reason I don't like it being mixed together is because our UI guy won't be comfortable touching this Java code.
What other options do I have? I don't really want to generate an XML file with the required data and then have to transform it, so I think XSL-FO might 开发者_开发知识库be out.
I've used Jasper Reports for this on several projects and it works really well, pretty fast too when you compile the reports. The UI for creating reports isn't too bad although you'll need to be a little familiar with the data model. You can pass in anything you want as a datasource, including an XML file or a simple hashmap populated with your data:
http://jasperforge.org/website/jasperreportswebsite/trunk/documentation.html?header=project&target=jasperreports
Pseudocode
File myJasperFile // reference to compiled Jasper file
JRExporter exp = new JRPdfExporter()
ByteArrayOutputSream b;
JasperPrint p = JasperFillManager.fillReport(...)
exp.setParameter(JRExporterParameter.OUTPUT_STREAM, b)
exp.setParameter(JRExporterParameter.JASPER_PRINT, p)
exp.exportReport() // b now contains the PDF binary
There are a number of HTML->PDF options floating around (including iText). You could generate HTML from a template your UI guy creates, and convert that to PDF. Trivial.
If your HTML gets all fancy and scriptified, iText (and most other converters) won't handle it properly. For the advanced stuff, you'd need WKHTML2PDF, which is a webkit-based app that writes to PDF instead of the screen. It's SLICK (and a bit on the heavy-weight side). iText would be more efficient.
精彩评论