Click here to Skip to main content
Licence CPOL
First Posted 11 Jan 2006
Views 110,898
Downloads 1,160
Bookmarked 45 times

Creating PDF with nFOP

By | 11 Jan 2006 | Article
How to use generate PDF files on the fly using nFOP

Introduction

nFOP is an elegant solution to generate PDF on the fly. It is originally a Java open source product that has been migrated to .NET.

The purpose of this article is to show the use of nFOP to generate a PDF file from XML data. We won't describe the FO format here. Many excellent articles exist on the Web about FO which is a W3C standard. Unfortunately it doesn't seem to be much known by the .NET community so far.

First, you have to download nFOP from here.

Using the Code

Here is the data in input - my favourite French books:

Books.xml

<?xml version="1.0" encoding="utf-8" ?>
<Books>
  <Book>
    <Title>Madame Bovary</Title>
    <Author>Gustave Flaubert</Author>
    <Price>10</Price>
  </Book>
  <Book>
    <Title>Bel Ami</Title>
    <Author>Maupassant</Author>
    <Price>18</Price>
  </Book>
  <Book>
    <Title>La Curée</Title>
    <Author>Emile Zola</Author>
    <Price>12</Price>
  </Book>
  <Book>
    <Title>Les Chouans</Title>
    <Author>Honoré de Balzac</Author>
    <Price>15</Price>
  </Book>
</Books>

Here is now the XSL transformation that I will apply to this XML. I create a table in FO language, with three columns. The rows will be generated by the XSL transformation, one for each book.

BookFo.xsl

<fo:table-body>
      <fo:table-row>
        <fo:table-cell>
          <fo:block>Title</fo:block>
        </fo:table-cell>
        <fo:table-cell>
          <fo:block>Author</fo:block>
        </fo:table-cell>
        <fo:table-cell>
          <fo:block>Price</fo:block>
        </fo:table-cell>
      </fo:table-row>
      <xsl:for-each select="Books/Book">
        <fo:table-row>
          <fo:table-cell>
            <fo:block>
              <xsl:value-of select=".//Title" />
            </fo:block>
          </fo:table-cell>
          <fo:table-cell>
            <fo:block>
              <xsl:value-of select=".//Author" />
            </fo:block>
          </fo:table-cell>
          <fo:table-cell>
            <fo:block>
              <xsl:value-of select=".//Price" />
            </fo:block>
          </fo:table-cell>
        </fo:table-row>
      </xsl:for-each>
    </fo:table-body>
  </fo:table>

In Visual Studio, reference the nFop assembly ApacheFop.Net.dll and the J# assembly vjslib.dll.

You will need to add the following namespaces to your class:

    using System.Xml;
    using System.Xml.Xsl;
    using System.Xml.XPath;
    using org.apache.fop;
    using org.apache.fop.apps;
    using org.apache.fop.tools;
    using org.xml.sax;
    using java.io;

We call the classic XML/XSL transformation to produce the FO file:

    // Load the FO style sheet.
    XslCompiledTransform xslt = new XslCompiledTransform();
    xslt.Load("bookFo.xsl");

    // Execute the transform and output the results to a file.
    xslt.Transform("books.xml", "books.fo");

At last, here comes the most interesting part, the use of NFOP to generate the PDF file.

Here is the method that does the job:
    private void GeneratePDF(string foFile, string pdfFile)
    {
        FileInputStream streamFO = new FileInputStream(foFile);
        InputSource src = new InputSource(streamFO);
        FileOutputStream streamOut = new FileOutputStream(pdfFile);
        Driver driver = new Driver(src, streamOut);
        driver.setRenderer(1);
        driver.run();
        streamOut.close();
    }

Conclusion

nFOP is an easy solution to implement and it is free. Because the PDF structure is described in an XML file, it is easy to configure its disposition, which clients always ask for.

To learn more about FOP, visit the Apache site and download the Java version. It contains plenty of samples of FO files and good documentation.

History

  • 11th January, 2006: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Jerome Bellanger

Software Developer (Senior)
Freelance
France France

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
SuggestionEditors for FOP PinmemberMember 82758820:49 27 Nov '11  
GeneralMy vote of 5 Pinmembervinayak.bhadage22:52 19 Apr '11  
QuestionDriver.setRenderer(8); not working Pinmemberpricksaw13:43 24 May '10  
GeneralSecured PDF Generation Pinmembervinvino200121:32 22 Apr '10  
QuestionDriver present in which library? ISO8859_1 error? Pinmemberpricksaw13:24 9 Feb '10  
AnswerRe: Driver present in which library? ISO8859_1 error? Pinmemberpricksaw18:22 9 Feb '10  
GeneralRe: Driver present in which library? ISO8859_1 error? Pinmemberpricksaw16:34 11 Feb '10  
GeneralI am getting error like this PinmemberShaik Haneef20:41 14 Sep '09  
GeneralRe: I am getting error like this PinmemberKotty19854:51 6 Nov '09  
GeneralIssue parsing out encoded html Pinmembercoolaid098:51 25 Aug '09  
Questionimages Pinmembercoolaid099:34 20 Aug '09  
AnswerRe: images Pinmemberpricksaw11:08 1 Mar '10  
QuestionFop license? Pinmemberpereszczako22:39 8 Jun '09  
QuestionBarcode fonts embedded using the userconfig.xml file Pinmemberdjblues8:46 30 Apr '09  
GeneralIssue with FOP Pinmembersonu9422:09 1 Dec '08  
Generalreference-orientation does not work PinmemberMember 21212209:14 31 Mar '08  
QuestionLatin2 character set PinsussGabor23:41 10 Dec '07  
QuestionBuild Pinmemberlokesh_sg0:23 7 Nov '07  
QuestionHyphenation PinmemberDaniele Fusi23:34 28 Mar '07  
Thanks for the article, it really helps FO-newbies like me starting... I have a question about hyphenation: how can I use it in NFOP? The binaries I download from sourceforge just include a couple of DLL's, and if I try to generate PDF from a block like:
 
<fo:block hyphenate="true" language="en">...long text...</fo:block>
 
I get multiple warnings from the processor like:
...
Using org.apache.xerces.parsers.SAXParser as SAX2 Parser
building formatting object tree
setting up fonts
[1]
Couldn't find hyphenation pattern en
Error building hyphenation tree for language en
...

GeneralJava does not work PinmemberMember #38563225:42 23 Feb '07  
GeneralRe: Java does not work PinmemberWingnut12138:02 27 Feb '07  
QuestionLarge FO files take very long time to process PinmemberDadou00219:45 20 Nov '06  
Generalunable to use XslCompiledTransform .NET1.1 Pinmembernewbie137:26 10 Oct '06  
AnswerRe: unable to use XslCompiledTransform .NET1.1 PinmemberSid DeLuca6:04 25 Jan '07  
GeneralFop and arabic support Pinmemberssam7:34 14 Feb '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 11 Jan 2006
Article Copyright 2006 by Jerome Bellanger
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid