Click here to Skip to main content
15,881,803 members
Articles / Operating Systems / Windows
Article

Creating PDF with nFOP

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


Written By
Software Developer (Senior) Freelance
France France
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionInputSource constructor err Pin
Member 125699229-Jun-16 8:31
Member 125699229-Jun-16 8:31 
QuestionError assembly Pin
Member 953878123-Oct-12 3:32
Member 953878123-Oct-12 3:32 
SuggestionEditors for FOP Pin
Member 827588227-Nov-11 0:49
Member 827588227-Nov-11 0:49 
GeneralMy vote of 5 Pin
vinayak.bhadage19-Apr-11 22:52
vinayak.bhadage19-Apr-11 22:52 
QuestionDriver.setRenderer(8); not working Pin
Uday_T24-May-10 13:43
Uday_T24-May-10 13:43 
GeneralSecured PDF Generation Pin
vinvino200122-Apr-10 21:32
vinvino200122-Apr-10 21:32 
GeneralRe: Secured PDF Generation Pin
WiseLearner11-Feb-13 8:04
WiseLearner11-Feb-13 8:04 
GeneralRe: Secured PDF Generation Pin
vinvino200111-Feb-13 17:48
vinvino200111-Feb-13 17:48 
QuestionDriver present in which library? ISO8859_1 error? Pin
Uday_T9-Feb-10 13:24
Uday_T9-Feb-10 13:24 
AnswerRe: Driver present in which library? ISO8859_1 error? Pin
Uday_T9-Feb-10 18:22
Uday_T9-Feb-10 18:22 
GeneralRe: Driver present in which library? ISO8859_1 error? Pin
Uday_T11-Feb-10 16:34
Uday_T11-Feb-10 16:34 
GeneralI am getting error like this Pin
Shaik Haneef14-Sep-09 20:41
Shaik Haneef14-Sep-09 20:41 
GeneralRe: I am getting error like this Pin
Kotty19856-Nov-09 4:51
Kotty19856-Nov-09 4:51 
GeneralIssue parsing out encoded html Pin
coolaid0925-Aug-09 8:51
coolaid0925-Aug-09 8:51 
Questionimages Pin
coolaid0920-Aug-09 9:34
coolaid0920-Aug-09 9:34 
AnswerRe: images Pin
Uday_T1-Mar-10 11:08
Uday_T1-Mar-10 11:08 
QuestionFop license? Pin
pereszczako8-Jun-09 22:39
pereszczako8-Jun-09 22:39 
QuestionBarcode fonts embedded using the userconfig.xml file Pin
djblues30-Apr-09 8:46
djblues30-Apr-09 8:46 
GeneralIssue with FOP Pin
sonu941-Dec-08 22:09
sonu941-Dec-08 22:09 
Hi,

This code is working fine in my local, but when i placed this code in my QA server it is giving this error.

Please help me in this regard.
================================================
Microsoft (R) Visual Basic Compiler version 8.0.50727.1433
for Microsoft (R) .NET Framework version 2.0.50727.1433
Copyright (c) Microsoft Corporation. All rights reserved.

C:\inetpub\wwwroot\CBIQuestionnaire\PDF\Default.aspx.vb(11) : warning BC40056: Namespace or type specified in the Imports 'org.apache.fop.apps' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases.

Imports org.apache.fop.apps
~~~~~~~~~~~~~~~~~~~
C:\inetpub\wwwroot\CBIQuestionnaire\PDF\Default.aspx.vb(13) : warning BC40056: Namespace or type specified in the Imports 'org.xml.sax' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases.

Imports org.xml.sax
~~~~~~~~~~~
C:\inetpub\wwwroot\Ire\PDF\Default.aspx.vb(17) : error BC30182: Type expected.

Dim obj_DA As New DataAccess
~~~~~~~~~~
C:\inetpub\wwwroot\Ire\PDF\Default.aspx.vb(399) : error BC30002: Type 'java.io.FileInputStream' is not defined.

Dim streamFO As New java.io.FileInputStream(FoFile)
~~~~~~~~~~~~~~~~~~~~~~~
C:\inetpub\wwwroot\Ire\PDF\Default.aspx.vb(400) : error BC30002: Type 'InputSource' is not defined.

Dim src As New InputSource(streamFO)
~~~~~~~~~~~
C:\inetpub\wwwroot\Ire\PDF\Default.aspx.vb(401) : error BC30002: Type 'java.io.FileOutputStream' is not defined.

Dim streamOut As New java.io.FileOutputStream(PDFFile)
~~~~~~~~~~~~~~~~~~~~~~~~
C:\inetpub\wwwroot\Ire\PDF\Default.aspx.vb(402) : error BC30002: Type 'Driver' is not defined.

Dim driver As New Driver(src, streamOut)
Generalreference-orientation does not work Pin
Member 212122031-Mar-08 9:14
Member 212122031-Mar-08 9:14 
QuestionLatin2 character set Pin
gabor10-Dec-07 23:41
gabor10-Dec-07 23:41 
QuestionBuild Pin
lokesh_sg7-Nov-07 0:23
lokesh_sg7-Nov-07 0:23 
QuestionHyphenation Pin
Daniele Fusi28-Mar-07 23:34
Daniele Fusi28-Mar-07 23:34 
GeneralJava does not work Pin
Member 385632223-Feb-07 5:42
Member 385632223-Feb-07 5:42 
GeneralRe: Java does not work Pin
Wingnut121327-Feb-07 8:02
Wingnut121327-Feb-07 8:02 

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.