Import
Import a Word file
Import a Word letter with merge fields or template tags, see which field syntaxes the import reads, and what it does with styles, header, footer and the schema.
On this page
brevier import mahnung.docx -o templates/mahnungThe import writes three files: mahnung.html, mahnung.css and
mahnung.schema.json. It writes no sample.json. Its input is a Word file in
which a template library has marked where the data goes. The data code of that
library stays as it is.
The fields that the import reads
| In Word | In the template |
|---|---|
MERGEFIELD KundeName |
{{ KundeName }} |
$kunde.name (Velocity: XDocReport, docx4j) |
{{ kunde.name }} |
A table row with #foreach($p in $posten) |
A row that repeats over posten: {{#each posten}} |
{{ a.b }} and {%tr for p in a %} (Jinja: docxtpl) |
A tag and a repeating row |
{kunde.name} and {#posten} (docxtemplater) |
A tag and a repeating row |
{{Kunde.Name}} and {{#Posten}} (DocxTemplater for .NET) |
A tag and a repeating row |
An example: the Mahnung
The Mahnung is a dunning letter with a customer address, a table of open
positions and a header and footer. All names and numbers are made up. This is
the Velocity version, as XDocReport reads it. The right column is what the
import wrote into mahnung.html.
| In the Word file | In mahnung.html |
|---|---|
Merge field $kunde.name |
<p>{{ kunde.name }}</p> |
Kunde: $kunde.name |
<p>Kunde: {{ kunde.name }}</p> |
Zahlungserinnerung Nr. $mahnung.nummer in the style Heading 1 |
<h1 class="Heading1">Zahlungserinnerung Nr. {{ mahnung.nummer }}</h1> |
| The words "keinen Zahlungseingang" in the character style Betont | <span class="Betont">keinen Zahlungseingang</span> |
Header: $mahnung.firma · Forderungsmanagement |
<div class="docx-header"> with the paragraph |
Footer: IBAN $mahnung.iban |
<div class="docx-footer"> with the paragraph |
The table in the Word file has a header row and one row that repeats. The row
starts with the merge field @before-row#foreach($p in $mahnung.posten) and
ends with @after-row#end. Its cells hold $p.bezeichnung, $p.menge and
$p.betrag. The import writes:
<table class="MahnTabelle">
<thead>
<tr><th><p><strong>Bezeichnung</strong></p></th><th><p><strong>Menge</strong></p></th><th><p><strong>Betrag</strong></p></th></tr>
</thead>
<tbody>
{{#each mahnung.posten}}<tr><td><p>{{ bezeichnung }}</p></td><td><p>{{ menge }}</p></td><td><p>{{ betrag }}</p></td></tr>{{/each}}
</tbody>
</table>The same letter written for docxtpl, docxtemplater or DocxTemplater for .NET gives
the same mahnung.html.
Styles, header and footer
- Each Word style that the file uses becomes a CSS rule with the name of the
style, for example
.Heading1(font-size: 16pt, bold, colour#1F3864) and.Betont(bold, colour#C00000). - The page size and the margins of the document become
@page. - The header and the footer of the page become the blocks
docx-headeranddocx-footer. The CSS puts them in the page margin with@top-center { content: element(docx-header); }and the same for the footer. - A font that is embedded in the Word file is not copied. Install the font on
the machine that renders, or load it with
@font-face.
The schema
The schema lists every field of the template. Each value is a string, and a
repeating row becomes an array of objects:
{
"type": "object",
"properties": {
"mahnung": {
"type": "object",
"properties": {
"nummer": { "type": "string", "title": "nummer" },
"posten": {
"type": "array",
"items": {
"type": "object",
"properties": {
"bezeichnung": { "type": "string", "title": "bezeichnung" },
"menge": { "type": "string", "title": "menge" },
"betrag": { "type": "string", "title": "betrag" }
}
}
}
}
}
}
}The real file lists all fields of mahnung and kunde, and each object has
required and "additionalProperties": false. The engine rejects a field that
the schema does not list.
Change the type of a field where the Word file could not say it, for example
"type": "number" for menge.
If your Java code binds objects by the name of their class (step 2),
move each object into $defs, with the name of the class, and point its root
at the type with $ref:
{
"properties": { "kunde": { "$ref": "#/$defs/Kunde" } },
"$defs": {
"Kunde": {
"type": "object",
"properties": { "name": { "type": "string" } }
}
}
}Check the result
Open mahnung.html and mahnung.css and compare them with the Word file. The
import writes what it can read. Then render the letter with data:
brevier pdf templates/mahnung/mahnung.html --data mahnung.json -o mahnung.pdfNext: Replace the call. For a worked example from start to end, see Example.