【发布时间】:2018-05-09 10:32:11
【问题描述】:
在尝试使用 Apache FOP 来使用自定义字体(Google Lato 字体)时,我遇到了错误。我已根据文档将 ttf font 转换为 xml 并将 ttf 和 xml 文件保存在同一目录中 resource
警告:
Font "Lato,normal,700" not found。替换为“any,normal,700”
警告:Font "Lato,normal,400" not found。替换为“any,normal,400”。
配置文件:
<?xml version="1.0"?>
<fop version="1.0">
<base>.</base>
<source-resolution>72</source-resolution>
<target-resolution>72</target-resolution>
<default-page-settings height="11.00in" width="8.50in"/>
<renderers>
<renderer mime="application/pdf">
<filterList>
<!-- provides compression using zlib flate (default is on) -->
<value>flate</value>
</filterList>
<fonts>
<font metrics-url="Lato-Regular.xml" kerning="yes" embed-url="Lato-Regular.ttf">
<font-triplet name="Lato" style="normal" weight="400"/>
</font>
<font metrics-url="Lato-Bold.xml" kerning="yes" embed-url="Lato-Bold.ttf">
<font-triplet name="Lato" style="normal" weight="700"/>
</font>
</fonts>
</renderer>
</renderers>
</fop>
执行代码:
public static void main(String[] args) throws SAXException, IOException, TransformerException, URISyntaxException {
File fopConf = new File("\\fop.xconf");
FopFactory fopFactory = FopFactory.newInstance(fopConf);
OutputStream out = new BufferedOutputStream(new FileOutputStream(new File("D:/Hello.pdf")));
try {
// Step 3: Construct fop with desired output format
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
TransformerFactory factory = TransformerFactory.newInstance();
Source xslt = new StreamSource(new File(ClassLoader.getSystemResource("resources/foo.xsl").toURI()));
Transformer transformer = factory.newTransformer(xslt);
// Resulting SAX events (the generated FO) must be piped through to FOP
Result res = new SAXResult(fop.getDefaultHandler());
Source src = new StreamSource(new File(ClassLoader.getSystemResource("resources/name.xml").toURI()));
// Step 6: Start XSLT transformation and FOP processing
transformer.transform(src, res);
} finally {
//Clean-up
out.close();
}
}
有什么建议吗?我在谷歌上浪费了一整天,但找不到相关的解决方案。我看过 stackoverflow 帖子提到使用真实/绝对路径、子字体等,但它们似乎都不起作用
FOP 2.2Would use this code on web for dynamic pdf generationJDK 1.8
【问题讨论】:
-
您是否尝试使用示例配置文件运行独立的 FOP?顺便说一句,从 FOP 2.0 版(左右)开始不再需要字体度量文件。您可以肯定地删除此属性
metrics-url。找出问题的最佳方法 - 尝试弄清楚这是 FOP 本身的问题还是嵌入式代码的问题。 -
能否详细说明使用独立 FOP 的情况?我将使用 web 问题中提到的执行代码为一个或多个文件进行动态 pdf 打印。是的,在阅读文档时,我发现将字体转换为 xml 已被弃用,但我似乎仍然无法弄清楚为什么它不断发出警告并且令人惊讶的是没有错误。注意:我刚刚开始使用 FOP...
-
感谢您的建议有效。使用独立它可以工作,我注意到在通过命令行运行时,当我没有指定配置文件时,我得到了相同的警告。指定conf文件位置后,它工作顺利。我的执行代码有同样的问题,它从不读取文件,所以加载了默认配置。我现在已经更正了描述中的执行代码
标签: apache fonts apache-fop