Apache Reports with XSLT - Yash-777/LearnJava GitHub Wiki

Change language of Excel numbers

1st (only changes in Excel)

  • In Excel go to File - Options - Advanced
  • Under Editing Options uncheck Use system separators and set your own.

2nd Option

  • From Start button search for Region settings.
  • In the new window, under Formats tab change to English then press OK.

The Apache™ FOP Project xmlgraphics/fop/trunk

Generating XSL-FO and Display/Print by XSL Formatter

Example: Find below used xml file over here...

public static void main(String[] args) {
    try {
        // Setup directories
        File baseDir = new File("./FOP");
        // Setup input and output files
        File xmlfile = new File(baseDir, "/projectteam.xml");
        File xsltfile = new File(baseDir, "/projectteam2fo.xsl");
        File pdffile = new File("C:/Yash/ResultXML2.pdf");

        System.out.println("Transforming...");

        // Setup output
        OutputStream out = FopIO.setupOutput(pdffile);

        try {
            // Construct fop with desired output format
            Fop fop = FopIO.getFopInstance(FopIO.fopXConf, out);

            String xmlStr = "<projectteam> <projectname>The Killer Application</projectname>"
                    + "<member> <name>John Doe</name> <function>lead</function> <email>[email protected]</email> </member>"
                    + "</projectteam>";
            FopIO.setupXSLTwithXML(fop, xsltfile, xmlStr);
                            
            // FopIO.setupXSLTwithXML(fop, xsltfile, xmlfile);
        } finally {
            out.close();
        }

        System.out.println("Success!");
    } catch (Exception e) {
        e.printStackTrace(System.err);
    }
}
public class FopIO {
  static File fopXConf = new File("./FOP/fop.xconf");
  public static OutputStream setupOutput(File pdffile) throws FileNotFoundException {
    OutputStream out = new java.io.FileOutputStream(pdffile);
    out = new java.io.BufferedOutputStream(out);
    return out;
  }
  public static Fop getFopInstance(File fopXConf, OutputStream outStream) throws SAXException, IOException {
    FopConfParser parser = new FopConfParser(fopXConf); //parsing configuration  
    FopFactoryBuilder builder = parser.getFopFactoryBuilder(); //building the factory with the user options
    FopFactory fopFactory = builder.build();
    // Construct fop with desired output format
    Fop fop = fopFactory.newFop("application/pdf", outStream);
    return fop;
  }
  public static Transformer setupXSLTwithXML(Fop fop, File xsltfile, File xmlInput) throws Exception {
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer(new StreamSource(xsltfile));
    // Set the value of a <param> in the stylesheet
    transformer.setParameter("versionParam", "2.0");
    // Setup input for XSLT transformation
    Source src = new StreamSource(xmlInput);
    // Resulting SAX events (the generated FO) must be piped through to FOP
    Result res = new SAXResult(fop.getDefaultHandler());
    // Start XSLT transformation and FOP processing
    transformer.transform(src, res);
    return transformer;
  }
  public static void setupXSLTwithXML(Fop fop, File xsltfile, String xmlStr) throws Exception {
    TransformerHandler handler = FopIO.setXSLTWrapper(fop, xsltfile);
    
    
    // SAX implementation... SAX and DOM have no major difference in performance or memory consumption..
    StringReader reportFilterXmlReader = new StringReader(xmlStr);
    InputSource xmlSource = new InputSource(reportFilterXmlReader);
    SAXSource reportFilterSource = new SAXSource(xmlSource);
    
    SAXTransformerFactory factory = (SAXTransformerFactory) new net.sf.saxon.TransformerFactoryImpl();

    Transformer t = factory.newTransformer();

    // UnsupportedOperationException: The TransformerHandler is not serially reusable. The startDocument() method must be called once only.
    // handler.startDocument();
	    SAXResult sr = new SAXResult(handler);
	    t.transform(reportFilterSource, sr);
    
    handler.endDocument();
  }
  public static TransformerHandler setXSLTWrapper(Fop fop, File xsltfile) throws SAXException, TransformerConfigurationException, FileNotFoundException {
    SAXTransformerFactory factory = (SAXTransformerFactory) new net.sf.saxon.TransformerFactoryImpl();
    TransformerHandler xmlContentHandler = factory.newTransformerHandler();
    
    Source xslSource = new StreamSource(new FileInputStream(xsltfile));
    TransformerHandler xslTransformer = factory.newTransformerHandler(xslSource);
    SAXResult sr = new SAXResult(xslTransformer);
    
    xmlContentHandler.setResult(sr);
    
    Result res = new SAXResult(fop.getDefaultHandler());
    xslTransformer.setResult(res);
    return xmlContentHandler;
  }
}

