-2

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 be 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.

bluish
  • 26,356
  • 27
  • 122
  • 180
Venkat Sadasivam
  • 1,435
  • 5
  • 24
  • 42
  • About "CSS+XHTML to PDF" technologies, see [Why use XSL-FO instead of CSS2, for transform HTML into good PDF?](http://stackoverflow.com/q/10641667/287948) question and answers. – Peter Krauss Jul 26 '12 at 19:29

5 Answers5

10

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).

Apache FOP can use a lot of memory for large documents (e.g., a 200-page PDF), which may require tweaking the JVM memory settings.

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
4

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.
  • 7,625
  • 4
  • 27
  • 24
3

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"

mohdajami
  • 9,604
  • 3
  • 32
  • 53
1

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
  • 73,784
  • 33
  • 194
  • 347
0

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.)