Reputation: 1816
I want to generate PDF online like xslt tranformation
INPUT XML:
<?xml version="1.0" encoding="UTF-8"?>
<students>
<student>
<name>Amrendra Kumar</name>
<class>BCA</class>
</student>
<student>
<name>Sanjeev Kumar</name>
<class>MCA</class>
</student>
</students>
XSLT to generate PDF:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
exclude-result-prefixes="xs"
version="2.0">
<xsl:template match="/">
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="a">
<fo:region-body margin="1in"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="a">
<fo:flow flow-name="xsl-region-body">
<fo:block>
<fo:table border="1pt">
<fo:table-column column-number="1" column-width="180pt"/>
<fo:table-column column-number="2" column-width="300pt"/>
<fo:table-header>
<fo:table-row padding-top="0pt" padding-bottom="0pt">
<fo:table-cell>
<fo:block font-family="TimesNewRoman" font-size="10pt" space-before="4pt" text-align="left">
<fo:inline font-weight="bold" font-size="12pt">Name</fo:inline>
</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block font-family="TimesNewRoman" font-size="10pt" space-before="4pt" text-align="left">
<fo:inline font-weight="bold" font-size="12pt">Class</fo:inline>
</fo:block>
</fo:table-cell>
</fo:table-row>
</fo:table-header>
<fo:table-body start-indent="0pt" end-indent="0pt">
<xsl:for-each select="students/student">
<fo:table-row padding-top="0pt" padding-bottom="0pt">
<fo:table-cell>
<fo:block font-family="TimesNewRoman" font-size="10pt" space-before="4pt" text-align="left">
<fo:inline font-size="12pt">
<xsl:value-of select="name"/>
</fo:inline>
</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block font-family="TimesNewRoman" font-size="10pt" space-before="4pt" text-align="left">
<fo:inline font-size="12pt">
<xsl:value-of select="class"/>
</fo:inline>
</fo:block>
</fo:table-cell>
</fo:table-row>
</xsl:for-each>
</fo:table-body>
</fo:table>
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
</xsl:stylesheet>
Output: need to generate PDF online
Upvotes: 1
Views: 2469
Reputation: 411
Yes, we can generate the pdf from XML and XSL file using apache fop.
public void convertXmlToPdf(String xmlFilePath, String xslFilePath, String pdfFileDirectory) throws IOException {
File xmlfile = new File(xmlFilePath);
File xsltfile = new File(xslFilePath);
File pdfDir = new File(pdfFileDirectory);
pdfDir.mkdirs();
File pdfFile = new File(pdfDir, "pdfFile.pdf");
System.out.println(pdfFile.getAbsolutePath());
final FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI());
FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
OutputStream out = new FileOutputStream(pdfFile);
out = new java.io.BufferedOutputStream(out);
try{
Fop fop;
fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new StreamSource(xsltfile));
Source src = new StreamSource(xmlfile);
Result res = new SAXResult(fop.getDefaultHandler());
transformer.transform(src, res);
}catch (FOPException | TransformerException e){
e.printStackTrace();
}finally {
out.close();
}
System.out.println("PDF generated successfully");
}
Upvotes: 1