POM.xml

<dependencies>
    <dependency>
        <groupId>net.sf.saxon</groupId>
        <artifactId>Saxon-HE</artifactId>
        <version>9.7.0-15</version>
    </dependency>
    <dependency>
        <groupId>org.apache.xmlgraphics</groupId>
        <artifactId>fop</artifactId>
        <version>2.4</version>
    </dependency>
    <dependency>
        <groupId>org.apache.xmlgraphics</groupId>
        <artifactId>fop-util</artifactId>
        <version>2.4</version>
    </dependency>
    <dependency>
        <groupId>org.apache.xmlgraphics</groupId>
        <artifactId>fop-events</artifactId>
        <version>2.4</version>
    </dependency>
    <dependency>
        <groupId>org.apache.xmlgraphics</groupId>
        <artifactId>fop-core</artifactId>
        <version>2.4</version>
    </dependency>
    <dependency>
        <groupId>org.apache.xmlgraphics</groupId>
        <artifactId>xmlgraphics-commons</artifactId>
        <version>2.4</version>
    </dependency>
    <dependency>
        <groupId>org.apache.avalon.framework</groupId>
        <artifactId>avalon-framework-api</artifactId>
        <version>4.3.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.avalon.framework</groupId>
        <artifactId>avalon-framework-impl</artifactId>
        <version>4.3.1</version>
    </dependency>
    
    <!--
    <dependency>
        <groupId>org.apache.xmlgraphics</groupId>
        <artifactId>fop</artifactId>
        <version>2.1</version>
        <exclusions>
            <exclusion>
                <artifactId>xml-apis</artifactId>
                <groupId>xml-apis</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    -->
<dependencies>

fop.xconf

<!-- This is the default version retrieved from documentation, 
 we need to enhance it further based on common configuration that we can pull -->

<fop version="1.0">
  <!-- Strict user configuration -->
  <strict-configuration>true</strict-configuration>

  <!-- Strict FO validation -->
  <strict-validation>true</strict-validation>

  <!-- Base URL for resolving relative URLs -->
  <base>./</base>

  <!-- Font Base URL for resolving relative font URLs -->
  <font-base>./</font-base>

  <!-- Source resolution in dpi (dots/pixels per inch) for determining the size of pixels in SVG and bitmap images, default: 72dpi -->
  <source-resolution>72</source-resolution>
  <!-- Target resolution in dpi (dots/pixels per inch) for specifying the target resolution for generated bitmaps, default: 72dpi -->
  <target-resolution>72</target-resolution>

  <!-- default page-height and page-width, in case
       value is specified as auto -->
  <default-page-settings height="11in" width="8.26in"/>

  <!-- Use file name nl_Bel instead of the default nl_BE -->
  <hyphenation-pattern lang="en" country="US">en_US</hyphenation-pattern>

  <!-- etc. etc..... -->
  <renderers>
 <renderer mime="application/pdf">
    <filterList>
      <!-- provides compression using zlib flate (default is on) -->
      <value>flate</value>
    </filterList>
    <fonts>
      <font metrics-url="arial.xml" kerning="yes" embed-url="arial.ttf">
        <font-triplet name="Arial" style="normal" weight="normal"/>
        <font-triplet name="ArialMT" style="normal" weight="normal"/>
      </font>
      <font metrics-url="arialb.xml" kerning="yes" embed-url="arialb.ttf">
        <font-triplet name="Arial" style="normal" weight="bold"/>
        <font-triplet name="ArialMT" style="normal" weight="bold"/>
      </font>
    </fonts>
  </renderer>
  </renderers>
