Click here to Skip to main content
14,867,685 members
Articles / Operating Systems / Windows
Article
Posted 11 Jan 2006

Stats

191K views
3.2K downloads
48 bookmarked

Creating PDF with nFOP

Rate me:
Please Sign up or sign in to vote.
4.27/5 (12 votes)
11 Jan 2006CPOL1 min read
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
<?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

XML
<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:

C#
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:

C#
// 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:
C#
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)

Share

About the Author

Jerome Bellanger
Software Developer (Senior) Freelance
France France
No Biography provided

Comments and Discussions

 
QuestionLarge FO files take very long time to process Pin
Dadou00220-Nov-06 19:45
MemberDadou00220-Nov-06 19:45 
Generalunable to use XslCompiledTransform .NET1.1 Pin
newbie1310-Oct-06 7:26
Membernewbie1310-Oct-06 7:26 
AnswerRe: unable to use XslCompiledTransform .NET1.1 Pin
Sid DeLuca25-Jan-07 6:04
MemberSid DeLuca25-Jan-07 6:04 
GeneralFop and arabic support Pin
ssam14-Feb-06 7:34
Memberssam14-Feb-06 7:34 
GeneralRe: Fop and arabic support Pin
ssam14-Feb-06 23:07
Memberssam14-Feb-06 23:07 
GeneralRe: Fop and arabic support Pin
Sohid6-Mar-07 3:00
MemberSohid6-Mar-07 3:00 
GeneralRe: Fop and arabic support Pin
ssam6-Mar-07 22:13
Memberssam6-Mar-07 22:13 
GeneralRe: Fop and arabic support Pin
MystiqueMan17-Mar-07 15:15
MemberMystiqueMan17-Mar-07 15:15 
GeneralRe: Fop and arabic support Pin
ssam15-Jun-07 0:30
Memberssam15-Jun-07 0:30 
QuestionFO-Editor? Pin
Thomas M.18-Jan-06 1:11
MemberThomas M.18-Jan-06 1:11 
AnswerRe: FO-Editor? Pin
gsaielli20-Jan-06 2:23
Membergsaielli20-Jan-06 2:23 
GeneralRe: FO-Editor? Pin
Thomas M.20-Jan-06 2:43
MemberThomas M.20-Jan-06 2:43 
GeneralRe: FO-Editor? Pin
Jaro30-Jan-06 9:12
MemberJaro30-Jan-06 9:12 
Generalreport viewer Pin
ignatandrei13-Jan-06 4:35
professionalignatandrei13-Jan-06 4:35 
Generaluncomplete FOP portage Pin
bxb12-Jan-06 0:09
Memberbxb12-Jan-06 0:09 
GeneralRe: uncomplete FOP portage Pin
Jerome Bellanger13-Jan-06 8:20
MemberJerome Bellanger13-Jan-06 8:20 

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

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