问题描述
我正在维护一个使用Apache FOP打印PDF文档的程序。有几个关于中文字符出现####的投诉。我找到了一个关于这个问题的现有线程,并在我身边做了一些研究。
I am maintaining a program which uses the Apache FOP for printing PDF documents. There have been a couple complaints about the Chinese characters coming up as "####". I have found an existing thread out there about this problem and done some research on my side.
我有uming.tff语言我系统上安装的文件。与这个帖子中的人不同,我仍然得到####。
I do have the uming.tff language files installed on my system. Unlike the person in this thread, I still getting the "####".
从这一点开始,有没有人看过一个可以让你打印的工作使用Apache FOP的PDF文档中的复杂字符?
From this point forward, has anyone seen a work around that would allow you to print complex characters in a PDF document using Apache FOP?
Ryan
推荐答案
必须采取三个步骤才能使中文字符正确显示在使用FOP 创建的PDF文件中(对于默认字体中不可用的所有字符也是如此,更常见的是使用非默认字体)。
Three steps must be taken for chinese characters to correctly show in a PDF file created with FOP (this is also true for all characters not available in the default font, and more generally to use a non-default font).
让我们使用这个简单的示例来显示FOP在出现问题时产生的警告:
Let us use this simple fo example to show the warnings produced by FOP when something is wrong:
<?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="one">
<fo:region-body />
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="one">
<fo:flow flow-name="xsl-region-body">
<!-- a block of chinese text -->
<fo:block>博洛尼亚大学中国学生的毕业论文</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
处理此输入时,FOP会发出类似于此警告的几条警告:
Processing this input, FOP gives several warnings similar to this one:
org.apache.fop.events.LoggingEventListener processEvent
WARNING: Glyph "?" (0x535a) not available in font "Helvetica".
...
在FO文件中没有任何明确的字体系列指示,FOP默认值使用Helvetica,它是之一(字体)随处可用,所以不需要嵌入它们。)
Without any explicit font-family indication in the FO file, FOP defaults to using Helvetica, which is one of the Base-14 fonts (fonts that are available everywhere, so there is no need to embed them).
每个字体都支持一组字符,为它们分配一个可见的字形;当字体不支持字符时,会产生上述警告, PDF显示#而不是缺少的字形。
Each font supports a set of characters, assigning a visible glyphs to them; when a font does not support a character, the above warning is produced, and the PDF shows "#" instead of the missing glyph.
如果默认字体不支持我们的文字的字符(或者我们只想使用,我们必须使用 font-family
属性来说明所需的属性。
If the default font doesn't support the characters of our text (or we simply want to use a different font), we must use the font-family
property to state the desired one.
继承了 font-family
的值,因此如果我们想对整个文档使用相同的字体,我们可以在上设置属性:页序
;如果我们只需要一些段落或单词的特殊字体,我们可以在相关的 fo:block font-family
>或 fo:inline
。
The value of font-family
is inherited, so if we want to use the same font for the whole document we can set the property on the fo:page-sequence
; if we need a special font just for some paragraphs or words, we can set font-family
on the relevant fo:block
or fo:inline
.
因此,我们的输入变为(使用我所拥有的字体):
So, our input becomes (using a font I have as example):
<?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="one">
<fo:region-body />
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="one">
<fo:flow flow-name="xsl-region-body">
<!-- a block of chinese text -->
<fo:block font-family="SimSun">博洛尼亚大学中国学生的毕业论文</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
但是现在除了旧警告之外我们还会收到新的警告!
But now we get a new warning, in addition to the old ones!
org.apache.fop.events.LoggingEventListener processEvent
WARNING: Font "SimSun,normal,400" not found. Substituting with "any,normal,400".
org.apache.fop.events.LoggingEventListener processEvent
WARNING: Glyph "?" (0x535a) not available in font "Times-Roman".
...
FOP不知道如何映射SimSun到字体文件,所以它默认为不支持我们的中文字符的通用Base-14字体(Times-Roman), PDF仍然显示#。
FOP doesn't know how to map "SimSun" to a font file, so it defaults to a generic Base-14 font (Times-Roman) which does not support our chinese characters, and the PDF still shows "#".
在FOP的文件夹中,文件 conf / fop.xconf
是一个示例配置;我们可以直接编辑它或制作一个副本来开始。
Inside FOP's folder, the file conf/fop.xconf
is an example configuration; we can directly edit it or make a copy to start from.
配置文件是一个XML文件,我们必须添加 / fonts / (每个可能的输出mime类型都有一个渲染器
部分,因此请检查您是否在右侧插入映射):
The configuration file is an XML file, and we have to add the font mappings inside /fop/renderers/renderer[@mime = 'application/pdf']/fonts/
(there is a renderer
section for each possible output mime type, so check you are inserting your mapping in the right one):
<?xml version="1.0"?>
<fop version="1.0">
...
<renderers>
<renderer mime="application/pdf">
...
<fonts>
<!-- specific font mapping -->
<font kerning="yes" embed-url="/Users/furini/Library/Fonts/SimSun.ttf" embedding-mode="subset">
<font-triplet name="SimSun" style="normal" weight="normal"/>
</font>
<!-- "bulk" font mapping -->
<directory>/Users/furini/Library/Fonts</directory>
</fonts>
...
</renderer>
...
</renderers>
</fop>
- 每个
字体
元素指向字体文件 - 每个
font-triplet
条目标识 + (正常,斜体,......) + ):From java code we can use (see also FOP's site):
fopFactory.setUserConfig(new File("/path/to/our/fop.xconf"));
现在,最后,PDF应该正确使用所需的字体并按预期显示。
Now, at last, the PDF should correctly use the desired fonts and appear as expected.
如果反而FOP突然终止并出现如下错误:
If instead FOP terminates abruptly with an error like this:
org.apache.fop.cli.Main startFOP SEVERE: Exception org.apache.fop.apps.FOPException: Failed to resolve font with embed-url '/Users/furini/Library/Fonts/doesNotExist.ttf'
这意味着FOP找不到字体文件,需要再次检查字体配置;典型的原因是
it means that FOP could not find the font file, and the font configuration needs to be checked again; typical causes are
- 字体中的拼写错误
- 访问字体文件的权限不足
这篇关于Apache FOP使用SunSim显示###的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!