iReport 预览/导出器输出不处理 UTF-8 翻译?

发布于 2024-12-04 13:37:50 字数 1108 浏览 0 评论 0原文

我正在尝试使我的报告与翻译包一起使用。无论我尝试什么,我似乎都无法为任何语言呈现 UTF-8 - 我只是为每个 UTF-8 字符得到两个损坏的字符。

我的报告以此开头: ...但是iReport预览不起作用,

而我的实际代码是这样的:

JRProperties.setProperty("net.sf.jasperreports.default.pdf.encoding", "UTF-8");
exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8")

两者似乎都无法输出UTF-8。这里可能发生了什么?我正在拔头发。即使将字体设置为 Arial,我仍然遇到问题。我的网络应用程序使用与 Grails 相同的字体和类似的捆绑包,并且我没有遇到任何问题,因此 jasperreports 发生了特定的情况。

编辑: 我认为字节顺序标记可能是问题所在,但事实并非如此。我创建了一个基本报告,显示 4.0.2 中的失败。

这是资源包:

Greek properties file

iReport 中的输出:

在此处输入图像描述

这是 报告,以及 properties 文件用于观察这些结果。

其他信息: 当属性文件以 ANSI 编码保存时,我可以获得所有德语字符,包括 ä 和 ß。当属性文件保存为 UTF-8 时,仅 ASCII 字符有效。

I'm trying to make my reports work with a translations bundle. No matter what I try, I can't seem to render UTF-8 for any language - I just get two broken characters for every UTF-8 character.

My report starts with this: <?xml version="1.0" encoding="UTF-8"?>
...but iReport preview doesn't work,

and my actual code has this:

JRProperties.setProperty("net.sf.jasperreports.default.pdf.encoding", "UTF-8");
exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8")

Neither seems to be able to output UTF-8. What could possibly be going on here? I'm pulling my hair out. Even with the font set to Arial, I'm having issues. My web-app is using the same font and similar bundles with Grails, and I have no issues there, so something is happening specific to jasperreports.

EDIT:
I thought byte order markers may have been the issue, but it isn't. I've created a basic report that shows the failure in 4.0.2.

Here's the resource bundle:

Greek properties file

And the output in iReport:

enter image description here

And here's the report, and the properties file used to observe these results.

Nore information:
When the properties files are saved in ANSI encoding, I can get all german characters, including ä and ß. When the properties files are saved as UTF-8, only ASCII characters work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

⊕婉儿 2024-12-11 13:37:50

Stefan,我知道这不是很好的解决方案,但它适用于您的示例。

        <textField>
            <reportElement x="49" y="0" width="359" height="38"/>
            <textElement>
                <font fontName="Arial" pdfEncoding="Identity-H" isPdfEmbedded="true"/>
            </textElement>
            <textFieldExpression><![CDATA[new String($R{title}.getBytes("ISO-8859-1"), "UTF-8")]]></textFieldExpression>
        </textField>

如果您知道源代码页,转换将对您有所帮助。也许您可以在报告中传递代码页并将其用作表达式中的参数或变量。

表达式可以是这样的:

   <textFieldExpression><![CDATA[new String($R{title}.getBytes($P{codePage}), "UTF-8")]]></textFieldExpression>

或者这样,如果您可以在属性文件的标头中传递代码页:

   <textFieldExpression><![CDATA[new String($R{title}.getBytes($R{codePage}), "UTF-8")]]></textFieldExpression>

更新:
添加 Arial 字体后,我的 irfonts.xml (%IREPORT_DIR%\ireport\fonts) 具有以下内容:

<?xml version="1.0" encoding="UTF-8"?>

<fontFamilies>
   <fontFamily name="Arial">
       <normal><![CDATA[arial.ttf]]></normal>
       <bold><![CDATA[arialbd.ttf]]></bold>
       <italic><![CDATA[ariali.ttf]]></italic>
       <boldItalic><![CDATA[arialbi.ttf]]></boldItalic>
       <pdfEncoding><![CDATA[Identity-H]]></pdfEncoding>
       <pdfEmbedded><![CDATA[false]]></pdfEmbedded>
   </fontFamily>

</fontFamilies>

