JasperReports:转换为 PDF 时文本字段上的奇怪行为

JasperReports: weird behaviour on textfield when convert to PDF

我需要在来自 JSON 数组的 Jasper Report 中显示大量文本。 这是我的 json 数组:

{
"data": {
    "contractualConditions": [
        {
            "text": "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam feugiat neque nec felis fringilla, sed eleifend odio dictum. Suspendisse arcu ligula, gravida condimentum fringilla ac, tincidunt nec orci. Suspendisse et ullamcorper magna. Aliquam suscipit posuere dolor at sagittis. In sed nisl sit amet lorem elementum tempor a in justo. Nam facilisis tellus ut semper lacinia. Cras eu nibh nisl. Maecenas egestas posuere dignissim.</p><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam feugiat neque nec felis fringilla, sed eleifend odio dictum. Suspendisse arcu ligula, gravida condimentum fringilla ac, tincidunt nec orci. Suspendisse et ullamcorper magna. Aliquam suscipit posuere dolor at sagittis. In sed nisl sit amet lorem elementum tempor a in justo. Nam facilisis tellus ut semper lacinia. Cras eu nibh nisl. Maecenas egestas posuere dignissim.</p><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam feugiat neque nec felis fringilla, sed eleifend odio dictum. Suspendisse arcu ligula, gravida condimentum fringilla ac, tincidunt nec orci. Suspendisse et ullamcorper magna. Aliquam suscipit posuere dolor at sagittis. In sed nisl sit amet lorem elementum tempor a in justo. Nam facilisis tellus ut semper lacinia. Cras eu nibh nisl. Maecenas egestas posuere dignissim.</p>"
        },
        {
            "text": "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam feugiat neque nec felis fringilla, sed eleifend odio dictum. Suspendisse arcu ligula, gravida condimentum fringilla ac, tincidunt nec orci. Suspendisse et ullamcorper magna. Aliquam suscipit posuere dolor at sagittis. In sed nisl sit amet lorem elementum tempor a in justo. Nam facilisis tellus ut semper lacinia. Cras eu nibh nisl. Maecenas egestas posuere dignissim.</p>"
        },
        {
            "text": "<p>Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Morbi nibh diam, tincidunt nec condimentum quis, pulvinar nec sem. Curabitur nec vehicula purus. Pellentesque ac metus in sem aliquet pretium vitae non tellus. Aenean quis mi euismod orci euismod facilisis. Aliquam quis velit nisi. Ut hendrerit malesuada ligula id venenatis. Sed sagittis dolor mauris, nec elementum dui ornare nec. Fusce vel elementum dui. Donec tempor scelerisque sagittis. Nulla facilisi. Nam ac sagittis justo. Maecenas felis sapien, mollis quis feugiat at, mollis at justo. Proin leo dui, consequat eu pretium quis, eleifend sed sapien. Integer tellus eros, interdum ut molestie id, elementum et justo.</p>"
        }
    ]
}}

这是我的 Jasper 文件

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.11.0.final using JasperReports Library version 6.11.0-0c4056ccaa4d25a5a8c45672d2f764ea3498bebb  -->
<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="ClauseSubReport" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="c75e022d-0c3e-47e1-b8fb-bbe084875130">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="sectionFive.xml"/>
    <queryString language="json">
        <![CDATA[data.contractualConditions]]>
    </queryString>
    <field name="text" class="java.lang.String">
        <property name="net.sf.jasperreports.jsonql.field.expression" value="text"/>
    </field>
    <detail>
        <band height="10">
            <property name="com.jaspersoft.studio.layout"/>
            <frame borderSplitType="DrawBorders">
                <reportElement stretchType="ElementGroupHeight" x="0" y="0" width="555" height="10" uuid="0048bcd5-74f4-44e8-8aae-cb4fb6b9a5ec"/>
                <box>
                    <pen lineColor="#98FC26"/>
                    <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#98FC26"/>
                    <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#98FC26"/>
                    <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#98FC26"/>
                    <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#98FC26"/>
                </box>
                <textField textAdjust="StretchHeight">
                    <reportElement x="60" y="1" width="490" height="9" forecolor="#1F497D" uuid="6448707a-6f36-4bef-96a1-6bc7634e72c3">
                        <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="34deaa54-ac6e-42dd-8c82-c125d6810c22"/>
                    </reportElement>
                    <box>
                        <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#1F497D"/>
                        <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#1F497D"/>
                        <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#1F497D"/>
                        <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#1F497D"/>
                    </box>
                    <textElement textAlignment="Justified" markup="html">
                        <font size="7"/>
                    </textElement>
                    <textFieldExpression><![CDATA[$F{text}]]></textFieldExpression>
                </textField>
            </frame>
        </band>
    </detail>
</jasperReport>

结果...

因此,如您所见,每个文本字段的底部都有一个 "random" 边距。试图删除这个边距我变得很疯狂,但我不知道该怎么做。如果我在 JasperStudio 上预览它,看起来不错,但问题是当我导出为 PDF 时。

当然,green/blue行只是为了调试文本边距。

还有另一种奇怪的行为,当文本到达页面末尾时,换行会在某个点中断,而不是像您在这张图片上看到的那样到达行尾(顶部是第 1 页,底部是第 2 页):

谢谢!

感谢Alex K评论中的回答! 他给了我钥匙,是关于字体的。出于任何原因,如果我没有为我的文本字段定义任何字体,它们之间的 space 是随机的。 所以,固定添加这个:

<textElement textAlignment="Justified" markup="html">
    <font fontName="Arial" size="7"/>
</textElement>

这只解决了我使用 Jaspersoft Studio 的问题,但是我的 Spring 启动应用程序部署到 Docker linux 容器中,抛出异常,因为没有找到 Arial 字体JVM.

net.sf.jasperreports.engine.util.JRFontNotFoundException: Font 'Arial' is not available to the JVM.

我遵循这个媒介 post 以便在我的 Spring 启动项目中嵌入 Arial 字体:https://medium.com/@seymorethrottle/jasper-reports-adding-custom-fonts-589b55a52e7c 仔细阅读,特别注意这一段:

Note that the CDATA for the font (ttf) and pdf elements is a path to the font file relative to the root of the jar. How did that path get there? We entered it into the PDF Font Name field before we exported the jar. Intuitive right? So intuitive that I lost an hour of work, and used up what felt like half a gigabyte of RAM on browser tabs trying to figure out why the exported jar file was throwing errors in Java.

再次感谢 Alex K 的钥匙!