tags:

views:

604

answers:

6

Are there any API/solution to generate PDF report from XML file data and definition. For example the XML definition/data could be:

<pdf>
    <paragraph font="Arial">Title of report</paragraph>
</pdf>

converting HTML to PDF will also a good solution I feel.

Currently we write Java code using iText API. I want to externalize the code so that non-technical person can edit and make changes.

+4  A: 

Have a look at Apache FOP. Use an XSLT stylesheet to convert the XML (or XHTML) into XSL-FO. Then use FOP to read the XSL-FO document and format it to a PDF document (see Hello World with FOP).

Pascal Thivent
FOP works well, as long as you aren't trying to create a 200 page pdf, as you will run into memory problems.
James Black
See http://xmlgraphics.apache.org/fop/0.94/running.html#memory about FOP and Memory.
Pascal Thivent
Please see my answer for a suggestion for "an XSLT style sheet".
Thorbjørn Ravn Andersen
A: 

Prince is one of the best tools out there. It uses CSS for styles, so if you appreciate that method of separating data from display (read: your users are able to do that too), it may be a very good fit for you. (The control over display that browsers offer through CSS is, by comparision, primitive.)

Roger Pate
+2  A: 

iText has a facility for generating PDFs from XML (and HTML, I think). Here is the DTD, but I found it difficult to sort out. Aside from that, I never found any good documentation on what is supported. My approach was to look at the source for SAXiTextHandler and ElementTags to figure out what was acceptable. Although not ideal, it is pretty straight-forward.

<itext orientation="portrait" pagesize="LETTER" top="36" bottom="36" left="36" right="36" title="My Example" subject="My Subject" author="Me">
<paragraph size="8" >This is an example</paragraph>
</itext>

...

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.xml.SAXiTextHandler;

...

String inXml = ""; //use xml above as an example
ByteArrayOutputStream temp = new ByteArrayOutputStream();
Document document = new Document();
PdfWriter writer = null;
try
{
    writer = PdfWriter.getInstance(document, temp);
    SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
    parser.parse(new ByteArrayInputStream(inXml), new SAXiTextHandler(document));
}
catch (Exception e)
{
    // instead, catch the proper exception and do something meaningful
    e.printStackTrace();
}
finally
{
    if (writer != null)
    {
     try
     {
      writer.close();
     }
     catch (Exception ignore)
     {
      // ignore
     }
    } // if
}

//temp holds the PDF
jt
A: 

Take a look at JasperReports, it uses iText to export files i think, and its IDE is simple and can be used by non-programmers.

Edit: i forgot to mention, you can use JasperReports engine directly in your application, or you can use iReport "Designer for JasperReports"

medopal
+1  A: 

You will want to use a well supported XML format for this, as it will allow you to leverage the work of others.

A well supported XML format is DocBook XML - http://www.docbook.org/ - and this - http://sagehill.net/docbookxsl/index.html - appears to be a good resource on doing the XML -> PDF using XSLT with the Docbook style sheets and other formats.

This approach allows you to use any XSLT processor and any XSL/FO processor to get to your result. This gives you easy scriptability as well as the freedom to switch implementations if needed - notably older Apache FOP implementations degraded badly when the resulting PDF got "too large".

Thorbjørn Ravn Andersen
A: 

Our tool takes any HTML/CSS page and returns it as a PDF. Might be useful? It's a very simple and quick solution. You can try it from the website to see if it handles your page.

http://fourpdf.com/

Regards, Jake.

Jake Liddell