I realize this question has been asked before (I looked at all the solutions and tried them all) but I am still trying to generate a pdf document with a header and footer that repeat on every page.
I am using flying saucer
R8 with iText2.0.8
I have tried many different methods to get it working but so far to no avail. Some methods I tested out were https://gist.github.com/626264, using running elements and margin boxs http://pigeonholdings.com/projects/flyingsaucer/R8/doc/guide/users-guide-R8.html#xil_40 (css3 feature), a guide for flying saucer r7 that doesn't work for r8 http://today.java.net/pub/a/today/2007/06/26/generating-pdfs-with-flying-saucer-and-itext.html#page-specific-features, a long with many other methods which did not work for me.
My header div
contains 2 other divs with images and my footer is just for page numbering. The html is being put into a StringBuffer
called buf.
buf.append("<head>");
buf.append("<title>blabla</title> ");
buf.append("<style type='text/css' media='print'> ");
buf.append("@page { size:8.5in 11in; padding:1em; @bottom-left { content: element(footer); } } ");
buf.append("#footer { font-size: 90%; font-style: italic; position: running(footer); top: 0; left: 0; }");
buf.append("#pagenumber:before { content: counter(page); } ");
buf.append("#pagecount:before { content: counter(pages); } ");
buf.append("</style></head>");
buf.append("<body>");
buf.append("<div class='header' style='clear:both;'>");
buf.append("<div id='moneyLogo' style='float:left'>");
buf.append("<img src='logo.jpg' alt='Some alt text' />");
buf.append("</div>");
buf.append("<div id='canLogo' style='float:right'>");
buf.append("<img src='someImg.gif' alt='alt text' />");
buf.append("</div>");
buf.append("<h3 style='text-align:center; clear:both;'>alt text</h3>");
buf.append("<div style='text-align:center;'>");
buf.append("Some texy text");
buf.append("<br />"););
buf.append("</div>");
buf.append("</div><br /><br />");
buf.append("<div id='footer'> Page <span id='pagenumber'/> of <span id='pagecount'/> </div>");
buf.append("</body>");
buf.append("</html>");
My pdf generates fine except for the fact that the header only appears on the first page and the footer only appears on the bottom of the last page. When I put the html through the w3c validator it came out fine, but when I used their CSS validator it said that their were parse errors in the line @page { size:8.5in 11in; padding:1em; @bottom-left { content: element(footer); } }
As far as I could tell from all the guides I was reading this was fine. I also heard that the W3C CSS validator was incomplete for CSS3 specs so I assumed it was the validator who was wrong.
If anyone could give me some tips of where to look or ideas it would make my week :)
p.s. Has to use Flying saucer R8 and/or iText 2.0.8
Here is a working example:
This is using the following XHTML document
After researching and testing a lot, I just came up with a solution that really works.
You can control:
- header height by editing margin-top;
- footer height by editing margin-bottom;
- content width by editing div.content width.
Page numbers are displayed at footer.
See code below:
Hope it helps!!!
for itext using java checkout this
using below one in ur pdfwriter