Please see my answer regarding specific font handling.
Considering the PDF file generation itself, I'm used to build my own PDF table and fill its cells with something like below, the htmlContext
and the cssResolver
being defined as reusable class attributes:
private ElementList getElementsFromHtml(final String html) throws IOException {
//...
// Pipelines
final ElementList elements = new ElementList();
final ElementHandlerPipeline end = new ElementHandlerPipeline(elements, null);
final HtmlPipeline htmlPipeline = new HtmlPipeline(htmlContext, end);
final CssResolverPipeline cssPipeline = new CssResolverPipeline(cssResolver, htmlPipeline);
// XML Worker
final XMLWorker worker = new XMLWorker(cssPipeline, true);
final XMLParser p = new XMLParser(true, worker, htmlContext.charSet());
final String resolvedHtml = "<body>" + html + "</body>";
p.parse(new ByteArrayInputStream(resolvedHtml.getBytes(htmlContext.charSet())), htmlContext.charSet());
return elements;
}
public void addHtml(final PdfPCell cell, final String html) throws IOException {
for (final Element e : getElementsFromHtml(html)) {
if (ColumnText.isAllowedElement(e)) {
cell.addElement(e);
} else {
LOG.error(String.format("### Element not allowed! ###\nElement (type: %d): %s\nContext: %s", e.type(), e.toString(), html));
}
}
}