Discussion:
[iText-questions] Converting HTML with ordered list and div with itextpdf
Peter Varga
2015-06-26 12:25:48 UTC
Permalink
Hi,

I tried to convert a HTML page with ordered list and some div tags with
itextpdf, but it does not seem to work. I created the following simple
ordered list structure in HTML to demonstrate the problem:


<!DOCTYPE html>

<html>

<head>

<title>Insert title here</title>

</head>

<body>

<!-- <div> -->

<ol>

<li>A

<ol>

<li>AA</li>

<li>AB</li>

<li>AC

<ul>

<li>ACA</li>

<li>ACB</li>

</ul>

</li>

<li>AD

<ul>

<li>ADA</li>

<li>ADB</li>

</ul>

</li>

</ol>

</li>

</ol>

<!-- </div> -->

</body>

</html>

It works fine so far, and I get the correct, well formatted PDF. When,
however, I remove the comment around the div tag, it does not work any
more. I get all element of the ordered list in a single line, in the result
PDF file like this:


A AAABAC ACAACBAD ADAADB

My method for the conversion looks like this:


private void convertHtmlToPdf(String htmlFilePath, String
generatedPdfFilePath) throws IOException, FileNotFoundException,
DocumentException {

String fileContent = FileUtils.readFileToString(new File(htmlFilePath));

OutputStream file = new FileOutputStream(new
File(generatedPdfFilePath));

Document document = new Document();

PdfWriter writer = PdfWriter.getInstance(document, file);

document.open();

InputStream is = new ByteArrayInputStream(fileContent.getBytes());

XMLWorkerHelper.getInstance().parseXHtml(writer, document, is);

document.close();

file.close();

}


So it is the simplest conversion code. I don't use any CSS, or insert any
pictures. In my real code however, I do need to use CSS for the conversion,
and the formatting is defined via div elements. So I can not simple remove
all the div-s to get the conversion working. Is it a bug, in itextpdf, or I
miss something? Has anybody any idea for a workaround, to deal with this
problem?

I am using the actually latest (5.5.6) version of itextpdf.

Loading...