Click here to Skip to main content
12,458,167 members (53,979 online)
Click here to Skip to main content

Tagged as

Stats

53.6K views
1.6K downloads
127 bookmarked
Posted

Generating PDF reports using nfop

, , 7 Jun 2011 CPOL
This article will help you to examine the main features of XSL schemes to generate programmatically advanced PDF reports.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.1"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:fo="http://www.w3.org/1999/XSL/Format"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:fox="http://xml.apache.org/fop/extensions"
  exclude-result-prefixes="fo">

  <xsl:output method="xml" version="1.0" omit-xml-declaration="no" indent="yes"/>
  <xsl:template match="/">
    <fo:root font-family="arialuni">
      <fo:layout-master-set>
        <!--spm for first page-->
        <fo:simple-page-master master-name="first" page-height="29.7cm" page-width="21cm" >
          <fo:region-body margin="40pt"/>
          <fo:region-after extent="1in"/>
          <fo:region-before extent="0.5in"/>
          <fo:region-end extent="0.4in"/>
          <fo:region-start extent="0.4in"/>
        </fo:simple-page-master>

        <!-- spm for all pages -->
        <fo:simple-page-master master-name="allPages" page-height="29.7cm" page-width="21cm">
          <fo:region-body margin="40pt"/>
          <fo:region-after extent="1in"/>
          <fo:region-before extent="0.4in"/>
          <fo:region-end extent="0.4in"/>
          <fo:region-start extent="0.4in"/>
        </fo:simple-page-master>

        <!-- Control the sequencing for odd and even pages in chapters -->
        <fo:page-sequence-master master-name="chaps">          
        </fo:page-sequence-master>
      </fo:layout-master-set>      
      <!-- page-sequence for introduction page -->
      <fo:page-sequence master-reference="first">
        <fo:static-content flow-name="xsl-region-before">
          <fo:block text-align="center" space-before="10pt" font-size="13pt" font-weight="bold">
            <fo:leader leader-length="90%" leader-pattern="rule"
                   rule-thickness="0.5pt" color="black"/>
          </fo:block>
        </fo:static-content>
        <fo:static-content flow-name="xsl-region-after">
          <!--Numbers of pages and logo in lower right part of report page-->
          <fo:block text-align="right" margin-right="25pt" >
            <fo:external-graphic scaling="uniform" height="0.4in" width="1in" src="file:///E:/pdf_root/1.JPG"/>
          </fo:block>
          <fo:block text-align="center" space-before="10pt" font-size="13pt" font-weight="bold">
            <fo:page-number/>
          </fo:block>
        </fo:static-content>
        
        <fo:flow flow-name="xsl-region-body">
          <!--Building table of contents-->
          <fo:block font-family="arialuni" font-size="25pt" id="content_table" space-after="20pt" text-align="center">
            Table of contents       
          </fo:block>          
          <fo:block text-align-last="justify" break-after="page">
            <xsl:call-template name="BuildContentTable"/>
          </fo:block>          
          <!--Building table with data-->
          <fo:block>
            <xsl:for-each select="ArrayOfAuthor/Author">
              <fo:block>
                <xsl:if test="position() != last()">
                  <xsl:attribute name="break-after">
                    <xsl:value-of select="'page'"/>
                  </xsl:attribute>
                </xsl:if>
                <xsl:call-template name="BuildTable"/>
              </fo:block>
            </xsl:for-each>
          </fo:block>
          
          <!--Building bookmark tree-->
          <fo:block>
            <xsl:call-template name="BuildBookmarkTree"/>
          </fo:block>         
        </fo:flow>
      </fo:page-sequence>
    </fo:root>
  </xsl:template>  

  <xsl:template name="BuildContentTable">
    <xsl:for-each select="ArrayOfAuthor/Author">
      <fo:basic-link>
        <xsl:attribute name="internal-destination">
          <xsl:value-of select="id"/>
        </xsl:attribute>
        <fo:inline font-weight="bold" color="red">
          <xsl:value-of select="name"/>
        </fo:inline>
        <fo:leader leader-pattern="dots"/>
        <fo:page-number-citation>
          <xsl:attribute name="ref-id">
            <xsl:value-of select="id"/>
          </xsl:attribute>
        </fo:page-number-citation>       
      </fo:basic-link>      
      <xsl:for-each select="created/Book">
          <fo:block margin-left="1cm" text-align-last="justify">
            <fo:basic-link>
              <xsl:attribute name="internal-destination">
                <xsl:value-of select="id"/>
              </xsl:attribute>
              <fo:inline font-weight="bold" font-family="Courier">
                <xsl:value-of select="bookProps/name"/>
              </fo:inline>
              <fo:leader leader-pattern="dots"/>
              <fo:page-number-citation>
                <xsl:attribute name="ref-id">
                  <xsl:value-of select="id"/>
                </xsl:attribute>
              </fo:page-number-citation>
            </fo:basic-link>
          </fo:block>
        </xsl:for-each>
      <xsl:if test="position() != last()">
        <fo:block>
          <!--separator-->
          <fo:leader leader-length="100%" leader-pattern="rule"
                   rule-thickness="0.5pt" color="black"/>
        </fo:block>
      </xsl:if>
    </xsl:for-each>
  </xsl:template>
  
  <xsl:template name="BuildBookmarkTree">
    <fox:outline internal-destination="content_table">
      <fox:label>
        <xsl:text>Table of contents</xsl:text>
      </fox:label>
    </fox:outline>
    <xsl:for-each select="ArrayOfAuthor/Author">    
      <fox:outline>
        <xsl:attribute name="internal-destination">
          <xsl:value-of select="id"/>
        </xsl:attribute>
        <fox:label>
          <xsl:value-of select="name"/><!--Name of bookmark-->
        </fox:label>
        <xsl:for-each select="created/Book">
          <fox:outline>
            <xsl:attribute name="internal-destination">
              <xsl:value-of select="id"/>
            </xsl:attribute>
            <fox:label>
              <xsl:value-of select="bookProps/name"/>
            </fox:label>
          </fox:outline>
          </xsl:for-each>
      </fox:outline>
    </xsl:for-each>
  </xsl:template>
  
  <xsl:template name="FillGrid">
    <fo:table>
      <fo:table-column background-color="#DCDCDC" column-width="20%"/>
      <fo:table-column background-color="#FFFFFF"/>
      <fo:table-body>
        <xsl:for-each select="articles/Pare">
          <fo:table-row>
            <fo:table-cell padding="1pt" border="0.5pt solid black">
              <fo:block>
                <xsl:value-of select="name"/>
              </fo:block>
            </fo:table-cell>
            <fo:table-cell padding="1pt" border="0.5pt solid black">
              <fo:block>
                <xsl:value-of select="pages"/>
              </fo:block>
            </fo:table-cell>
          </fo:table-row>
        </xsl:for-each>
      </fo:table-body>
    </fo:table>
  </xsl:template>
  
  <xsl:template name="BuildTable">    
    <fo:block text-align="center" font-size="25pt"  space-after="5pt" font-weight="bold" color="#58FF11">
      <xsl:attribute name="id">
        <xsl:value-of select="id"/>
      </xsl:attribute>        
      <xsl:value-of select="name"/>
    </fo:block>
    <xsl:for-each select="created">
      <xsl:for-each select="Book">
        <fo:table space-after="3pt">
          <fo:table-column background-color="#DCDCDC"/>
          <fo:table-body>
            <fo:table-row>
              <fo:table-cell padding="4pt" border="1pt solid black">
                <fo:block text-align="center">
                  <xsl:attribute name="id">
                    <xsl:value-of select="id"/>
                  </xsl:attribute>
                  <xsl:value-of select="bookProps/name"/>
                  <xsl:text> / Price </xsl:text>
                  <xsl:value-of select="bookProps/price"/>
                </fo:block>
                <fo:block>
                  <xsl:call-template name="FillGrid"/>
                </fo:block>
              </fo:table-cell>
            </fo:table-row>            
          </fo:table-body>
        </fo:table>
      </xsl:for-each>     
    </xsl:for-each>
  </xsl:template>

  <xsl:template name="ParallelReading">
    <xsl:for-each select="Rows">
      <fo:table>
        <fo:table-column background-color="#DCDCDC" column-width="20%"/>
        <fo:table-column background-color="#FFFFFF" />
        <fo:table-body>
          <xsl:for-each select="Row">
            <fo:table-row>
              <fo:table-cell padding="4pt" border="1pt solid black">
                <xsl:for-each select="../../Columns/Column">
                  <fo:block>
                    <xsl:value-of select="Title"/>
                  </fo:block>
                </xsl:for-each>
              </fo:table-cell>
              <fo:table-cell padding="4pt" border="1pt solid black">
                <xsl:for-each select="Value">
                  <fo:block>
                    <xsl:value-of select="Link"/>
                  </fo:block>
                </xsl:for-each>
              </fo:table-cell>
            </fo:table-row>
          </xsl:for-each>
        </fo:table-body>      
      </fo:table>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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

Share

About the Authors

Apriorit Inc
Apriorit Inc.
Hungary Hungary
ApriorIT is a Software Research and Development company that works in advanced knowledge-intensive scopes.

Company offers integrated research&development services for the software projects in such directions as Corporate Security, Remote Control, Mobile Development, Embedded Systems, Virtualization, Drivers and others.

Official site http://www.apriorit.com
Group type: Organisation

32 members


Petro Vodopyan
Ukraine Ukraine
No Biography provided

You may also be interested in...

Pro
Pro
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160826.1 | Last Updated 7 Jun 2011
Article Copyright 2010 by Apriorit Inc, Petro Vodopyan
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid