java 使用飞碟,如何在页脚的每一页上生成带有页码和总页数的 pdf?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17953829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
With Flying Saucer, how do I generate a pdf with a page number and page total on every page at the footer?
提问by Daniel Kaplan
It took me a little more research than I would have liked to figure this out on my own, so I'm going to post a comprehensive answer here. It seems like the information to do this is spread across many different websites and I'd like to put it in one place. This answermaybe the same thing, but my eyes glaze over because it's in a Java string and not in a html template. Here's the question:
我花了比我想自己解决的更多的研究,所以我将在这里发布一个全面的答案。似乎这样做的信息分布在许多不同的网站上,我想把它放在一个地方。 这个答案可能是一样的,但我的眼睛呆滞了,因为它在 Java 字符串中,而不是在 html 模板中。这是问题:
I'm rendering a PDF and I want a footer at the bottom of the page that says, "Page n of m" where "n" is the page number you're on and "m" is the total pages in the document. How do I do that?
我正在渲染一个 PDF,我想要在页面底部有一个页脚,上面写着“第 n 页”,其中“n”是您所在的页码,“m”是文档中的总页数。我怎么做?
回答by Daniel Kaplan
In your html, you need to put this somewhere in the body
tag:
在你的 html 中,你需要把它放在body
标签中的某个地方:
<div id="footer">
page <span id="pagenumber"></span> of <span id="pagecount"></span>
</div>
And this should be your css/style:
这应该是你的 css/style:
#footer {
position: running(footer);
text-align: right;
}
@page {
@bottom-right {
content: element(footer);
}
}
#pagenumber:before {
content: counter(page);
}
#pagecount:before {
content: counter(pages);
}
You can learn more about that @bottom-right
option here.
您可以在此处了解有关该@bottom-right
选项的更多信息。
回答by John Mark
You could also try a slightly more succinct approach:
您还可以尝试更简洁的方法:
@page {
@bottom-right {
content: "Page " counter(page) " of " counter(pages);
}
}
You need to put this in your css/style, and you do not need to add an extra footer HTML element.
你需要把它放在你的 css/style 中,并且不需要添加额外的页脚 HTML 元素。
回答by Adrian Higgins
Important Error & Solution!
重要错误和解决方案!
For Starters, using John Mark's solution below is the CORRECT way to get it working:
对于初学者来说,使用下面的 John Mark 解决方案是让它工作的正确方法:
@page {
@bottom-right {
content: "Page " counter(page) " of " counter(pages);
}
}
However many people may have encountered the following issuewhere your PDF HAS STOPPED GENERATING!
然而,许多人可能遇到过以下问题,即您的PDF 已停止生成!
This is solved by updating your VERSIONof FLYING SAUCER.
这可以通过更新您的飞碟版本来解决。
I was running 9.1.4when my PDF stopped generating due to the @page & @bottom-rightcss code.
当我的 PDF 由于@page 和 @bottom-rightcss 代码而停止生成时,我正在运行9.1.4。
This was a glitch in Flying Saucerthat was fixed in version 9.1.5!
这是飞碟中的一个小故障,已在9.1.5版本中修复!
Make sure to update to 9.1.5and use John Mark's answer.
确保更新到9.1.5并使用 John Mark 的回答。
回答by Matt Morgan
@John Mark 's solution worked for me. In my application I needed to embed the style info in the HTML, so I wrapped this in <style>
tags and put it inside the <head>
before <title>
. Maybe that info will be useful to someone else.
@John Mark 的解决方案对我有用。在我的应用程序中,我需要将样式信息嵌入到 HTML 中,因此我<style>
将其包装在标签中并将其放在<head>
before 中<title>
。也许这些信息对其他人有用。