Import
Example: import a JasperReports report
Import an account statement from a .jrxml file, read every file the import writes, render it with sample data and see how a named element is reported.
On this page
This page imports one report from start to end: an account statement (a
Kontoauszug). All names and numbers are made up. The same report is in the
Brevier repository as templates/kontoauszug/java/before-jasper/kontoauszug.jrxml.
The report
The report has 8 parameters, 5 fields and a page header, a column header, a detail band and a page footer. This is the part that matters:
<parameter name="bankName" class="java.lang.String"/>
<parameter name="auszugNummer" class="java.lang.Integer"/>
<parameter name="alterKontostand" class="java.math.BigDecimal"/>
<!-- 5 more parameters -->
<field name="buchungstag" class="java.lang.String"/>
<field name="betrag" class="java.math.BigDecimal"/>
<!-- 3 more fields -->
<pageHeader>
<band height="90">
<textField>
<reportElement x="0" y="0" width="260" height="20"/>
<textElement><font size="14" isBold="true"/></textElement>
<textFieldExpression><![CDATA[$P{bankName}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="300" y="0" width="215" height="20"/>
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA["Kontoauszug Nr. " + $P{auszugNummer}]]></textFieldExpression>
</textField>
<!-- 6 more text fields -->
</band>
</pageHeader>
<detail>
<band height="20">
<textField>
<reportElement x="0" y="0" width="75" height="16"/>
<textFieldExpression><![CDATA[$F{buchungstag}]]></textFieldExpression>
</textField>
<!-- 4 more text fields -->
</band>
</detail>Run the import
brevier import kontoauszug.jrxml -o templates/kontoauszugImported kontoauszug.jrxml: wrote kontoauszug.html, kontoauszug.css, kontoauszug.schema.json, sample.jsonThere is no Not converted line, so the import read every element. The folder
templates/kontoauszug now holds four files.
The template
kontoauszug.html has the page header and footer as running blocks, and the
detail band as a table with one row for each booking. This is the page header
and the table. The file also has a footer block, <p>{{bankName}} · Kontoauszug</p>.
<div class="running-header">
<div class="header-left">
<h1>{{bankName}}</h1>
<p>{{kontoinhaber}}</p>
<p>IBAN: {{iban}}</p>
<p>BIC: {{bic}}</p>
</div>
<div class="header-right">
<p>Kontoauszug Nr. {{auszugNummer}}</p>
<p>Datum: {{auszugDatum}}</p>
<p>Alter Kontostand: {{alterKontostand}} EUR</p>
<p>Neuer Kontostand: {{neuerKontostand}} EUR</p>
</div>
</div>
<table class="report-table" data-brevier-uebertrag="true">
<colgroup><col><col><col><col><col data-brevier-carry="true"></colgroup>
<thead>
<tr><th>Buchungstag</th><th>Valuta</th><th>Vorgang</th><th>Buchungstext</th><th class="amount">Betrag</th></tr>
</thead>
<tbody>
{{#each rows}}
<tr><td>{{buchungstag}}</td><td>{{valuta}}</td><td>{{vorgang}}</td><td>{{buchungstext}}</td><td class="amount">{{betrag}}</td></tr>
{{/each}}
</tbody>
</table>| In the report | In the template |
|---|---|
$P{bankName} |
{{bankName}} |
"Kontoauszug Nr. " + $P{auszugNummer} |
Kontoauszug Nr. {{auszugNummer}} |
| The column header band | <thead> |
| The detail band | {{#each rows}} ... {{/each}}, one cell for each $F{...} |
A field of a number type (java.math.BigDecimal) |
class="amount", right-aligned |
The import turns on the carry-over (Übertrag) for the first column of a decimal
type: the Betrag column carries its total to the next page. The attributes
are data-brevier-uebertrag and data-brevier-carry: see
Tables.
kontoauszug.css holds the page size (595pt by 842pt), the margins, the
running header and footer, and the table rules. The font size 14
and bold of the bank name became the rule .header-left h1.
The schema
kontoauszug.schema.json has one property for each parameter, and rows with
one property for each field. This is a part of it:
{
"title": "kontoauszug",
"type": "object",
"properties": {
"bankName": { "type": "string" },
"auszugNummer": { "type": "integer" },
"alterKontostand": { "type": "number" },
"rows": {
"type": "array",
"items": {
"type": "object",
"properties": {
"buchungstag": { "type": "string" },
"betrag": { "type": "number" }
},
"required": ["buchungstag", "betrag"]
}
}
},
"required": ["bankName", "auszugNummer", "alterKontostand", "rows"]
}java.lang.Integer became integer, java.math.BigDecimal became number,
and java.lang.String became string. The real file lists all 8 parameters and
all 5 fields.
The sample data
sample.json is generated from the schema. It is a starting point for the
first render, not your data:
{
"bankName": "Erika Mustermann",
"auszugNummer": 1,
"alterKontostand": 12.5,
"rows": [
{ "buchungstag": "Beispieltext 1", "betrag": 12.5 },
{ "buchungstag": "Beispieltext 2", "betrag": 7.25 },
{ "buchungstag": "Beispieltext 3", "betrag": 3.75 }
]
}The generator picks a value by the name of a field (iban, datum, and any
name that contains name) and otherwise by its type. Some values do not fit
their field: bankName holds a person's name, because its name contains
name. Replace the values with your own, or pass the data of your service.
Render and lint
brevier pdf templates/kontoauszug/kontoauszug.html \
--data templates/kontoauszug/sample.json -o kontoauszug.pdf
brevier lint templates/kontoauszug/kontoauszug.html \
--data templates/kontoauszug/sample.jsonThe first command writes the PDF and checks the data against
kontoauszug.schema.json. The second lists text that does not fit its column
and other layout problems: see Check a document with lint.
When the import names an element
Add a line to the detail band, as a divider under each row:
<detail>
<band height="20">
<!-- the 5 text fields -->
<line>
<reportElement x="0" y="18" width="515" height="1"/>
</line>
</band>
</detail>The import still writes the four files, and it prints one more line:
Imported kontoauszug.jrxml: wrote kontoauszug.html, kontoauszug.css, kontoauszug.schema.json, sample.json
Not converted: line 137: line in the detail bandline 137 is the line of the <line> element in your .jrxml. The table in
Ways out says what to do: a CSS border
on the cells, tbody td { border-bottom: 0.5pt solid #000; }.
Next
Replace the call that fills the report with the Brevier call: Replace the call.