How to render HTML in java

When we open a html content in browser then browser render that html also fire events like “document.ready”,”body onload”. Java script code do whatever we write in that code for document ready function. Eg: Render some grid on Document ready,show some image on basis of some condition etc.
But when we have that html code as a string in java and we want the html rendered object(with document ready and body onload functionality) in java then we need renderer API .
We majorly get this type of requirement when we need to create PDF from html or we want to write html object as input stream on some protocol etc.
Htmlunit and HTML Cleaner is the widely used API in java for this functionality.

I am going to explain below how to use it in java .

public void renderHtml(String htmlToConvert){
HtmlCleaner cleaner = new HtmlCleaner();
ByteArrayOutputStream out = new ByteArrayOutputStream();
final WebClient webClient = new WebClient();
try
{
String htmlFileName="D:\\gaurav\htmlFile.html";
String pdfFileName=null;
File htmlFile = new File(filePath+htmlFileName);
FileWriter fileWriter = new FileWriter(htmlFile);
BufferedWriter writer = new BufferedWriter(fileWriter);
writer.write(htmlToConvert);
writer.close();
fileWriter.close();
final HtmlPage page = webClient.getPage(htmlFile.toURI().toURL().toString());
final String pageAsXml = page.asXml();
system.out.println("Before tag node -->"+pageAsXml);
TagNode node = cleaner.clean(pageAsXml);
DomSerializer domSerializer = new DomSerializer(props);
Document document = domSerializer.createDOM(node);
new PrettyXmlSerializer(props).writeToStream(node, out);
out.flush();
out.close();
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
webClient.closeAllWindows();
if(webClient!=null && webClient.getCookieManager()!=null)
{
webClient.getCookieManager().clearCookies();
}
}
}

HtmlCleaner cleans the html. Eg: If there is a tag which is not closed then it make them close to make html proper.
Htmlunit then perform rendering.
Jars required for these functionality are given below:

  1. htmlcleaner-2.2.jar
  2. htmlunit-2.14.jar
  3. htmlunit-core-js-2.14.jar
  4. httpclient-4.3.2.jar
  5. httpcore-4.3.1.jar
  6. httpmime-4.3.2.jar

You can download these jars from sourceforge or mvnrepository .

If you want to learn how to convert this rendered HTML object into PDF please click on below link

HTML to PDF conversion in JAVA | HTML to PDF example in JAVA

Please Ask Questions or give suggestions through comments in below section .

Other related posts are :
HTML to PDF conversion in JAVA | HTML to PDF example in JAVA
Top 10 spring interview questions and their answers. Spring basic concepts.
MQ connection not closed and giving MQ Error-2009
JPA Hibernate Sequence generator generating odd ID value and IntegrityConstraintVolation exception is coming

Please share this posts on facebook,Linkeedin,Google+,Twitter by clicking on below icons.

Advertisements

2 thoughts on “How to render HTML in java

  1. Pingback: HTML to PDF conversion in JAVA | HTML to PDF example in JAVA | Techno Tailor

  2. Pingback: Are Payment banks a competitive threat or addon to the traditional banking. | Techno Tailor

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s