In my application I've to generate a report from the html content. For this purpose I am using the iTextPDF library 5.5.0 with XMLWorker 5.5.0. End user can design the content of report (Header, Footer, Subject) using tinyMC editor. I am storing those HTML content in DB. Please note this HTML content may include images, tables or any thing that tinyMC allows. Now when user tries to generate the report, I am fetching the data from the database and tried to generate the header and footer from HTML using code given below.
/**
* Adds the header and the footer.
* @see com.itextpdf.text.pdf.PdfPageEventHelper#onEndPage(
* com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)
*/
public void onEndPage(PdfWriter writer, Document document) {
Phrase footerPhrase = new Phrase();
DBFooter = "<table width='200 border='1' cellpadding='1' cellspacing='1'><tbody><tr><td>content1</td><td>content2</td></tr><tr><td> </td><td> </td>
</tr><tr><td>jhvhjvh</td><td><img src='D:\DemoApp\images\User1.png' width='48' height='48' alt='' /></td></tr></tbody></table>
<p style='text-align: center;'>This is the footer</p>"
CSSResolver cssResolver = new StyleAttrCSSResolver();
CssFile cssFile = XMLWorkerHelper.getCSS(new ByteArrayInputStream(CSS.getBytes()));
cssResolver.addCss(cssFile);
// HTML
HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
// Pipelines
ElementList elements = new ElementList();
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);
try {
p.parse(new ByteArrayInputStream(DBFooter.toString().getBytes()));
} catch (IOException e) {
}
footerPhrase.addAll(elements);
Rectangle rect = writer.getBoxSize("art");
switch (writer.getPageNumber() % 2) {
case 0:
ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_RIGHT, header[0], rect.getRight(),
rect.getTop(), 0);
break;
case 1:
ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_LEFT, header[1], rect.getLeft(),
rect.getTop(), 0);
break;
}
ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, footerPhrase,
(rect.getLeft() + rect.getRight()) / 2, rect.getBottom() - 50, 0);
}
I've followed this link to add header footer. And changed the onEndPage function as above.
Problem is the above code only prints the "This is the footer" from the content of footer. It's not showing the table. How can I bring the HTML content in Header / Footer.
Please note that I can show html content in report body (including table / images etc.) but not in header / footer.