使用Flying Saucer生成pdf报表:
1.首先看一下Flying Saucer的官方介绍:
https://github.com/flyingsaucerproject/flyingsaucer
OVERVIEW
Flying Saucer is a pure-Java library for rendering arbitrary well-formed XML (or XHTML) using CSS 2.1 for layout and formatting, output to Swing panels, PDF, and images.
Comprehensive documentation available in our user's guide, linked from our website at http://code.google.com/p/flying-saucer/ For information on our development releases, please contact us on our mailing lists.
If you end up using Flying Saucer for your own projects, please drop us an email and tell us about it; it helps inform where we go next, and is interesting and inspiring to other developers.
可以看到Flying Saucer可以完美的解析html,并且可以使用css2.1的样式,添加图片等功能。
2.简单的使用:
业务上经常有这样的场景:
将用户信息查询出来并且显示在页面上设计好的table上。并且可以选择下载。
解决方法:
1.)使用java模板(velocity、freemarker等)定义好表格样式,这里我们使用freemarker
引入freemarker
<!-- https://mvnrepository.com/artifact/org.freemarker/freemarker -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
创建ftl模板
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
.tg {
border-collapse: collapse;
border-spacing: 0;
}
.tg td {
font-family: 'Arial Unicode MS';
font-size: 14px;
padding: 16px 5px;
border-style: solid;
border-width: 1px;
overflow: hidden;
word-br/eak: normal;
}
.tg th {
font-family:'Arial Unicode MS';
font-size: 14px;
font-weight: normal;
padding: 16px 5px;
border-style: solid;
border-width: 1px;
overflow: hidden;
word-break: normal;
}
.tg .tg-s6z2 {
text-align: center
}
.tg .tg-baqh {
text-align: center;
vertical-align: top
}
.tg .tg-yw4l {
vertical-al