熬到半夜,终于整出来了。
第一步,引入fo-1.0.jar
<dependency> <groupId>org.apache.xmlgraphics</groupId> <artifactId>fop</artifactId> <version>1.0</version> </dependency>
第二步,编写1个测试fo,暂且叫做test01.xml
Eclipse中配置外部dtd的方法,见ws-JAX中关于dtd的那篇文章
<?xml version="1.0" encoding="UTF-8"?> <!-- 引入dtd,提供提示功能 --> <!DOCTYPE xsl-fo SYSTEM "fo.dtd"> <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> <fo:layout-master-set> <fo:simple-page-master master-name="LetterPage"> <fo:region-body region-name="PageBody"/> </fo:simple-page-master> </fo:layout-master-set> <fo:page-sequence master-reference="LetterPage"> <fo:flow flow-name="PageBody" font-family="Microsoft YaHei" font-size="24pt"> <fo:block>Hello XSL-FO! 成功!</fo:block> </fo:flow> </fo:page-sequence> </fo:root>
第三步,引入字体,FOP默认提供的字体没得搞,不支持中文的。
以微软雅黑为例,该字体可以从windows/fonts目录中拷贝。
将fop.xconf配置放到java/main/resources目录下
并在java/main/resources目录下新建1个fonts目录
将windows/fonts目录下的需要的字体拷贝到resources/fonts目录中
打开fop.xconf文件,将微软雅黑字体配置进去
采用最简单的方法,提供1个字体目录,让fop到此目录下找需要的字体
注意:
fop配置文件中有一个相对路径的默认配置<base>.</base>
该路径指向的是项目的根目录,所以在指定字体目录时,是参考根目录写相对路径的
而不是文档中所描述的:Relative config url's will be resolved relative to the location of this file.
<renderer mime="application/pdf"> <filterList> <!-- provides compression using zlib flate (default is on) --> <value>flate</value> </filterList> <fonts> <directory>src/main/resources/fonts</directory> </fonts> <!-- This option lets you specify additional options on an XML handler --> <!--xml-handler namespace="http://www.w3.org/2000/svg"> <stroke-text>false</stroke-text> </xml-handler--> </renderer> .....
第四步,在程序中指定fop.xconf文件的位置,让fop在渲染时根据这个配置文件进行读取字体。
String path = PdfApplication.class.getClassLoader().getResource("fop.xconf").getPath(); fopFactory.setUserConfig(path);
第五步,运行程序,不出意外,将生成期待已久的PDF!
补充:
如果使用DTD校验fo,则引入其它命名空间会报错!
使用apache提供的xsd对fo进行校验,再引入thymeleaf的命名空间就不报错了。
附件中的几个文件的说明:
xconf is font character setting file
dtd is provided by RenderX
xsd is provided by apache FOP
相关推荐
XML+XSL/FO生成PDF文件Demo,主要用于Fop插件,算是个小型练习项目
xsl-fo pdfxsl-fo pdfxsl-fo pdfxsl-fo pdfxsl-fo pdfxsl-fo pdfxsl-fo pdfxsl-fo pdfxsl-fo pdfxsl-fo pdfxsl-fo pdfxsl-fo pdf
FO PDF is similar to ASP.NET Server Controls, written in C#.It takes DataTable and few other params to generate the XSL FO and renders a DataGrid like PDF Report using NFOP (Apache FOP Port in J#) PDF...
【学习 XSL-FO】.pdf
藏经阁-Deep Reinforcement Learning fo.pdf
藏经阁-JUST-IN-TIME ANALYTICS NEED FO.pdf
word,html转fo开源程序,能将word,html文件转换成fo文件
Foxit PDF Editor 是第一个真正的PDF文件编辑软件。许多人都希望能找到一个象编辑其它类型的文档的编辑器,事实上在 Foxit PDF Editor 出现之前,根本没有这样的工具。每一个PDF 文件都包含很多页面,每一页包含各种...
DAOS_A_Scale-Out_High_Performance_Storage_Stack_fo.pdf
注意,第82页开始为中文说明 夏普FO-11cn,FO-51cn,FO-71cn传真机说明书用户手册下载.pdf
XSL-FO 简介 XSL-FO 的简介。包括其概念和作用。 XSL-FO 文档 本章将解 XSL-FO 文档的结构。 XSL-FO 区域 本章讲解 XSL-FO 的区域模型(area model)。 XSL-FO 输出 本章讲解 XSL-FO 文档的输出元素(Output ...
样本pdf 示例 XML 到 FO 到 PDF spring-boot 应用程序。 示例 spring-boot 项目使用 xslt 样式将 xml 数据转换为 fo: 然后使用 apache FOP 将其转换为 pdf 文件
WSL 环境准备Windows 商店安装 Ubuntu 20.04搭建 Node.js 开发环境使用 WSL 环境1. Command 'nvm' not fo
夏普FO-50传真机维修手册.pdf 英文版 方便 应用
GLOBAL STATUS REPORT ON ROAD SAFETY Time fo Action.pdf
信息安全_数据安全_Hacking Your Security Culture fo 安全响应 解决方案 工控安全 安全测试 水坑攻击
信息安全_数据安全_A Probabilistic Verification Scheme fo 工业网络 安全架构云安全 风险分析 工控安全
STM32FO系列:MCU.pdf