</fop>

projectteam2fo.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" version="1.1" exclude-result-prefixes="fo">
  <xsl:output method="xml" version="1.0" omit-xml-declaration="no" indent="yes" />
  <xsl:param name="versionParam" select="'1.0'" /> <!-- Property values parameters -->
  <!--  root element of XML input  -->
  <xsl:template match="projectteam">
    <fo:root>
      <fo:layout-master-set>
        <fo:simple-page-master master-name="PageSatrt_A4" page-height="29.7cm" page-width="21cm" margin-top="2cm" margin-bottom="2cm" margin-left="2cm" margin-right="2cm">
          <!-- PageItems: body[xsl-region-body], before[xsl-region-before], after[xsl-region-after], start, end ~ Not allowing : padding="6pt"-->
          <fo:region-body region-name="xsl-region-body" margin-top="0.4in" column-gap="0.25in" column-count="2" />
          <!-- fo:flow  -->
          <fo:region-before region-name="Header" extent="0.2in" display-align="after" background-color="blue" precedence="true" />
          <fo:region-after region-name="Footer" extent="0.2in" display-align="before" background-color="green" precedence="true" />
        </fo:simple-page-master>
      </fo:layout-master-set>
      <!-- <fo:bookmark-tree> ... </fo:bookmark-tree> -->
      <fo:page-sequence master-reference="PageSatrt_A4">
        <!--  Failed to read font file arial.ttf [.\FOP\arial.xml] font-family="Arial" -->
        <fo:static-content flow-name="Header" color="white" font-size="10pt">
          <fo:block>The header of the page is aligned on the bottom of fo:region-before.</fo:block>
        </fo:static-content>
        <fo:static-content flow-name="Footer">
          <fo:block text-align="right" border-top="1pt solid black" padding-top="1mm" color="white" font-size="10pt">
            Page <fo:page-number /> of <fo:page-number-citation ref-id="endPageNumber" /> <!-- fo:flow last: <fo:block id="the End"/> -->
          </fo:block>
        </fo:static-content>
        
        <fo:flow flow-name="xsl-region-body" font-size="10pt" text-align="justify">
          <fo:block id="toc" font-size="16pt" font-weight="bold" space-after="5mm">
            Project: <xsl:value-of select="projectname" />
          </fo:block>
          <fo:block font-size="12pt" space-after="5mm">
            Version <xsl:value-of select="$versionParam" />
          </fo:block>
          <fo:block id="PAGE001" font-size="10pt">
            <fo:table table-layout="fixed" width="100%" border-collapse="separate">
              <fo:table-column column-width="4cm" />
              <fo:table-column column-width="4cm" />
              <fo:table-column column-width="5cm" />
              <fo:table-body>
                <xsl:apply-templates select="member" />
              </fo:table-body>
            </fo:table>
          </fo:block>
          <fo:block id="PAGE002" break-before="page">The text content of the second page.</fo:block>
          <fo:block id="endPageNumber" />
        </fo:flow>
      </fo:page-sequence>
    </fo:root>
  </xsl:template>
  <!--  child element referenced template for root element  -->
  <xsl:template match="member">
    <fo:table-row>
      <xsl:if test="function = 'lead'">
        <xsl:attribute name="font-weight">bold</xsl:attribute>
      </xsl:if>
      <fo:table-cell>
        <fo:block> <xsl:value-of select="name" />   </fo:block>
      </fo:table-cell>
      <fo:table-cell>
        <fo:block> <xsl:value-of select="function" /> </fo:block>
      </fo:table-cell>
      <fo:table-cell>
        <fo:block> <xsl:value-of select="email" />  </fo:block>
      </fo:table-cell>
    </fo:table-row>
  </xsl:template>
</xsl:stylesheet>
⚠️ **GitHub.com Fallback** ⚠️