Geeks With Blogs

News
Ivan Porto Carrero Placeholder.Add("Really Cool Stuff");

Every application needs reporting, my favorite is pdf. Virtually everybody has a pdf reader on his pc and if not they should get one ;-).
Getting reporting done in pdf is cool and VS ships with Crystal reports (let's call it the lite version). This crystal reports has some shortcomings i.e. no external images, no complex data etc.  Since I need at least something that inserts images i am lost.  I could do reporting in html but then people will be sending invoices with the header and footer well known by everybody. My clients didn't want this... Since I am not a rich man, nor were these clients the pdf generator needed to be FREE and fairly easy to create reports with since I don't want to spend days figuring out how to make a table.

The first thing you need is the right software :

After looking at a lot of different pdf libraries, most of them can be found on the sourceforge site.

These guys have done an extensive examination of pdf components : http://www.howtoselectguides.com/dotnet/pdf/

The best of them I found :

iTextSharp - an opensource pdf library
For the itextsharp you will need the zip library : Sharp Zip library
I couldn't get this stuff to work, for me it always gave an error on the zip library.. Although I use the same library for zipping my pages before they are sent.

Then on the codeproject there is the GIOS pdf project. It looks all very promissing but it didn't have the flexibility I wanted.
GIOS Pdf Library

So my search continued and I came on to the pdf creator library. Very nice especially when you are writing a winforms application. But in my case it was a web form application.
Again, no go....
Back to the google board. And the next one that came out Report.NET. Also a very cool program but the lay-out building was a drag so I also didn't like it so much because in meantime I was reading about xml and xsl-fo for transforming any xml-datasource into a variety of documents. Yess this is it .. Highly flexible and i can easily generate pdf with this.. no more worries on my side. I thought.

Apache has a Apache Formation Objects Processor (FOP) which is for free, and open source so I could learn from this. This was fitting my needs exactly.. If only they would have this for .NET..
A little googling and yes : NFOP pops up. A .NET port of the java application from apache. NFOP can be downloaded here

Up to now I was never using xml.. so it took me some time to get the lingo down but after an afternoon of swearing and trying I started to get the hang of it.

Some links to get you started with this xslt :
XSLT Stylesheets
W3-Schools xsl-fo tutorial
RenderX xsl-fo tutorial
Xml.com xsl-fo tutorial

Using NFOP in .NET :

What you need basically is a well-formatted xml document and a well formatted xslt stylesheet to create a good xsl-fo document.  This document you run through the pdf engine and voilà a nicely formatted pdf.

Step one :
Create the xml and xslt documents.  These I won't provide here because i just don't have any simple examples at hand for the moment.

Step two:
Transform the xml and xslt to xsl-fo document.

public static string getTransform(XmlDataDocument doc, string xslFilePath)
{
      XslTransform tr =
new XslTransform();
      XmlUrlResolver res =
new XmlUrlResolver();
      res.Credentials = System.Net.CredentialCache.DefaultCredentials;
      System.IO.
StringWriter sw = new System.IO.StringWriter();
      tr.Load(xslFilePath,res);
      tr.Transform(doc.CreateNavigator(),
null,sw,res) ;
      return sw.ToString();
}

Step three :
Run the transformation through the nfop engine.

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

    public void SavePdfFromFo(System.Web.UI.Page page,string fullfodoc)

            {                

                  InputSource source = new InputSource(new java.io.StringReader(fullfodoc));

                  ByteArrayOutputStream output = new ByteArrayOutputStream();

                  try

                  {

                        Driver driver = new Driver(source, output);

                        driver.setRenderer(Driver.RENDER_PDF);

                        driver.run();

                        output.close();

                        int sz = output.buf.Length;

                        byte[] pdf = new byte[sz];

                        for (int i = 0; i < sz; i++)

                              pdf[i] = (byte)output.buf[i];

                        page.Response.ClearHeaders();

                        page.Response.Clear();

 

                        page.Response.AddHeader("Content-Disposition", "attachment; filename=myNewPdf.pdf");

                        page.Response.ContentType = "application/octet-stream";

 

                        page.Response.Flush();

                        page.Response.BinaryWrite(pdf);

                        page.Response.End();

                  }

                  catch (FOPException fope)

                  {

 

                  }

            }

 

Below you can find a sample xml document and xslt document. This will screw up the layout of the article so I apologise for the strange layout.

 

xml version="1.0" standalone="yes"?>
<
dsWolfSeco xmlns="http://www.tempuri.org/dsWolfSeco.xsd">
<
Contacts>
    <
Contact_ID>987Contact_ID>
    <
Contact_Firma>Workaholic Ltd.Contact_Firma>
    <
Contact_Naam>I. MFckdContact_Naam>
    <
Contact_Voornaam />
    <
ContactTitel_Naam>Mr.ContactTitel_Naam>
    <
Contact_Adres>Third corner right 988Contact_Adres>
    <Contact_Postcode>8765Contact_Postcode>
    <Contact_Gemeente>GeekvilleContact_Gemeente>
    <
Contact_Land>WorkaholicContact_Land>
    <
Contact_BTW>NL.987.394.088Contact_BTW>
    <
Contact_Telefoon>05/555.55.55Contact_Telefoon>
    <
Contact_Fax />
    <
Contact_GSM />
    <
Contact_Email>biteme@intheass.comContact_Email>
    <
Contact_Website>www.biteme.beContact_Website>
    <Contact_Bank />
    <
Contact_Opmerking />
  Contacts>
dsWolfSeco>

And now the xsl document which will be a big chunk

xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0" xmlns:ds="http://www.tempuri.org/dsWolfSeco.xsd" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:fox="http://xml.apache.org/fop/extensions" exclude-result-prefixes="ds fo fox">

      <xsl:output method="xml"/>

      <xsl:template match="ds:dsWolfSeco">

            <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">

                  <fo:layout-master-set>

                        <fo:simple-page-master master-name="voorpagina" page-width="210mm" page-height="297mm" margin-top="15mm" margin-bottom="15mm" margin-left="15mm" margin-right="15mm">

                              <fo:region-body margin-top="15mm" margin-bottom="10mm" margin-left="10mm" margin-right="10mm" border="thin silver ridge" padding="6pt"/>

                        fo:simple-page-master>

                        <fo:page-sequence-master master-name="rapport">

                              <fo:repeatable-page-master-reference master-reference="voorpagina"/>

                        fo:page-sequence-master>

                  fo:layout-master-set>

                  <fo:page-sequence master-reference="rapport">

                        <fo:flow flow-name="xsl-region-body">

                              <fo:block space-after="25mm" font-size="9pt">

                                    <fo:block space-after="5mm">

 

                                    fo:block>

                                    <fo:block>

                                          <fo:block language="EN-GB" widows="1" orphans="1">

                                                <fo:inline color="#000000" language="EN-GB">

                                                      <fo:leader leader-length="0pt" />

                                                      Wolf

                                                fo:inline>

                                                <fo:inline color="#000000" language="EN-GB">

                                                      <fo:leader leader-length="0pt" />

                                                      Seco

                                                fo:inline>

                                          fo:block>

                                    fo:block>

                                    <fo:block language="NL" widows="1" orphans="1">

                                          <fo:inline color="#000000">

                                                <fo:leader leader-length="0pt" />

                                                Drie Eikenstraat 358

                                          fo:inline>

                                    fo:block>

                                    <fo:block language="NL" widows="1" orphans="1">

                                          <fo:inline color="#000000">

                                                <fo:leader leader-length="0pt" />

                                                2650 Edegem

                                          fo:inline>

                                    fo:block>

                                    <fo:block font-family="Arial" language="NL" widows="1" orphans="1" >

                                          <fo:inline color="#000000">

                                                <fo:leader leader-length="0pt" />

                                                BELGIUM

                                          fo:inline>

                                    fo:block>

                                    <fo:block language="NL" widows="1" orphans="1">

                                          <fo:inline>

 

                                          fo:inline>

                                          <fo:inline>

                                                <fo:leader leader-pattern="space" leader-length="4pt" />

                                          fo:inline>

                                          <fo:inline color="#000000">

                                                <fo:leader leader-length="0pt" />

                                                +32 (3) 449 59 39

                                          fo:inline>

                                    fo:block>

                                    <fo:block language="NL" widows="1" orphans="1">

                                          <fo:inline>

 

                                          fo:inline>

                                          <fo:inline>

                                                <fo:leader leader-pattern="space" leader-length="4pt" />

                                          fo:inline>

                                          <fo:inline color="#000000">

                                                <fo:leader leader-length="0pt" />

                                                +32 (3) 449 72 13

                                          fo:inline>

                                    fo:block>

                                    <fo:block language="NL" widows="1" orphans="1">

                                          <fo:inline>

 

                                          fo:inline>

                                          <fo:inline>

                                                <fo:leader leader-pattern="space" leader-length="4pt" />

                                          fo:inline>

                                          <fo:inline color="#000000">

                                                <fo:leader leader-length="0pt" />

                                                <fo:basic-link external-destination="url('mailto:wolf-seco@skynet.be')" color="#0000ff">wolf-seco@skynet.befo:basic-link>

                                          fo:inline>

                                    fo:block>

                                    <fo:block language="NL" widows="1" orphans="1">

                                          <fo:inline>

                                                <fo:leader leader-pattern="space" leader-length="13pt" />

                                          fo:inline>

                                          <fo:inline color="#000000">

                                                <fo:leader leader-length="0pt" />

                                                <fo:basic-link external-destination="url('http://www.wolf-seco.be')" color="#0000ff">http://www.wolf-seco.befo:basic-link>

                                          fo:inline>

                                    fo:block>

                              fo:block>

                              <fo:table width="162mm" left="10mm" border-style="solid" border-width="0.5mm" border-color="#000000">

                                    <fo:table-column column-width="35mm" text-align="right"/>

                                    <fo:table-column column-width="45mm"/>

                                    <fo:table-column column-width="25mm"/>

                                    <fo:table-column column-width="45mm"/>

                                    <fo:table-body>

                                          <fo:table-row>

                                                <fo:table-cell text-align="right"  padding="1mm"%3 Posted on Thursday, August 4, 2005 6:25 PM | Back to top


Copyright © Ivan Porto Carrero | Powered by: GeeksWithBlogs.net