pms
pms

Reputation: 944

itext pdf generation fail on parsing some html tags

I have this html code, which reside in db and I want to parse it in pdf. I am using itext for pdf generation. here is the html in db:

<p>no note.</p><br>
<ul><br>
<li><strong>section</strong></li><br>
</ul><br>
<ol><br>
<li>first</li><br>
<li><em>second</em></li><br>
<li><span style="text-decoration: underline;">third</span></li><br>
</ol><br>

and here is what is parsed and inserted into pdf:

<p>no note.</p><br>
<strong>section</strong><br>
first<br>
<em>second</em><br>
<span style="text-decoration: underline;">third</span><br>

and also here is my code to parse the html into pdf:

org.jsoup.nodes.Document doc = Jsoup.parse(text);
List<Element> objects;
objects = HTMLWorker.parseToList(new StringReader(doc.outerHtml()), null);
for (Element object : objects) {
        Element ele = (Element) object;
        document.add(ele);
}

as can be seen numbers and bullet are not shown (which are "ol" and "li" tags in html). How to solve this?

Edit

For more clarification. Here is the text I have in html:

enter image description here

and here is the note inserted into pdf:

enter image description here

Upvotes: 1

Views: 5408

Answers (2)

Du-Lacoste
Du-Lacoste

Reputation: 12817

HTMLWorker is deprecated long time ago and its intention was not to convert complete HTML pages. It basically does not know web page consists of <head> and <body> section.

XMLWorker was meant as a generic framework to parse XML. To avoid these conflicts, iText 7 came into play with convertToPdf function which is capable of converting HTML into PDF.

Following would be the code snippet.

HtmlConverter.convertToPdf(new File(src), new File(dest));

Add your HTML coding into first parameter and provide file location to save HTML into PDF in provided location (file path).

Upvotes: 1

pms
pms

Reputation: 944

my friend just solved it:

XMLWorkerHelper.getInstance().parseXHtml(new XHtmlElementHandler(document), new StringReader(text));

simple :)

Upvotes: 0

Related Questions