1.导入pom依赖
<!-- https://mvnrepository.com/artifact/org.apache.xmlgraphics/fop -->
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>fop</artifactId>
<version>2.3</version>
</dependency>
2.查看官方案例
当前的方式为(来自官方的demo):使用的方式
所以需要创建pdf文件需要两个东西:
1.xml配置文件用于存储所需要的数据
2.xsl文件用来作为动态解析的对象
3.什么是XSL?
个人查看:w3school中的内容
所以可以使用xsl:for-each来迭代数据,使用xsl:value-of来绑定数据
3.如何在当前的xsl中使用表格和其他样式
本人使用菜鸟教程中的fo
所以可以使用fo标签来制作表格
4.开始编写helloworld.xml以及helloworld.xsl文件
helloworld.xml
<?xml version="1.0" encoding="UTF-8"?>
<users>
<title>当前所有的用户列表</title>
<user>
<name>admin</name>
<age>18</age>
<onwork>true</onwork>
<isleaf>yes</isleaf>
</user>
<user>
<name>guest</name>
<age>20</age>
<onwork>false</onwork>
<isleaf>no</isleaf>
</user>
</users>
helloworld.xsl
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<fo:root>
<fo:layout-master-set>
<fo:simple-page-master master-name="A4-portrait"
page-height="29.7cm" page-width="21.0cm" margin="2cm">
<fo:region-body/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="A4-portrait">
<fo:flow flow-name="xsl-region-body">
<fo:table>
<fo:table-header>
<fo:table-row>
<fo:table-cell>
<fo:block font-weight="bold" font-size="25pt"><xsl:value-of select="users/title"/></fo:block>
</fo:table-cell>
</fo:table-row>
<fo:table-row>
<fo:table-cell>
<fo:block font-weight="bold">name</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block font-weight="bold">age</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block font-weight="bold">onwork</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block font-weight="bold">isleaf</fo:block>
</fo:table-cell>
</fo:table-row>
</fo:table-header>
<fo:table-body>
<xsl:for-each select="users/user">
<fo:table-row>
<fo:table-cell>
<fo:block><xsl:value-of select="name"/></fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block><xsl:value-of select="age"/></fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block><xsl:value-of select="onwork"/></fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block><xsl:value-of select="isleaf"/></fo:block>
</fo:table-cell>
</fo:table-row>
</xsl:for-each>
</fo:table-body>
</fo:table>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
</xsl:stylesheet>
5.开始编写启动程序
当前的xml和xsl文件放在src目录中
public static void main(String[] args) {
Charset utf8 = Charset.forName("UTF-8");
try {
System.out.println("FOP ExampleXML2PDF\n");
System.out.println("Preparing...");
// Setup directories
File baseDir = new File("src");
File outDir = new File(baseDir, "out");
outDir.mkdirs();
// Setup input and output files
File xmlfile = new File(baseDir, "helloworld.xml");
File xsltfile = new File(baseDir, "helloworld.xsl");
File pdffile = new File(outDir, "helloworld.pdf");
System.out.println("Input: XML (" + xmlfile + ")");
System.out.println("Stylesheet: " + xsltfile);
System.out.println("Output: PDF (" + pdffile + ")");
System.out.println();
System.out.println("Transforming...");
// configure fopFactory as desired
final FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI());
FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
// configure foUserAgent as desired
// Setup output
OutputStream out = new java.io.FileOutputStream(pdffile);
out = new java.io.BufferedOutputStream(out);
try {
// Construct fop with desired output format
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
// Setup XSLT
TransformerFactory factory = TransformerFactory.newInstance();
FileInputStream fis=new FileInputStream(xsltfile);
Transformer transformer = factory.newTransformer(new StreamSource(new InputStreamReader(new FileInputStream(xsltfile),utf8 )));
// Set the value of a <param> in the stylesheet
transformer.setParameter("versionParam", "2.0");
// Setup input for XSLT transformation
Source src = new StreamSource(new InputStreamReader(new FileInputStream(xmlfile),utf8 ));
// Resulting SAX events (the generated FO) must be piped through to FOP
Result res = new SAXResult(fop.getDefaultHandler());
// Start XSLT transformation and FOP processing
transformer.transform(src, res);
} finally {
out.close();
}
System.out.println("Success!");
} catch (Exception e) {
e.printStackTrace(System.err);
System.exit(-1);
}
}
6.执行的结果
发现出现了一大堆警告
,当前的字符字体不存在!
查看生成的pdf文件:
生成成功,但是中文不能显示,这个有问题
7.总结
1.使用fop输出数据为pdf文件比较复杂,需要对xsl文件的迭代和xsl的样式和一定的知识
2.使用fop好像默认不能输出中文,输出的中文都变成了#号表示了
3.使用fop需要两个文件一个是数据文件xml以及另外一个文件xsl
4.个人感觉使用fop生成pdf文件过于复杂
以上纯属个人简介,如有问题请联系本人!