Flying Saucer很强大支持CSS样式,可以直接将html转换成PDF,而且免费跨平台。
下面我来说一下生成PDF页码
开始我以为生成页码会和Itext一样在程序中实现,但是不遂人愿,程序实现是不对的。后来我查了英文说明网站(因为英语不咋的实在懒得看)
http://today.java.net/pub/a/today/2007/06/26/generating-pdfs-with-flying-saucer-and-itext.html#page-specific-features
找到了显示页码的方法是在CSS操作
@page {
size: 4.18in 6.88in;
margin: 0.25in;
-fs-flow-top: "header";
-fs-flow-bottom: "footer";
-fs-flow-left: "left";
-fs-flow-right: "right";
border: thin solid black;
padding: 1em;
}
#header {
font: bold serif;
position: absolute; top: 0; left: 0;
-fs-move-to-flow: "header";
}
#footer {
font-size: 90%; font-style: italic;
position: absolute; top: 0; left: 0;
-fs-move-to-flow: "footer";
}
#pagenumber:before {
content: counter(page);
}
#pagecount:before {
content: counter(pages);
}
还有一段HTML里面代码
<div id="header" style="">Alice's Adventures in Wonderland</div>
<div id="footer" style=""> Page <span id="pagenumber"/> of <span id="pagecount"/> </div>
这样就可以显示了,但是仅能显示一页,把“Page <span id="pagenumber"/> of <span id="pagecount"/>”放到第二页时就显示第二页,没办法不给力啊,页数不能控制。
峰回路转我有找到另一个CSS样式
@page {
size: 4.18in 6.88in;
margin: 0.25in;
border: thin solid black;
padding: 1em;
@bottom-center{
content:"@xxoo有限公司 版权所有";
font-family: SimSun;
font-size: 9px;
color:red;
};
@top-center { content: element(header) };
@bottom-right{
content:"page " counter(page) " of " counter(pages);
font-family: SimSun;
font-size: 9px;
color:red;
};
}
div#myheader {
display: block;
position: running(header);
}
其实第一种方案是过时的写法,第二种才是现在的写法。
转自
http://topic.csdn.net/u/20111012/11/0e65faba-de91-4104-9aa1-eb7cd9c97ce2.html
我稍微改了一下
@page {
margin: 0.25in;
padding: 1em;
@bottom-center{
content:"@OOXX 版权所有";
font-family: SimSun;
font-size: 9px;
color:red;
};
@top-center { content: element(header) };
@bottom-right{
content:"第" counter(page) "页 共 " counter(pages) "页";
font-family: SimSun;
font-size: 9px;
color:#000;
};
}
div#myheader {
display: block;
position: running(header);
}
#pagenumber:before {
content: counter(page);
}
#pagecount:before {
content: counter(pages);
}
生成的例子

- 大小: 5.7 KB

- 大小: 5.9 KB
分享到:
相关推荐
总结,Java提供了一系列工具和库,如Apache PDFBox、iText、Flying Saucer等,用于根据模板动态生成PDF文件。开发者可以根据实际需求和项目规模选择合适的库,并结合模板引擎实现高效的数据绑定和文档生成。在实际...
本教程将探讨如何利用iText库在Spring Boot应用中从HTML页面生成PDF,并进行一系列的定制化操作,如添加页眉、页码、水印、目录以及二维码。 首先,iText是一个强大的Java库,专门用于创建和编辑PDF文档。它提供了...
在Java中,有多个库可以帮助我们生成PDF,如iText、Apache PDFBox、 Flying Saucer等。iText是一个非常流行的开源库,它提供了丰富的API用于创建和编辑PDF文档。根据描述,我们可以推测这个压缩包可能包含一个使用...
例如,iText和Flying Saucer等库支持在转换过程中自动分页,根据设定的纸张大小和边距进行内容布局。 4. **样式处理**:HTML中的CSS样式需要被正确地转换为PDF的样式。这包括字体、颜色、布局等。一些库会尽可能地...
可能使用的有iText、PDFBox或者Flying Saucer等。这些库提供了API来构建PDF文档结构,包括文本、图像、样式等。 3. **水印去除**:在处理Word到PDF的转换时,如果原始Word文档包含水印,转换过程中需要有特殊的处理...
另外,除了PDFBox,还有其他库如iText、 Flying Saucer 和PDFtoImage等,它们也可以完成类似的任务,但API使用和性能可能会有所不同。在选择库时,需要考虑项目的具体需求,如速度、内存消耗、许可证兼容性等因素。 ...