I need to display a large amount of text in a Jasper Report from a JSON array. This is my json array:
{
"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>"
}
]
}}
And this is my Jasper file
<?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>
And the result...
So, as you can see there is a "random" margin at the bottom of each text field. I'm getting crazy trying to remove this margin, but I don't know how to do it. If I preview this on JasperStudio, looks good, but the problem is when I export to PDF.
Of course, green/blue lines are just to debug text margins.
And there is another weird behaviour, when text reach the end of the page, the line breaks at some point, not going to the line end as you can see on this image (top is page 1, bottom page 2):
Thanks!
Thanks to Alex K answer in comments! He gave me the key, and it's about the font. For any reason if I don't define any font for my textfield the space between them are random. So, fixed adding this:
<textElement textAlignment="Justified" markup="html">
<font fontName="Arial" size="7"/>
</textElement>
This only solved my problem using Jaspersoft Studio, but my Spring Boot application deployed into a Docker linux container, throw and exception because didn't find the Arial font on JVM.
net.sf.jasperreports.engine.util.JRFontNotFoundException: Font 'Arial' is not available to the JVM.
I follow this medium post in order to embed Arial font on my Spring Boot proyect: https://medium.com/@seymorethrottle/jasper-reports-adding-custom-fonts-589b55a52e7c Read it carefully, special pay attention to this paragraph:
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.
Again, thanks to Alex K for the key!