我使用嵌入在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>
我尝试过的事情:
我不清楚我错过了什么。我一直在搜索Apache网站并谷歌搜索FOP 1.0和FOP 1.1之间的相关变化,但无济于事。有人有什么想法吗?
答案 0 :(得分:1)
我从我们公司的资产经理网站找到了FOP 1.0 jar的预编译版本,似乎解决了这个问题。不确定从Apache站点获取源代码的1.0版本出错了。我最好的猜测是预编译版本包含Apache源代码所没有的自包含字体。