Stefan, I know that it is not very good solution, but it works for your sample.

        <textField>
            <reportElement x="49" y="0" width="359" height="38"/>
            <textElement>
                <font fontName="Arial" pdfEncoding="Identity-H" isPdfEmbedded="true"/>
            </textElement>
            <textFieldExpression><![CDATA[new String($R{title}.getBytes("ISO-8859-1"), "UTF-8")]]></textFieldExpression>
        </textField>

In case you know source codepage the convertation will help you. May be you can pass codepage in report and use it as parameter or variable in expression.

The expression can be like this:

   <textFieldExpression><![CDATA[new String($R{title}.getBytes($P{codePage}), "UTF-8")]]></textFieldExpression>

or like this, if you can pass codepage in header of your properties file:

   <textFieldExpression><![CDATA[new String($R{title}.getBytes($R{codePage}), "UTF-8")]]></textFieldExpression>

UPDATE:
After adding Arial font my irfonts.xml (%IREPORT_DIR%\ireport\fonts) has this content:

<?xml version="1.0" encoding="UTF-8"?>

<fontFamilies>
   <fontFamily name="Arial">
       <normal><![CDATA[arial.ttf]]></normal>
       <bold><![CDATA[arialbd.ttf]]></bold>
       <italic><![CDATA[ariali.ttf]]></italic>
       <boldItalic><![CDATA[arialbi.ttf]]></boldItalic>
       <pdfEncoding><![CDATA[Identity-H]]></pdfEncoding>
       <pdfEmbedded><![CDATA[false]]></pdfEmbedded>
   </fontFamily>

</fontFamilies>
源来凯始玺欢你 2024-12-11 13:37:50

这是其他开发人员在生成报告时也遇到编码问题的答案...

我在使用法语语言环境生成报告时也遇到了类似的问题。
我的捆绑包和模板文件存储在数据库中,以允许新模板和区域设置,而无需重新编译项目。
但是法语包中包含的每个带有重音符号的字符都无法正确打印。
所以,我:

  • 检查捆绑包字符编码(设置为没有 BOM 的 UTF-8)
  • 检查 .jrxml 报告文件的 XML 标头()

我认为 jasper 报告默认采用 UTF-8 来生成报告,但我不确定100%。
可以强制使用:

JRExporter exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");

问题再次存在,而不是:

ResourceBundle bundle = new PropertyResourceBundle(new ByteArrayInputStream(myBundle)); // myBundle is a byte array retrieve from a DB

我这样做:

InputStreamReader reader = new InputStreamReader(new ByteArrayInputStream(myBundle), Charset.forName("UTF-8"));
ResourceBundle bundle = new PropertyResourceBundle(reader);

之后,只需将资源包传递给 JasperFillManager,现在一切顺利。
也许它很简单,但它可以节省我 2 或 3 个小时的研究时间......

This is an answer for other developer which also have encoding problem when generating reports...

I had a similar problem when generating reports with, for example, French locale.
My bundles and templates files are stored in a database to allow new templates and locale without recompiling the project.
BUT every characters with accents contains in the French bundle aren't print correctly.
So, I :

  • Check the bundle char encoding (set to UTF-8 without BOM)
  • Check the XML header of the .jrxml report file ()

I think jasper reports take UTF-8 by default to generate reports but I'm not sure at 100%.
It can be forced with :

JRExporter exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");

The problem persist again so instead of :

ResourceBundle bundle = new PropertyResourceBundle(new ByteArrayInputStream(myBundle)); // myBundle is a byte array retrieve from a DB

I do this :

InputStreamReader reader = new InputStreamReader(new ByteArrayInputStream(myBundle), Charset.forName("UTF-8"));
ResourceBundle bundle = new PropertyResourceBundle(reader);

After that, just passing the resource bundle to the JasperFillManager and every thing goes well now.
May be it's so simple but it would have saved me 2 or 3 hours of research...

冰葑 2024-12-11 13:37:50

确保您的资源包没有字节顺序标记。这会破坏 jasperreports,并且它不会显示在 IDE 或文本编辑器中。

Make sure your resource bundles don't have a byte order marker. This will break jasperreports, and it won't show up in IDEs or text editors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文