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:

pdf from xml

6 comments

  • Hi, I do believe your site may be having web browser
    compatibility issues. Whenever I look at your blog in Safari, it looks fine however when opening in I.E.,
    it’s got some overlapping issues. I just wanted to give you a quick heads up!
    Apart from that, excellent site!

  • Niklas Karlsson

    Hi I get the PDF, but no bluecolor
    in the HTML: I have the color..
    and then i recive:

    Warning: org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser: Property ‘http://www.oracle.com/xml/jaxp/properties/entityExpansionLimit’ is not recognized.
    Compilewarning:
    WARNING: ‘org.apache.xerces.jaxp.SAXParserImpl: Property ‘http://javax.xml.XMLConstants/property/accessExternalDTD’ is not recognized.’
    Warning: org.apache.xerces.parsers.SAXParser: Feature ‘http://javax.xml.XMLConstants/feature/secure-processing’ is not recognized.
    Warning: org.apache.xerces.parsers.SAXParser: Property ‘http://javax.xml.XMLConstants/property/accessExternalDTD’ is not recognized.
    Warning: org.apache.xerces.parsers.SAXParser: Property ‘http://www.oracle.com/xml/jaxp/properties/entityExpansionLimit’ is not recognized.
    file:/Q:/Projekt/eclipse-java-luna-SR2-win32-x86_64/XMLfiler/javadomain.html
    done!

    and then:
    IS it possible to use iText if i have a XML-file that i want to pictures if some text-element is a specific value?
    or do i do this in XsL?

  • it look strange when I try this..
    please loot at an exampel:
    a XML:

    <?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>

    a XLS:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <xsl:output method='html' version='1.0' encoding='UTF-8' indent='yes'/>
    <xsl:template match="/">
    <html>
    <body>
    <img src="picture.jpg"/>

    <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>

    when I run this, it generate a strange HTML-code 🙁
    <html>
    <body>
    <img src="picture.jpg"><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>
    <tr>
    <td>Hemakumar</td><td>Arcot</td>
    </tr>
    <tr>
    <td>Chudar</td><td>Vellore</td>
    </tr>
    <tr>
    <td>Selva kumar</td><td>Arcot</td>
    </tr>
    </table>
    </body>
    </html>

    so it generate this:
    <img src="picture.jpg">
    and NOT this:
    <img src="picture.jpg"/>
    🙁

    and the error:
    Warning: org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser: Property 'http://www.oracle.com/xml/jaxp/properties/entityExpansionLimit&#039; is not recognized.
    Kompileringsvarningar:
    WARNING: 'org.apache.xerces.jaxp.SAXParserImpl: Property 'http://javax.xml.XMLConstants/property/accessExternalDTD&#039; is not recognized.'
    Warning: org.apache.xerces.parsers.SAXParser: Property 'http://javax.xml.XMLConstants/property/accessExternalDTD&#039; is not recognized.
    Warning: org.apache.xerces.parsers.SAXParser: Property 'http://www.oracle.com/xml/jaxp/properties/entityExpansionLimit&#039; is not recognized.
    file:/Q:/Projekt/eclipse-java-luna-SR2-win32-x86_64/XMLfiler/javadomain2.html
    ERROR: 'The element type "img" must be terminated by the matching end-tag "</img>".'
    org.xhtmlrenderer.util.XRRuntimeException: Can't load the XML resource (using TRaX transformer). org.xml.sax.SAXParseException; lineNumber: 18; columnNumber: 3; The element type "img" must be terminated by the matching end-tag "</img>".
    at org.xhtmlrenderer.resource.XMLResource$XMLResourceBuilder.createXMLResource(XMLResource.java:191)
    at org.xhtmlrenderer.resource.XMLResource.load(XMLResource.java:71)
    at org.xhtmlrenderer.swing.NaiveUserAgent.getXMLResource(NaiveUserAgent.java:211)
    at org.xhtmlrenderer.pdf.ITextRenderer.loadDocument(ITextRenderer.java:134)
    at org.xhtmlrenderer.pdf.ITextRenderer.setDocument(ITextRenderer.java:138)
    at Xml2Pdf.main(Xml2Pdf.java:82)
    Caused by: javax.xml.transform.TransformerException: org.xml.sax.SAXParseException; lineNumber: 18; columnNumber: 3; The element type "img" must be terminated by the matching end-tag "</img>".
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown Source)
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown Source)
    at org.xhtmlrenderer.resource.XMLResource$XMLResourceBuilder.createXMLResource(XMLResource.java:189)
    … 5 more
    Caused by: org.xml.sax.SAXParseException; lineNumber: 18; columnNumber: 3; The element type "img" must be terminated by the matching end-tag "</img>".
    at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(Unknown Source)
    … 8 more

    • You are trying to create a table in pdf using xml right ? When I tried with the sample code and data shared here, I am able to get the output which is mentioned in the post.

  • you can try this free online pdf to text converter(http://www.online-code.net/pdf-to-word.html) to convert pdf to txt file online.

Leave a Reply