iReport 预览/导出器输出不处理 UTF-8 翻译?
我正在尝试使我的报告与翻译包一起使用。无论我尝试什么,我似乎都无法为任何语言呈现 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 中的失败。
这是资源包:
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:
And the output in iReport:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Stefan,我知道这不是很好的解决方案,但它适用于您的示例。
如果您知道源代码页,转换将对您有所帮助。也许您可以在报告中传递代码页并将其用作表达式中的参数或变量。
表达式可以是这样的:
或者这样,如果您可以在属性文件的标头中传递代码页:
更新:
添加 Arial 字体后,我的 irfonts.xml (%IREPORT_DIR%\ireport\fonts) 具有以下内容:
Stefan, I know that it is not very good solution, but it works for your sample.
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:
or like this, if you can pass codepage in header of your properties file:
UPDATE:
After adding Arial font my irfonts.xml (%IREPORT_DIR%\ireport\fonts) has this content:
这是其他开发人员在生成报告时也遇到编码问题的答案...
我在使用法语语言环境生成报告时也遇到了类似的问题。
我的捆绑包和模板文件存储在数据库中,以允许新模板和区域设置,而无需重新编译项目。
但是法语包中包含的每个带有重音符号的字符都无法正确打印。
所以,我:
我认为 jasper 报告默认采用 UTF-8 来生成报告,但我不确定100%。
可以强制使用:
问题再次存在,而不是:
我这样做:
之后,只需将资源包传递给 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 :
I think jasper reports take UTF-8 by default to generate reports but I'm not sure at 100%.
It can be forced with :
The problem persist again so instead of :
I do this :
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...
确保您的资源包没有字节顺序标记。这会破坏 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.