2017-09-15 169 views
1

我使用flyingsaucer 9.1.1和itext 2.17来生成pdf文件。仅在最后一页显示页脚

我怎样才能显示页脚(可能是一个div,表或img元素)只在最后一页,但没有在其他的?

+1

iText的2.1.7是我曾经写过的API,并[不应再被使用(https://developers.itextpdf.com/question/versions-older-than-5)。其中一个原因是你不再使用它的原因之一是它的年龄。 iText 2.1.7的日期从2009年开始。您还在使用飞碟。这是一个旧的第三方库,在旧的iText版本上编写。你应该考虑使用[iText 7 + pdfHTML](https://developers.itextpdf.com/content/itext-7-converting-html-pdf-pdfhtml)。浏览本教程,您会注意到[pdfHTML](https://itextpdf.com/itext7/pdfHTML)中有更多对CSS的支持。 –

+1

至于你的问题,你可以使用'IEventHandler'创建一个显示自定义行为的页脚,如果你决定做正确的事情并升级到iText 7。[教程](https://developers.itextpdf.com/content/itext-7-converting-html-pdf-pdfhtml/chapter-4-creating-reports-using-pdfhtml)有一个自定义页眉或页脚可以如何创建的例子。您可能还想阅读[此评论](https://stackoverflow.com/questions/46238645/utf-8-encoding-characters-not-working-with-spring-mvc-lowagie-itext#comment79441200_46238645) –

回答

3

不幸的是,CSS定义了page:first伪元素,但没有定义page:last

您仍有可能利用飞碟的bug,这使得页脚仅在声明后才出现。因此,如果您将页脚div置于HTML的末尾,它将只出现在最后一页上。

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<style type="text/css"> 
    .newpage{page-break-before:always} 

    @page{@bottom-center {content: element(footer)}} 
    #footer {position: running(footer);}  
} 
</style> 
</head> 
    <body> 
     <div>Page 1 content</div> 
     <div class="newpage">Page 2 content</div> 
     <div class="newpage">Page 3 content</div> 
     <div id="footer">Footer on last page</div> 
    </body> 
</html> 

它也可以,如果你想在每一页上一个页脚,具有不同内容的最后一页。您只需在HTML中定义页脚div两次。

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<style type="text/css"> 
    .newpage{page-break-before:always} 

    @page{ 
     @bottom-center {content: element(footer)}} 
     #footer {position: running(footer);}  
    } 
</style> 
</head> 
    <body> 
     <div id="footer">Footer for all pages except last</div> 
     <div>Page 1 content</div> 
     <div class="newpage">Page 2 content</div> 
     <div class="newpage">Page 3 content</div> 
     <div id="footer">Footer on last page</div> 
    </body> 
</html> 
相关问题