【问题标题】:FOP doesn't show cyrillic charactersFOP 不显示西里尔字符
【发布时间】:2014-08-22 19:58:06
【问题描述】:

我需要帮助才能在 Java 中使用 FOP 显示西里尔字符。 我创建了内容的 xml 和 xsl 文件,它将格式化新的 PDF。

它们都使用 UTF-8 字符编码。

但在结果文件中我只看到“#”字符而不是西里尔字符。

我尝试使用 XmlSpy 创建 resultxml。看起来不错。我想我缺少一种字体。如何将字体添加到 FOP 配置?如果可能的话,请给我一个详细的例子。

这是Java代码:

    Document xslDoc = getConverterXsl(index);
    FopFactory fopFactory = null;
    fopFactory = FopFactory.newInstance();
    FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
    Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, pdfOutputStream);
    Result res = new SAXResult(fop.getDefaultHandler());
    doXslTransform(index, xslDoc, fopDoc, res);

这里是 xsl:

<?xml version="1.0" encoding="UTF-16"?>
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" exclude-result-prefixes="fo">
  <xsl:output method="xml" version="1.0" omit-xml-declaration="no" indent="yes"/>
  <xsl:param name="versionParam" select="'1.0'"/> 
  <!-- ========================= -->
  <!-- root element: ReportRoot -->
  <!-- ========================= -->
  <xsl:template match="ReportRoot">
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
      <fo:layout-master-set>
        <fo:simple-page-master master-name="simpleA4" page-width="21cm" page-height="29.7cm" margin-top="2cm" margin-bottom="2cm" margin-left="2cm" margin-right="2cm" font-family="Arial">
          <fo:region-body/>
        </fo:simple-page-master>
      </fo:layout-master-set>
      <fo:page-sequence master-reference="simpleA4">
        <fo:flow flow-name="xsl-region-body">
          <fo:block font-size="16pt" font-weight="bold" space-after="3mm"><xsl:value-of select="projectname"/>
          </fo:block>
          <fo:block font-size="10pt">
            <fo:table table-layout="fixed" width="100%" border-collapse="separate">
              <fo:table-column column-width="4cm"/>
              <fo:table-column column-width="4cm"/>
              <fo:table-column column-width="5cm"/>
              <fo:table-body>
                <xsl:apply-templates select="ReportContent"/>
              </fo:table-body>
            </fo:table>
          </fo:block>
        </fo:flow>
      </fo:page-sequence>
    </fo:root>
  </xsl:template>
  <!-- ========================= -->
  <!-- child element: PlatInfo     -->
  <!-- ========================= -->
  <xsl:template match="ReportContent">
    <fo:table-row>
      <fo:table-cell>
        <fo:block>
          <xsl:attribute name="font-weight">bold</xsl:attribute>
          <xsl:value-of select="PlatNumber"/>
        </fo:block>
        <fo:block>
          <xsl:value-of select="PlatValue"/>
        </fo:block>
      </fo:table-cell>
      <fo:table-cell>
        <fo:block>
          <xsl:attribute name="font-weight">bold</xsl:attribute>
          <xsl:value-of select="Country"/>
        </fo:block>
        <fo:block>
          <xsl:value-of select="CountryValue"/>
        </fo:block>
      </fo:table-cell>
    </fo:table-row>
    <fo:table-row>
      <fo:table-cell>
        <fo:block>
            <fo:leader />
        </fo:block>
      </fo:table-cell>
    </fo:table-row>
    <fo:table-row>
      <fo:table-cell>
        <fo:block>
            <fo:leader />
        </fo:block>
      </fo:table-cell>
    </fo:table-row>
    <fo:table-row>
      <fo:table-cell>
        <fo:block>
          <xsl:attribute name="font-weight">bold</xsl:attribute>
          <xsl:value-of select="Image"/>
        </fo:block>
      </fo:table-cell>
    </fo:table-row>
    <fo:table-row>
      <fo:table-cell>
        <fo:block>
          <fo:external-graphic height="12cm" width="24cm" content-height="scale-down-to-fit" content-width="scale-down-to-fit">
            <xsl:attribute name="src"><xsl:value-of select="ImageSrc"/></xsl:attribute>
          </fo:external-graphic>
        </fo:block>
        <fo:block>
        </fo:block>
      </fo:table-cell>
    </fo:table-row>
    <fo:table-row>
      <fo:table-cell>
        <fo:block break-before="page">
        </fo:block>
      </fo:table-cell>
    </fo:table-row>
    <fo:table-row>
      <fo:table-cell>
        <fo:block font-size="14pt" space-after="1mm" font-family="MS Mincho">
          <xsl:value-of select="//StaticText/Text1"/>
          <xsl:value-of select="//ShortEditFields/EditField[1]/Value" />
          <xsl:value-of select="//StaticText/Text2" />
          <xsl:value-of select="//ShortEditFields/EditField[2]/Value" />
          <xsl:value-of select="//StaticText/Text3" />
          <xsl:value-of select="//PlatValue"/>
          <xsl:value-of select="//StaticText/Text4" />
          <xsl:value-of select="//DateValue"/>
          <xsl:value-of select="//StaticText/Text5" />
          <xsl:value-of select="//TimeHour"/>
          <xsl:value-of select="//StaticText/Text6" />
          <xsl:value-of select="//TimeMinute"/>
          <xsl:value-of select="//StaticText/Text7" />
          <xsl:value-of select="//StaticText/Text8" />
        </fo:block>
        <fo:block>
        </fo:block>
      </fo:table-cell>
    </fo:table-row>
  </xsl:template>
</xsl:stylesheet>

【问题讨论】:

    标签: pdf utf-8 fonts apache-fop cyrillic


    【解决方案1】:

    您必须安装正确的字体。 # 只是缺少西里尔 unicode 字母的字形。您可以找到有关 FOP 字体处理here 的一些信息。有很好的 tutorial - 它有点特定于 bizagi,但你可以理解主要思想。

    【讨论】:

    • 我创建了教程中提到的字体 xml (msmsgothic.xml)。我还创建了一个配置 xml,并将字体的文件路径放在那里。我还将配置添加到 FopFactory。但我仍然无法生成西里尔字符。我仍然收到'#'而不是他们。 :-(
    • AFAIK MS Gothic 是日文字体 - 尝试选择西里尔字母
    • 请指定一个关于 Cyrillic 的字体系列。
    • 这与错误的字体系列无关,例如女士哥特式。它支持西里尔字母。我检查了它的字符图。我收到问题:找不到字体“Tahoma,normal,400”。替换为“any,normal,400”。但我也为这个字体系列创建了 tahoma.xml 文件。求求求救!!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-27
    • 1970-01-01
    • 2015-09-02
    相关资源
    最近更新 更多