FOP doesn't show cyrillic characters
Asked Answered
M

1

7

I need help to display Cyrillic characters with FOP in Java. I created the xml of the content and the xsl file what will format the newly PDF.

Both of them are using UTF-8 character encoding.

But in the result file I see only "#" charactersinstead of Cyrillic characters.

I tried to create the resultxml with XmlSpy. It looks OK.I suppose I'm missing a font. How to add a font to FOP configuration? Please, show me a detailed example if possible.

Here is the Java code:

    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);

Here is the 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>
Mimas answered 2/7, 2014 at 6:17 Comment(0)
W
2

You have to install proper font. # is just missing glyphs for cyrillic unicode letters. You can find some information on FOP font-handling here. There is good tutorial - it's a bit bizagi-specific, but you can get the main idea.

Willner answered 2/7, 2014 at 6:34 Comment(5)
I created the font xml (msmsgothic.xml) as it was mentioned in the tutorial. I also created a config xml, and put the file path of the font there. I also added the configuration to FopFactory. But I still can not generate Cyrillic characters. I'm still receiving '#' instead of them. :-(Mimas
AFAIK MS Gothic is Japanese font - try to choose cyrillic oneWillner
Please, specify one font family about Cyrillic.Mimas
It is not about wrong font-family e.g. MS Gothic. It is supporting Cyrillic. I checked it's Character Map. I'm receiving issue: Font "Tahoma,normal,400" not found. Substituting with "any,normal,400". But I created the tahoma.xml file also for this font-family. Please, please, help!!!Mimas
Looks like this issue https://mcmap.net/q/1628008/-apache-fop-and-arial-fontWillner

© 2022 - 2024 — McMap. All rights reserved.