Step 1 : In step one we will render html object like browser render it, so that DOM(Document Object Model) can be clean and fire events like “document ready” and “body onload”. We can do it with help of htmlunit and htmlcleaner jars . To learn how to do it please click on below link.
Step 2 : If you do not want to render html string and only wants to convert it to PDF as is, then you can avoid step 1 and convert that HTML string into DOM with help of DocumentBuilder.
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
InputSource is = new InputSource();
Document document = db.parse(is);
Step 3 :In step three we will convert that rendered/parsed object into PDF with help of Itext and CoreRenderer Jars. Below is the code given for it.
ITextRenderer renderer = new ITextRenderer();
File pdfFile = new File(filePath+pdfFileName);
OutputStream outputStream = new FileOutputStream(pdfFile);
rootLogger.debug("Before create PDF with file name --> "+pdfFileName);
rootLogger.debug("After create PDF with file Name -->"+pdfFileName);
This is it now you have converted PDF object.
Jars required for this are :
Please Ask Questions or give suggestions through comments in below section .
Other related posts are :
How to render HTML in java
Top 10 spring interview questions and their answers. Spring basic concepts.
MQ connection not closed and giving MQ Error-2009
Logback logs are not working. Log4j logs are not working. Steps to make logging work.
Please share this posts on facebook,Linkeedin,Google+,Twitter by clicking on below icons.