【发布时间】:2014-12-30 09:18:03
【问题描述】:
我使用嵌入在 Java 应用程序中的 Apache FOP 1.1 成功地将 XML 转换为 PDF。不幸的是,我被指示降级到 FOP 1.0 以遵守公司许可。所以,我用 1.1 jar 替换了 1.0 jar,并交叉检查了依赖 jar 的兼容性。一切正常,程序运行良好。但是,FOP 1.0 生成的 PDF 仅包含每个字符的“#”错误:
Glyph "M" (0x4d, M) not available in font "Helvetica".
(此处,“M”仅用作示例。但它会针对文件中的每个字符显示此错误。)我在 Apache 站点上查找了此错误,其中指出:
If no glyph can be found for a given character, FOP will issue a warning and use the glpyh for "#" (if available) instead.
我觉得这很奇怪,因为这段代码适用于 FOP 1.1,而且我使用的是基本的字母数字字符。 (即,即使是“Hello World”也无法正确打印”)。我的转换代码基于 Apache 站点的示例,非常简单
//Constructs a fop with desired output format and user settings
Fop fop = fopFactory.newFop("application/pdf", agent, out);
//Setup JAXP using identity transformer direct from XSLT:FO output template
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new StreamSource(new StringReader(xslfoReceiptLayout))); // identity transformer
//Setup input stream from XML string
Source src = new StreamSource(new StringReader(receiptXml));
//Resulting SAX events msut be piped through to FOP
Result res = new SAXResult(fop.getDefaultHandler());
//Start XSLT transformation and FOP processing
transformer.transform(src, res);
logger.debug("Finished XML to PDF conversion");
xslfoReceiptLayout 是一个包含我的 xslfo 数据的字符串。我尝试将这些数据简化为 W3 站点中的示例,效果相同:
<?xml version="1.0" encoding="UTF-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="A4">
<fo:region-body />
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="A4">
<fo:flow flow-name="xsl-region-body">
<fo:block>Hello W3Schools</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
我尝试过的事情:
- 使用配置文件手动配置字体以自动检测系统字体
- 将字体基目录设置为系统字体目录
- 为 xsl:fo 中的 font-family 值设置不同的字体
我不清楚我错过了什么。我一直在搜索 Apache 网站和谷歌,寻找与此相关的 FOP 1.0 和 FOP 1.1 之间的更改,但无济于事。有人有什么想法吗?
【问题讨论】:
标签: java apache pdf fonts apache-fop