Create advanced templates for document generation

By Vishnu Subramoniam | Automation

Create advanced templates for document generation

Generating documents using Mail merge is the method of reading data from a data source and replacing corresponding variables in a template document. It usually requires two files, templates for storing the merge fields - the variable for the data to be inserted. The other contains both the instructions for formatting the variable data and the information that will be identical across each result of the mail merge.

Templates can be created using various syntaxes. PDF4me accepts Word, HTML, and PDF templates for generating documents. With the new reporting engine, you can use a simple syntax structure to design advanced templates in Word for your Invoices, receipts, contracts, quotes, or any personalized document. For detailed reading follow the link to our documentation -

How to design Word Templates?

You can manually create the merge fields or tokens in the following syntax

<<[FieldName]>>

You will also be able to add conditional expressions to evaluate and add a particular data option on the result of the condition. Let us look in short at how these options can be used for designing Word templates for document generation.

Conditional Expressions

The Generate Documents action in PDF4me lets you use conditional expressions to populate data into templates. Based on the status of a condition, dynamic data can be added to a document. You can even combine expressions to satisfy multiple conditions before a particular data is filled.

If else condition syntax

A basic syntax of how an if-else condition can be used in a document -

<<if [condition1]>>
  Add first data
  <<elseif [condition2]>>
    Add second data
  <<else>>
    Add third data
<</if>>

There are more advanced conditions you can apply. Read more on Conditional statements

Formating in Templates

While generating documents you may want to apply formatting to a dynamically populated result - like a resulting value of an expression. Using PDF4me, you can easily apply formatting to the results of your condition or expression.

The basic syntax for applying formatting -

<<[token]:"format">>

E.g-

Invoice Date:  <<[invoiceDate]:"dd.MM.yyyy">>

To know all the formatting capabilities read more on formatting Word templates

Tables in Templates

Tables are a very common part of most documents. They help effectively display data for easy comparison, reference, or for computing. The PDF4me document generator can effectively insert tables into documents based on the data.

Let us look at examples of how the table syntax can be created.

We can create a Product list to understand this.

Column 1: <<foreach [product in products]>><<[product.productName]>>
Column 2: <<[product.manufactured]:"yyyy.MM.dd">>
Column 3: <<[product.price]>><</foreach>>

There are more about various table structures you can read in the documentation on Tables

Add Images in Templates

PDF4me also lets you insert images into documents. The syntax for images can be added in the design phase of the template.

The image syntax should be added inside a Text Box in order for populating an image.

Images can be added using 2 ways

Using a Public Image URL

<<image [imageUrl]>>

The data can be passed to the above syntax in the following ways respectively -

With a Base64 string of an Image

<<image [imageBase64]>>

Read the documentation for details on how to add images in Word templates with examples -

Insert HTML snippets in Templates

Now you can also directly add HTML sections and formatting to templates for populating data. You can use the following syntax for inserting the HTML

<<[token] -html>>

For seeing example read the documentation for inserting HTML in Word templates

Sample Template

Let us look at a quick sample to try. The following is a sample Word template and data for you to quickly try out the generate document action. You can download the Word template and use the JSON for the template for testing.

Sample Word template image

Sample Data

{
    "VendorName": "PDF4me",
    "VendorCity":"Zurich",
    "VendorCountry":"Switzerland",
    "VendorZip":"8630",
    "VendorContact":"123456",
    "InvoiceDate":"22-08-2022",
    "CustomerName":"John Doe",
    "CustomerCity":"Start City",
    "CustomerCountry":"US",
    "CustomerZip":"3214",
    "products": [
    {
      "prodctName": "PDF Converter",
      "value": 2000,
      "expires": "01/08/2025"
    },
    {
      "productName": "PDF API",
      "value": 1250,
      "expires": "18/02/2029"
    },
    {
      "productName": "PDF Editor",
      "value": 1999,
      "expires": "12/12/2027"
    }
   ],
   "comment":"This document is <b>computer generated</b> and does not require a signature.",
  }

Related Blog Posts