Java Jasper 报告导出为 pdf 的字体大小问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23934218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 09:12:03  来源:igfitidea点击:

Jasper reports font size issue exporting to pdf

javafontsjasper-reportsexport-to-pdf

提问by Paco Abato

I'm using Jaspersoft Studio 5.6.

我正在使用 Jaspersoft Studio 5.6。

It displays correctly in Jaspersoft Studio in Design view. In Preview view it is also correctly displayed (using java or pdf).
When I execute the report from my Java application the three texts are displayed at the same size.

它在 Jaspersoft Studio 的设计视图中正确显示。在预览视图中,它也正确显示(使用 java 或 pdf)。
当我从我的 Java 应用程序执行报告时,三个文本以相同的大小显示。

I noticed that Bold and Italic works fine but if I change the font (DejaVu, Arial, Times new roman, etc) that is also ignored. In the report generated by my application I always get the same font.

我注意到粗体和斜体工作正常,但如果我更改字体(DejaVu、Arial、Times new roman 等),也会被忽略。在我的应用程序生成的报告中,我总是得到相同的字体。

What is failing? Am I forgetting any configuration option?

什么是失败?我忘记了任何配置选项吗?

The example mixes static fields and text fields just to test.

该示例混合静态字段和文本字段只是为了测试。

EDIT:

编辑:

It seems like I found a "solution" (wich has side effects).

似乎我找到了一个“解决方案”(有副作用)。

I was generating the report like this:

我正在生成这样的报告:

JasperRunManager.runReportToPdfStream(stream, outStream, params, datasource);

Now, generating in this way the size of the texts are correct in the generated pdf:

现在,以这种方式生成的文本大小在生成的 pdf 中是正确的:

JasperReport jasperReport = JasperCompileManager.compileReport("my/report.jrxml");
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, datasource);
JasperExportManager.exportReportToPdfStream(jasperPrint, outStream);

The font type however is not correct.

但是字体类型不正确。

The side effect, I think, is that JasperRunManager.runReportToPdfStream does not load the report into memory while JasperFillManager.fillReportdoes.

我认为,副作用是 JasperRunManager.runReportToPdfStream 不会将报告加载到内存JasperFillManager.fillReport中。

Any idea about why JasperRunManager.runReportToPdfStreamignores the font size?

关于为什么JasperRunManager.runReportToPdfStream忽略字体大小的任何想法?

Any idea about why the other way is ignoring the font type? Both JSS and my java application are on the same Windows machine (so the fonts must be available to both).

知道为什么另一种方式忽略字体类型吗?JSS 和我的 java 应用程序都在同一台 Windows 机器上(所以字体必须对两者都可用)。

Any advise to avoid loading into memory the whole report?

有什么建议可以避免将整个报告加载到内存中吗?

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="subinforme_pag_1" pageWidth="802" pageHeight="552" orientation="Landscape" columnWidth="802" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="c6f5d08f-f28b-4c77-8523-5cf4746cdcf2">
    <queryString language="SQL">
        <![CDATA[]]>
    </queryString>
    <detail>
        <band height="551" splitType="Stretch">
            <textField>
                <reportElement x="0" y="80" width="180" height="40" uuid="50721cba-c082-47de-9abf-effcf1b784dd">
                    <property name="local_mesure_unitheight" value="pixel"/>
                </reportElement>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="SansSerif" size="4"/>
                </textElement>
                <textFieldExpression><![CDATA["Text Field (size 4)"]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="0" y="0" width="180" height="40" uuid="05c5129f-f29e-47a1-ad82-4547b51e3d56"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="SansSerif" size="16"/>
                </textElement>
                <textFieldExpression><![CDATA["Text Field (Size 16)"]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement x="0" y="40" width="180" height="40" uuid="5c69d29b-b168-408d-89aa-c6d527f0cae1">
                    <property name="local_mesure_unitwidth" value="pixel"/>
                    <printWhenExpression><![CDATA[$V{REPORT_COUNT} == 1]]></printWhenExpression>
                </reportElement>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="SansSerif" size="8" isBold="false"/>
                </textElement>
                <text><![CDATA[Static Text (Size 8)]]></text>
            </staticText>
        </band>
    </detail>
</jasperReport>

回答by Pablo

The problem is when compiled with version 5.6.0.

问题是用 5.6.0 版编译时。

This is solved by compiling with the version 5.5.1

这是通过用5.5.1版本编译解决的

I hope they fix the issue!

我希望他们解决这个问题!

回答by Bigmwaj

I've also had this problem. What you have to do is to upgrade de jasperreport jar in your java web application. For example, if you use iReport 5.6.0 for designing your report, upgrade your jasperreport.jar to 6.2.1

我也遇到过这个问题。您需要做的是在您的 Java Web 应用程序中升级 de jasperreport jar。例如,如果您使用 iReport 5.6.0 来设计您的报告,请将您的 jasperreport.jar 升级到 6.2.1