Site icon NgDeveloper

Xml to Pdf in java using iText Sample code

Xml to Pdf:

We can convert any xml file to pdf in java using iText lib.

Required Jar Files:

Xml to Pdf Required Jars

Xml File: (Sample xml file – to convert to pdf file)

<?xml version="1.0"?>
<XMLtoPDF>
	<Friend>
		<FriendName>Hemakumar</FriendName>
		<FriendPlace>Arcot</FriendPlace>
	</Friend>
	<Friend>
		<FriendName>Chudar</FriendName>
		<FriendPlace>Vellore</FriendPlace>
	</Friend>
	<Friend>
		<FriendName>Selva kumar</FriendName>
		<FriendPlace>Arcot</FriendPlace>
	</Friend>
</XMLtoPDF>

Xsl File (stylesheet for the above xml):

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method='html' version='1.0' encoding='UTF-8' indent='yes'/>
	<xsl:template match="/">
		<html>
			<body>
				<h2>Xml 2 Pdf in Java Using iText</h2>
				<table border="1">
					<tr bgcolor="skyblue">
						<th align="left">Friend's Name</th>
						<th align="left">Friend's Place</th>
					</tr>
					<xsl:for-each select="XMLtoPDF/Friend">
						<tr>
							<td><xsl:value-of select="FriendName"/></td>
							<td><xsl:value-of select="FriendPlace"/></td>
						</tr>
					</xsl:for-each>
				</table>
			</body>
		</html>
	</xsl:template>
</xsl:stylesheet>

Java Program which converts the xml file to pdf file:

package com.ngdeveloper;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.dom4j.DocumentException;
import org.xhtmlrenderer.pdf.ITextRenderer;

public class Xml2Pdf {
	public static void main(String[] args) throws IOException, DocumentException, TransformerException,
			TransformerConfigurationException, FileNotFoundException {
		TransformerFactory tFactory = TransformerFactory.newInstance();

// specify the input xsl file location to apply the styles for the pdf
// output file
		Transformer transformer = tFactory.newTransformer(new StreamSource("D:\\javadomain.xsl"));

// specify the input xml file location
		transformer.transform(new StreamSource("D:\\javadomain.xml"),
				new StreamResult(new FileOutputStream("D:\\javadomain.html")));

// Specifying the location of the html file (xml converted to html)
		String File_To_Convert = "D:\\javadomain.html";

		String url = new File(File_To_Convert).toURI().toURL().toString();
		System.out.println("" + url);

// Specifying the location of the outpuf pdf file.
		String HTML_TO_PDF = "D:\\PdfFromXml.pdf";

		OutputStream os = new FileOutputStream(HTML_TO_PDF);
		ITextRenderer renderer = new ITextRenderer();
		renderer.setDocument(url);
		renderer.layout();
		renderer.createPDF(os);
		os.close();
	}
}

XML converted to Pdf File – output:

Exit mobile version