1

I am converting html to pdf using iText 5.5.9 XMLWorker.

<html>
<head></head>
<body>
<div style="width:100%; height:100%;">
    <div>This is sample text.
        <ol>
            <li>
                This is outer list item first
                <ul>
                    <li>Inner list item first</li>
                    <li>Inner list item second</li>
                    <li>Inner list item third</li>
                    <li>Inner list item fourth</li>
                    <li>Inner list item fifth</li>
                </ul>
            </li>
            <li>
                This is outer list item second
            </li>
        </ol>
    </div>
</div>
</body>
</html>

and output I am receiving for nested list is:

enter image description here

Code for conversion is:

public void createPdf_Using_ElimentList(String outPutPath) {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outPutPath));
    document.open();
    PdfPCell tempCell = new PdfPCell();
    ElementList elements = null;
    tempCell.setBorder(Rectangle.NO_BORDER);

    elements = parseToElementList_Unicode(clean_str, css);
    if (elements != null) {
        tempCell.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
        for (com.itextpdf.text.Element e : elements) {
            tempCell.addElement(e);
        }
        tempCell.setPadding(5);
        tempCell.setPaddingBottom(8);

        table.setSplitLate(false);
        table.addCell(tempCell);
        success = true;
    }
    document.add(newTable);

    document.close();
}

public static ElementList parseToElementList_Unicode(String HTMLContent, String CSSContent) {
    PdfPCell tempCell = new PdfPCell();
    ElementList elements = new ElementList();
    CSSResolver cssResolver = new StyleAttrCSSResolver();
    XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);

    try {
        InputStream cssIS = new ByteArrayInputStream(CSSContent.getBytes());
        InputStream HTML_IS = new ByteArrayInputStream(HTMLContent.getBytes());

        String dirName = "C:/Tasks/Fonts/";

        fontProvider.register(dirName + "arial.ttf", "Arial");
        CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);
        // HTML
        HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers);
        htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
        // Pipelines

        ElementHandlerPipeline pdf = new ElementHandlerPipeline(elements, null);
        HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
        CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);

        // XML Worker
        XMLWorker worker = new XMLWorker(css, true);
        XMLParser p = new XMLParser(worker);

        p.parse(HTML_IS, Charset.forName("UTF-8"));


    } catch (Exception e) {
        e.printStackTrace();
    }
    return elements;

}

Using iText 7 is not option for me as its not backward compatible. Do anyone have solution for iText 5.5.9. Thanks in advance.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
HappyRahul
  • 23
  • 2
  • 8
  • As documented, nested lists aren't supported in the context of tables. There is no solution for your problem in iText 5.5.9 or later (iText 5.8.9 doesn't exist) and we have no plans to fix this problem either. We've tried, but is was too difficult. We decided to invest in a complete rewrite (that is: iText 7). – Bruno Lowagie Oct 08 '16 at 12:01
  • I closed the question. If you want an answer, you will have to download the iText source code, fix the problem yourself, and share your fix with the community. – Bruno Lowagie Oct 08 '16 at 12:09
  • @BrunoLowagie I do not see XMLWorker in iText 7, (probably it's under development right now), is there any estimated timeframe on the release of that? just to plan accordingly. – HappyRahul Oct 13 '16 at 20:30
  • @BrunoLowagie if I get to know rough time frame for iTextWorker 7 release, 1/2 months or a year? it will help me. Thanks for your help in advance. – HappyRahul Nov 14 '16 at 17:41
  • The iText 7 implementation of iText 5's XML Worker should be released in February 2017. – Bruno Lowagie Nov 15 '16 at 06:54

0 Answers0