Home >Java >javaTutorial >How to Display Chinese Characters with SimSun in Apache FOP Generated PDFs?

How to Display Chinese Characters with SimSun in Apache FOP Generated PDFs?

Barbara Streisand
Barbara StreisandOriginal
2024-11-03 12:59:03524browse

How to Display Chinese Characters with SimSun in Apache FOP Generated PDFs?

Apache FOP Displaying Chinese Characters with SimSun

Many users have encountered the issue of Chinese characters displaying as "####" in PDF documents generated using Apache FOP. Resolving this issue involves a three-step process.

Firstly, it is necessary to specify the desired font-family in the FO file. By default, FOP uses the Helvetica font, which may not support Chinese characters. To specify a different font, such as SimSun, include the following line in the FO file:

<code class="xml"><fo:block font-family="SimSun">...</fo:block></code>

However, this alone may not be sufficient to resolve the issue. FOP also requires a configuration to map the specified font-family to the actual font file. This configuration is specified in FOP's configuration file, typically located at conf/fop.xconf. Add the following lines to the configuration file:

<code class="xml"><directory>/path/to/fonts</directory></code>

This informs FOP to automatically configure all font files within the specified directory. Alternatively, you can specify the font mapping explicitly for each font-family and style combination:

<code class="xml"><font kerning="yes" embed-url="/path/to/SimSun.ttf">
  <font-triplet name="SimSun" style="normal" weight="normal"/>
</font></code>

Finally, specify the configuration file when invoking FOP. From the command line, use the -c option:

<code class="bash">fop -c /path/to/fop.xconf input.fo input.pdf</code>

Or, if using FOP from Java code:

<code class="java">fopFactory.setUserConfig(new File("/path/to/fop.xconf"));</code>

By following these steps, FOP will have the necessary information to correctly display Chinese characters with the SimSun font in generated PDF documents.

The above is the detailed content of How to Display Chinese Characters with SimSun in Apache FOP Generated PDFs?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn