Document TemplateIntroduction to Document Templates

Document Templates are similar to client side templates, but are designed to generate documents, specifically PDF and DOCX files.

Sample Data

Lets start with a simple example of creating a PDF that renders a table of people.

{
    "static": [
        {
            "firstname": "John",
            "lastname": "Public"
        },
        {
            "firstname": "Fred",
            "lastname": "Flintstone"
        }
    ]
}

JSON Template

To generate a PDF/DOCX template, you need to supply a JSON definition.

Below is a template that generates a document that contains a simple table.

{
    "table": {
        "columns": [
            "2in",
            "2in"
        ],
        "area": {
            "border": {
                "style": "Single"
            }
        },
        "body": {
            "source": "*root",
            "sections": [
                {
                    "source": "*header",
                    "rows": [
                        {
                            "textformat": {
                                "bold": true
                            },
                            "area": {
                                "bottom": {
                                    "style": "Double"
                                }
                            },
                            "cells": [
                                {
                                    "text": "First Name"
                                },
                                {
                                    "text": "Last Name"
                                }
                            ]
                        }
                    ]
                },
                {
                    "rows": [
                        {
                            "cells": [
                                {
                                    "field": "firstname"
                                },
                                {
                                    "field": "lastname"
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    }
}

The Data applied against the simple Document Template creates a table of people.

images/sample1.PNG
Sample PDF

Understanding The Template

At the top of the template, the content is tabular, so we use the "table" container.

The first element inside "table" we see is "columns", which is an array of either columns widths, or an array of objects with a "width" property, we use the array of widths here since we are not using any other column properties in this example.

You will note that the columns are both 2in, scalar values are expressed in CSS compatible units.

This template has a "area" defined at the table level, with "border" set "style" or "Single" - this is a border for the entire table.

The "body" tag is the table 'object' (or objects), this can contain other table objects in "sections" which can be nested.

The "source" property in table object indicates what rows are to be iterated over, the source of "*root" indicates this object iterators over the top level array

The "sections" array lists all the nested table objects. We need two sections because we have a heading row and a data row.

The first table object in sections has a source or "*header", which indicates we only want this row showing up once for the parent "*root".

The first table object "rows" first and only row contains a "textformat" property, this overrides the font and text settings for the row, here we are setting the font to "bold".

The first row contains a "area" property which contains a "bottom" property, which is an override for the bottom border, which gives us our thick line under the title.

The first row contains two "cells" , which define "text" elements for the column headings

The second table object in sections has no source, so it inherits the '*root', which means we will get one record for every row at the top level of the data.

The second row contains two "cells" , which define "field" to read from the data.