Sunday, April 12, 2009

XML to PDF tutorial

To transfer xml information to pdf (create pdf file from xml file)
FOP : Formatting Objects Processor is an open source Java API that can convert your XML data into reports in PDF format.
1. Download fop from apache binary version
2. Unzip file and copy any drive say c:\fop-0.95-bin and add this path to your system environment path.
3. student.xml (c:\pdf\student.xml)
4. student.xsl (c:\pdf\student.xsl)
5. use command (c:\pdf> fop -xml student.xml -xsl student.xsl -pdf student.pdf)
You will get one student.pdf file. Very easy

Student.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<StudentRecord>
<Student>
<name>Manish</name>
<roll>130</roll>
<country>India</country>
<company>Infosys</company>
</Student>
<Student>
<name>Pramod Kumar</name>
<roll>120</roll>
<country>India</country>
<company>Patni Computers</company>
</Student>
<Student>
<name>Binod Kumar Suman</name>
<roll>110</roll>
<country>India</country>
<company>Satyam Computers</company>
</Student>
</StudentRecord>

student.xsl

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format%22&gt;
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">

<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format%22&gt;
<fo:simple-page-master master-name="first"
page-height="29.7cm"
page-width="21cm"
margin-top="1cm"
margin-bottom="2cm"
margin-left="2.5cm"
margin-right="2.5cm">

<fo:region-body margin-top="3cm"/>
<fo:region-before extent="3cm"/>
<fo:region-after extent="1.5cm"/>
</fo:simple-page-master>
</fo:layout-master-set>

<fo:page-sequence master-reference="first">
<fo:static-content flow-name="xsl-region-before">
<fo:block line-height="14pt" font-size="10pt"
text-align="end">By Binod</fo:block>
</fo:static-content>
<fo:static-content flow-name="xsl-region-after">
<fo:block line-height="14pt" font-size="10pt"
text-align="end">Page <fo:page-number/>
</fo:block>
</fo:static-content>

<fo:flow flow-name="xsl-region-body">
<fo:block font-size="18pt"
font-family="sans-serif"
line-height="24pt"
space-after.optimum="15pt"
background-color="blue"
color="white"
text-align="center"
padding-top="3pt">
STUDENT EMPLYOEE REPORT
</fo:block>

<fo:block font-size="16pt"
font-family="sans-serif"
space-after.optimum="15pt"
text-align="center">
ADMS JAVA AND CS DEPARTMENT
</fo:block>

<fo:block text-align="start">This is based on the performance. </fo:block>

<fo:table table-layout="fixed" width="100%" border-collapse="separate">
<fo:table-column column-width="50mm"/>
<fo:table-column column-width="50mm"/>
<fo:table-column column-width="50mm"/>
<fo:table-body>
<fo:table-row color="red">
<fo:table-cell><fo:block>NAME</fo:block></fo:table-cell> <fo:table-cell><fo:block>ROLL</fo:block></fo:table-cell> <fo:table-cell><fo:block>COMPANY</fo:block></fo:table-cell> </fo:table-row>
<fo:table-row color="red">
<fo:table-cell><fo:block>------------------</fo:block></fo:table-cell> <fo:table-cell><fo:block>------------------</fo:block></fo:table-cell> <fo:table-cell><fo:block>------------------</fo:block></fo:table-cell> </fo:table-row>

<xsl:for-each select="StudentRecord/Student">
<xsl:sort select="name"/>
<fo:table-row>
<fo:table-cell><fo:block><xsl:value-of select="name"/></fo:block></fo:table-cell>
<fo:table-cell><fo:block><xsl:value-of select="roll" /></fo:block></fo:table-cell>
<fo:table-cell><fo:block><xsl:value-of select="company" />
</fo:block>
</fo:table-cell>
</fo:table-row>
</xsl:for-each>
</fo:table-body>
</fo:table>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
</xsl:stylesheet>

Now you will feel that it is very job to develop PDF from XML. :)

1 comment:

You can put your comments here (Either feedback or your Question related to blog)