我正在尝试使用Jasper创建一个富文本模板。结果应该是包含许多段落,缩进列表,一些具有子列表和标题的文档。整个文档将分布在几个页面上,并且由于文档的许多部分是根据数据生成的,因此不会修复分页符。
我的第一个问题是,我的用例实际上是应该用jasper做的事情还是我应该用markdown甚至LateX设置一些东西?所有的模板和示例我能够找到更多的一个带有表格和图形但没有多个页面文档的寻呼机,但是Jasper创建时间很快,并且还能够创建word文件(除了pdf之外)这是一个很大的优点。
如果我正在使用Jasper用于正确的目的,那么我应该为每个段落使用新的文本字段还是应该在一个文本字段中编写整个文档?我不喜欢将所有内容写入一个的想法,因为Jasper不允许将代码格式化得很好,而且我也遇到了新页面的问题。
或者我应该为每个段落创建一个新的文本字段?这是我在下面尝试的但是我在布局上遇到了困难,段落相互叠加,一些部分在每个页面上重复,而有些部分从未出现过。我的整个文档搞砸了,我无法理解为什么,更改参数确实会有所改变,但我无法理解发生了什么
因此,如果我应为每个段落创建一个新的文本字段,如何设置布局以使它们相互显示。它们将被生成,因此长度不固定,我希望段落均匀分布,它们之间没有不同的空格。
要显示我遇到的问题,请参阅以下jrxml
文件,该文件编译为具有混乱布局的文档。我知道我可以将所有文本字段设置为浮动并将它们设置为确切的位置,但这不是主意,因为我将生成字段,所以我寻找的是某种flowlayout。
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.6.0.final using JasperReports Library version 6.6.0 -->
<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="Test" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="daade75b-7134-4af7-b206-2451426de5a9">
<property name="com.jaspersoft.studio.unit." value="pixel"/>
<property name="com.jaspersoft.studio.unit.pageHeight" value="pixel"/>
<property name="com.jaspersoft.studio.unit.pageWidth" value="pixel"/>
<property name="com.jaspersoft.studio.unit.topMargin" value="pixel"/>
<property name="com.jaspersoft.studio.unit.rightMargin" value="pixel"/>
<property name="com.jaspersoft.studio.unit.columnWidth" value="pixel"/>
<property name="com.jaspersoft.studio.unit.columnSpacing" value="pixel"/>
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<queryString>
<![CDATA[]]>
</queryString>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="72" splitType="Stretch">
<property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
<textField>
<reportElement x="0" y="0" width="555" height="36" uuid="a253abbc-8696-4ab4-8d58-bc33a059982c"/>
<textElement textAlignment="Center">
<font fontName="Times New Roman" size="12" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA["Hello"]]></textFieldExpression>
</textField>
<staticText>
<reportElement x="0" y="36" width="555" height="36" uuid="5494bb7d-0f10-479c-aad7-61ba1a10868c"/>
<textElement textAlignment="Center" markup="html">
<font fontName="Times New Roman" size="12" isBold="true"/>
</textElement>
<text><![CDATA[Consulting agreement]]></text>
</staticText>
</band>
</title>
<pageHeader>
<band height="19" splitType="Stretch"/>
</pageHeader>
<detail>
<band height="367" splitType="Stretch">
<property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
<textField isStretchWithOverflow="true">
<reportElement isPrintRepeatedValues="false" x="0" y="0" width="555" height="55" isPrintWhenDetailOverflows="true" uuid="892c2a2f-d320-4dc2-b31a-c15bccb6af43"/>
<textElement>
<font fontName="Times New Roman" size="12"/>
<paragraph firstLineIndent="20"/>
</textElement>
<textFieldExpression><![CDATA["Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi tincidunt rhoncus vestibulum. In vitae neque lectus. Donec nec elementum sem. Donec ullamcorper leo in felis mattis, sit amet fringilla sapien semper. In consectetur neque porttitor semper vulputate. Nunc et tellus purus. Nam et urna nec urna fringilla molestie. Duis eleifend rutrum porttitor."]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement positionType="Float" isPrintRepeatedValues="false" x="0" y="55" width="555" height="52" isPrintWhenDetailOverflows="true" uuid="ba36dcb6-d88f-43ea-bfc1-16dfcb1d5837"/>
<textElement>
<font fontName="Times New Roman" size="12"/>
<paragraph firstLineIndent="20"/>
</textElement>
<textFieldExpression><![CDATA["Phasellus euismod, sapien commodo iaculis lacinia, urna odio pretium quam, in scelerisque dolor nisi at purus. Proin at condimentum lectus. Phasellus vitae felis augue. Praesent vulputate ipsum nibh, in cursus metus vehicula quis. Vestibulum facilisis elit tristique, posuere sem vitae, rutrum dui. Sed varius eros nec euismod bibendum. Sed dapibus condimentum lectus, semper dignissim felis rhoncus sit amet. In viverra varius tincidunt. Aenean eu tincidunt nulla, eu sagittis ante. Duis ullamcorper eros ut tincidunt mattis. Quisque ac dolor imperdiet, volutpat lorem ut, luctus turpis."]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement positionType="Float" isPrintRepeatedValues="false" x="0" y="107" width="555" height="52" isPrintWhenDetailOverflows="true" uuid="71df41c1-9607-40b7-8dd8-897882470e88"/>
<textElement markup="html">
<font fontName="Times New Roman" size="12"/>
<paragraph lineSpacing="Single" firstLineIndent="20"/>
</textElement>
<textFieldExpression><![CDATA["Donec eget ligula vitae tellus tincidunt ornare at nec lacus. Aliquam interdum lobortis hendrerit. Suspendisse tempus ipsum urna, vel convallis est luctus et. Duis fermentum ipsum mi, sed lobortis arcu fermentum in. Mauris sagittis nisl arcu, at fermentum justo tincidunt quis. Curabitur tristique nunc et nulla ornare, eget bibendum nisl efficitur. Quisque porta sem sed quam porttitor tristique eget in ex. Maecenas scelerisque felis eu est vulputate condimentum. Donec efficitur lobortis tristique. Praesent nulla augue, mollis non elementum sit amet, luctus in arcu. Etiam viverra orci sit amet ante mattis aliquet."]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement positionType="Float" isPrintRepeatedValues="false" x="0" y="159" width="555" height="52" isPrintWhenDetailOverflows="true" uuid="71df41c1-9607-40b7-8dd8-897882470e88"/>
<textElement markup="html">
<font fontName="Times New Roman" size="12"/>
<paragraph lineSpacing="Single" firstLineIndent="0" leftIndent="30"/>
</textElement>
<textFieldExpression><![CDATA["Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur pretium malesuada dapibus. In hac habitasse platea dictumst. Curabitur suscipit metus vitae tellus laoreet semper. Fusce vehicula a libero sed venenatis. Vestibulum accumsan egestas nisl. Sed at enim vel mauris ornare auctor vel vitae nisl. Morbi sed lobortis arcu. Duis blandit aliquam auctor."]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement positionType="Float" isPrintRepeatedValues="false" x="0" y="211" width="555" height="52" isPrintWhenDetailOverflows="true" uuid="7ab5d201-6c88-4932-bc84-941900d1dc39"/>
<textElement markup="html">
<font fontName="Times New Roman" size="12"/>
<paragraph lineSpacing="Single" firstLineIndent="0" leftIndent="40"/>
</textElement>
<textFieldExpression><![CDATA["Phasellus a nulla ornare, dapibus nunc in, scelerisque urna. Integer vestibulum neque at tellus cursus congue. Aliquam tempor mauris porta, egestas ante vitae, sollicitudin enim. Vivamus cursus convallis mauris non varius. Integer euismod eros cursus justo pharetra, nec fringilla ante tempor. Lorem ipsum dolor sit amet, consectetur adipiscing elit. In facilisis at augue ac tempor."]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement isPrintRepeatedValues="false" x="0" y="263" width="555" height="52" isPrintWhenDetailOverflows="true" uuid="3b3879c4-fd99-4e49-b8b2-7f15f09d1667"/>
<textElement>
<font fontName="Times New Roman" size="12"/>
<paragraph firstLineIndent="20"/>
</textElement>
<textFieldExpression><![CDATA["Norse mythology is the body of myths of the North Germanic people stemming from Norse paganism and continuing after the Christianization of Scandinavia and into the Scandinavian folklore of the modern period. The northernmost extension of Germanic mythology, Norse mythology consists of tales of various deities, beings, and heroes derived from numerous sources from both before and after the pagan period, including medieval manuscripts, archaeological representations, and folk tradition."]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement positionType="Float" isPrintRepeatedValues="false" x="0" y="315" width="555" height="52" isPrintWhenDetailOverflows="true" uuid="c922c7c9-93d3-4cc3-b4df-8ab7f6f80090"/>
<textElement>
<font fontName="Times New Roman" size="12"/>
<paragraph firstLineIndent="20"/>
</textElement>
<textFieldExpression><![CDATA["Most of the surviving mythology centres on the plights of the gods and their interaction with various other beings, such as humanity and the jötnar, beings who may be friends, lovers, foes or family members of the gods. The cosmos in Norse mythology consists of Nine Worlds that flank a central cosmological tree, Yggdrasil. Units of time and elements of the cosmology are personified as deities or beings. Various forms of a creation myth are recounted, where the world is created from the flesh of the primordial being Ymir, and the first two humans are Ask and Embla. These worlds are foretold to be reborn after the events of Ragnarök when an immense battle occurs between the gods and their enemies, and the world is enveloped in flames, only to be reborn anew. There the surviving gods will meet, and the land will be fertile and green, and two humans will repopulate the world."]]></textFieldExpression>
</textField>
</band>
</detail>
<pageFooter>
<band height="367" splitType="Stretch">
<textField isStretchWithOverflow="true">
<reportElement positionType="Float" isPrintRepeatedValues="false" x="0" y="315" width="555" height="52" isPrintWhenDetailOverflows="true" uuid="4e12811e-3a76-4e19-86d1-08e2a1239c23"/>
<textElement>
<font fontName="Times New Roman" size="12"/>
<paragraph firstLineIndent="20"/>
</textElement>
<textFieldExpression><![CDATA["Numerous gods are mentioned in the source texts such as the hammer-wielding, humanity-protecting thunder-god Thor, who relentlessly fights his foes; the one-eyed, raven-flanked god Odin, who craftily pursues knowledge throughout the worlds and bestowed among humanity the runic alphabet; the beautiful, seiðr-working, feathered cloak-clad goddess Freyja who rides to battle to choose among the slain; the vengeful, skiing goddess Skaði, who prefers the wolf howls of the winter mountains to the seashore; the powerful god Njörðr, who may calm both sea and fire and grant wealth and land; the god Freyr, whose weather and farming associations bring peace and pleasure to humanity; the goddess Iðunn, who keeps apples that grant eternal youthfulness; the mysterious god Heimdallr, who is born of nine mothers, can hear grass grow, has gold teeth, and possesses a resounding horn; the jötunn Loki, who brings tragedy to the gods by engineering the death of the goddess Frigg's beautiful son Baldr; and numerous other deities."]]></textFieldExpression>
</textField>
</band>
</pageFooter>
</jasperReport>
我宁愿使用更适合您的用例的Apache FOP而不是JasperReports,它们更多地用于生成报告,统计信息和其他表格和网格结构化文档。
例如,我们使用Apache FOP生成包含大量段落,地址,个人数据,页码,徽标等的合同文档。
Apache FOP使用XSL-FO作为模板,使用XML作为数据源。这意味着您创建XSL-FO模板(手动或使用任何FO编辑器),然后您创建或以编程方式生成包含所有段落,列表,标题和其他动态内容的结构化XML,并使用Apache FOP执行XSL转换,您可以在其中定义各种输出格式如PDF,PS,RTF,PNG,......
互联网上有很多XSL-FO教程,你可以在this one上查看。
更新:我忘了提及DocBook,它定义了用于编写书籍,手册,学士论文等的语义语言。内容再次以XML格式存储,结构由DocBook架构定义,然后您可以使用预定义模板之一将其转换为PDF。转换为PDF也使用Apache FOP,但在这里您只创建带内容的XML(XSL模板已经完成)。