使用飞碟(xHTMLRenderer)和iText生成页眉/页脚 [英] Generating header/footer with flying saucer (xHTMLRenderer) and iText
问题描述
我意识到之前已经问过这个问题(我查看了所有的解决方案并尝试了所有这些)但我仍然在尝试生成一个带有页眉和页脚的pdf文档,这些文档在每个页面上重复。
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.
我使用飞碟
R8与 iText2.0.8
我已经尝试了许多不同的方法来使其工作,但到目前为止无济于事。我测试的一些方法是 https://gist.github.com/626264 ,使用运行元素和边距框 http://pigeonholdings.com/projects/ flyingsaucer / R8 / doc / guide / users-guide-R8.html#xil_40 (css3功能),飞碟r7的指南,不适用于r8 http://今天。 java.net/pub/a/today/2007/06/26/generating-pdfs-with-flying-saucer-and-itext.html#page-specific-features ,很多其他方法做过不适合我。
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.
我的标题 div
包含2个带图像的div,我的页脚仅用于页码编号。 html被放入一个名为buf的 StringBuffer
。
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>");
我的pdf生成正常,除了标题只出现在第一页和仅页脚出现在最后一页的底部。当我把html通过w3c验证器时,它很好,但是当我使用他们的CSS验证器时,它说它们是行中的解析错误 @page {size:8.5in 11in;填充:1em的; @ bottom-left {content:element(footer); }
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); } }
据我所知,我读到的所有指南都很好。我还听说W3C CSS验证器对于CSS3规范是不完整的,所以我认为它是验证器错了。
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 :)
PS必须使用飞碟R8和/或iText 2.0.8
p.s. Has to use Flying saucer R8 and/or iText 2.0.8
推荐答案
以下是一个工作示例:
package com.sg2net.test;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import org.xhtmlrenderer.pdf.ITextRenderer;
import com.lowagie.text.DocumentException;
public class XHTMLRenderer8 {
/**
* @author Giovanni Cuccu
*/
public static void main(String[] args) throws FileNotFoundException, DocumentException {
ITextRenderer renderer = new ITextRenderer();
String content="<html><head><style>\n" +
"div.header {\n" +
"display: block; text-align: center;\n" +
"position: running(header);}\n" +
"div.footer {\n" +
"display: block; text-align: center;\n" +
"position: running(footer);}\n" +
"div.content {page-break-after: always;}" +
"@page { @top-center { content: element(header) }}\n " +
"@page { @bottom-center { content: element(footer) }}\n" +
"</style></head>\n" +
"<body><div class='header'>Header</div><div class='footer'>Footer</div><div class='content'>Page1</div><div>Page2</div></body></html>";
renderer.setDocumentFromString(content);
renderer.layout();
renderer.createPDF(new FileOutputStream("test.pdf"));
}
}
这是使用以下XHTML文档
This is using the following XHTML document
<html>
<head>
<style>
div.header {
display: block; text-align: center;
position: running(header);
}
div.footer {
display: block; text-align: center;
position: running(footer);
}
div.content {page-break-after: always;}
@page {
@top-center { content: element(header) }
}
@page {
@bottom-center { content: element(footer) }
}
</style>
</head>
<body>
<div class='header'>Header</div>
<div class='footer'>Footer</div>
<div class='content'>Page1</div>
<div>Page2</div>
</body>
</html>
这篇关于使用飞碟(xHTMLRenderer)和iText生成页眉/页脚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!