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
    Add third data

Let us look at an example to understand the condition better -

We can configure a template with a condition to see if an Invoice is paid or not. If it is paid, a particular data is filled, else an alternate data. If both the above conditions are not satisfied a third data can be filled.

Let us look at the below condition -

<<if [invoicePaid == false]>>
The invoice has not been paid.
The invoice has been paid.

Prepare the data for the above condition in the following way

  "VendorName": "Ynoox GmbH",
  "InvoicePaid": false,
  "InvoiceDue": "15-09-2022",
  "InvoiceAmount": 1200,
  "InvoiceDate": "01-09-2022"

Advanced Conditional Structure

Now, let us look at a more advanced condition for validating the data and populating a result with a dynamic date. Prepare a templated with the following condition -

<<if [invoicePaid == false && paymentDue < generatedOn]>>
  The invoice is unpaid and overdue.
<<elseif [invoicePaid == false && paymentDue > generatedOn]>>
  The invoice is not paid and due on <<[invoiceDue]:"dd.MM.yyyy" >>
  The invoice is paid.

The data for testing the above condition can be created in the following JSON format -

  "companyName": "Ynoox GmbH",
  "invoicePaid": false,
  "paymentDue": "15-09-2022",
  "generatedOn": "15-08-2022",
  "invoiceValue": "$1200"