4

我正在使用 JasperReports 和 DynamicReports 和这段 java 代码来创建包含 utf-8 字符的 pdf 格式的报告,问题是生成的 pdf 文件根本不包含 utf-8 字符,就像它们已被替换为“ ”。使用 OutputStream 创建 utf-8 文件时有什么需要注意的吗?

    public void toPdf(String path){
        OutputStream outHtml;
        try {
            outHtml = new FileOutputStream(path);

            jasperBuilder.toPdf(outHtml);
        } catch (Exception e1) {
            logger.error("failed to create PDF", e1);
        }
}

值得注意的是,创建 XLS 和 HTML 文件不会遇到这样的问题。

请注意,jasperBuilder.toPdf(outHtml);我已经跟踪了很多代码行,并且在这些行中我的 utf-8 字符没有被消除。所以我猜魔鬼在里面outHtml = new FileOutputStream(path);

4

2 回答 2

5

我设法解决了它。这是一个字体和编码问题。只是按照教程here,但更改<pdfEncoding>UTF-8</pdfEncoding><pdfEncoding>Identity-H</pdfEncoding>fonts.xml

<fontFamilies>
  <fontFamily name="FreeUniversal">
    <normal>/home/moien/tahoma.ttf</normal>
    <bold>/home/moien/tahoma.ttf</bold>
    <italic>/home/moien/tahoma.ttf</italic>
    <boldItalic>/home/moien/tahoma.ttf</boldItalic>
    <pdfEncoding>Identity-H</pdfEncoding>
    <pdfEmbedded>true</pdfEmbedded>
  </fontFamily>
</fontFamilies> 

现在我有另一个挑战要解决,使字体 URL 相对!

于 2013-12-15T07:00:52.220 回答
2

AFileOutputStream完全不知道写入它的“东西”。它只写字节。如果字符被消除或损坏,那么这是由生成要写入流的字节的任何原因引起的。

在这种情况下,我的钱将用于您jasperBuilder在运行此代码之前配置/使用对象的方式。

于 2013-12-14T12:10:38.033 回答