Skip to content Skip to sidebar Skip to footer

Itext Partial Html Rendering

I am using iText PDF library for Java in order to generate PDF. I want to partially render some HTML content instead of the whole document. Here is the section that I want to parti

Solution 1:

If weit.getUnit() returns HTML, than you will see that HTML code in your cell if you use the snippet shown in your question.

To avoid this, you need to render the HTML to a list of iText objects. This is shown in the first part of the ParseHtmlObjects example:

// CSSCSSResolvercssResolver=
        XMLWorkerHelper.getInstance().getDefaultCssResolver(true);
// HTMLHtmlPipelineContexthtmlContext=newHtmlPipelineContext(null);
htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
htmlContext.autoBookmark(false);
// PipelinesElementListelements=newElementList();
ElementHandlerPipelineend=newElementHandlerPipeline(elements, null);
HtmlPipelinehtml=newHtmlPipeline(htmlContext, end);
CssResolverPipelinecss=newCssResolverPipeline(cssResolver, html);
// XML WorkerXMLWorkerworker=newXMLWorker(css, true);
XMLParserp=newXMLParser(worker);
p.parse(newFileInputStream(HTML));

Now you have an object elements with iText objects that you can add to a cell:

PdfPCellcell=newPdfPCell;
for (Element e : elements) {
    cell.addElement(e);
}

Suppose that the HTML returned by weit.getUnit() contains more data than you need, then it is very hard for iText to read your mind and to find out which part you want to keep and which part you want to throw away.

Maybe you are only interested in specific element types. In that case, you can examine whether e is a Paragraph, or a List, or any other of the types that are available in iText.

Or maybe you can reduce the HTML to the part that needs to be rendered up-front.

In any case: you should not expect that a computer can guess which parts of some HTML are important to you and which parts aren't ;-)

Post a Comment for "Itext Partial Html Rendering"