gpt4 book ai didi

java - 使用 Flying Saucer (xHTMLRenderer) 和 iText 生成页眉/页脚

转载 作者:IT老高 更新时间:2023-10-28 20:27:12 29 4
gpt4 key购买 nike

我意识到之前有人问过这个问题(我查看了所有解决方案并尝试了所有解决方案),但我仍在尝试生成一个带有在每一页上重复的页眉和页脚的 pdf 文档。

我正在使用 flying saucer 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://today.java.net/pub/a/today/2007/06/26/generating-pdfs-with-flying-saucer-and-itext.html#page-specific-features ,很长,还有许多其他方法对我不起作用。

我的页眉 div 包含其他 2 个带有图像的 div,我的页脚仅用于页码。 html 被放入名为 buf 的 StringBuffer 中。

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 生成良好,除了页眉只出现在第一页而页脚只出现在最后一页的底部。当我通过 w3c validator 将 html 放入时,结果很好,但是当我使用他们的 CSS validator 时,它说它们是 @page { size:8.5in 11in; 行中的解析错误。填充:1em; @bottom-left { 内容:元素(页脚); } }

据我所知,我正在阅读的所有指南都很好。我还听说 W3C CSS validator 对于 CSS3 规范是不完整的,所以我认为是 validator 错了。

如果有人可以给我一些关于在哪里寻找或想法的提示,那将是我的一周:)

附言必须使用飞碟 R8 和/或 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 文档

<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>

关于java - 使用 Flying Saucer (xHTMLRenderer) 和 iText 生成页眉/页脚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8263416/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com