1,fop依赖
<!-- https://mvnrepository.com/artifact/org.apache.xmlgraphics/fop -->
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>fop</artifactId>
<version>2.4</version>
</dependency>
2,简单实现
// 步骤1:构建一个FopFactory
FopFactory fopFactory = null;
try {
// 载入fop配置文件,需要下载
fopFactory = FopFactory.newInstance(new File("fop\\fop.xconf"));
// 步骤2:设置输出流。
OutputStream out = new BufferedOutputStream(new FileOutputStream(new File("abc.pdf")));
// 步骤3:使用所需的输出格式构造fop
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
// 步骤4:使用变压器设置JAXP
TransformerFactory factory = TransformerFactory.newInstance();
Source xslt = new StreamSource(new File("fop/index.xsl"));
Transformer transformer = factory.newTransformer(xslt);
// 步骤5:设置XSLT转换的输入和输出
// 设置输入流
Source src = new StreamSource(new File("fop/index.xml"));
// 产生的SAX事件(生成的FO)必须通过管道传递到FOP
Result res = new SAXResult(fop.getDefaultHandler());
System.out.println("开始XSLT转换和FOP处理。。。");
// 步骤6:开始XSLT转换和FOP处理
transformer.transform(src, res);
System.out.println("完成。。。");
// 关闭流
out.close();
} catch (Exception e) {
e.printStackTrace();
}