Build New DOCX Template
This section explains the basics of building a DOCX template for exporting e.g., a Quote to DOC/PDF. The procedure consists of two steps:
Create Logic
This calculation logic prepares data for the Word template.
The concept is: 1 element = 1 output variable in the DOCX template.
Complex elements can return a map or an array of maps, so they can contain multiple variables.
Fill Variables with Data
Let's assume that you have designed the following template in MS Word. The expressions in curly brackets are variables you are intending to use.
To replace the variables with data, you create elements in the calculation logic which will provide the values. The elements must be named as the variables in the Word.Â
Each element retrieves/calculates the variable value, which replaces the placeholders in the template. An element's result can be a primitive (Boolean, numerical or string) value or a tree structure combining any nesting of primitives, lists and maps.Â
Get Object Details
To fill in dynamic values, you need the context of that quote. Use the API call api.currentItem
which gives you a quoteView.
To get the name of the Quote, use the following code:
api.currentItem("uniqueName")
The code for getting the customer name will be similar.
Populate Rows
To populate the table rows in the sample template above, we need to get the Quote's line items:
api.currentItem("lineItems").collect { li ->
[sku: li.sku, qty: 1, price: li.outputs.find { it.resultName == "Listprice"}.result ]
}
Notes:Â
The
api.currentItem()
call without arguments can have significant performance impact. Make sure that you use the string path param to access only the data you need for the template, or make sure you cache the value ofapi.currentItem()
without arguments as the very first call in your logic.For CFS, Price Lists, LPG, Rebate Records and Simulations, you always have to wrap the variable with the 'items' tag:Â
{#items}
indicates the start of a row;{/items}
 indicates the end of a row in the template. For example:Â{#items} Hello {name}! {/items}
Set Output File Name
You can also have a variable name of the output PDF file. Add an element to the logic named targetFileName which will define the file name pattern. For example:
api.currentItem('uniqueName') + "-" + api.currentItem('label') + "-" + api.currentItem('customerName')
Logic Sample with PDF Output
To select the template of your choice, append the templateName
parameter with the name of the template.
The output=pdf
parameter ensures that the result will be PDF.
Create DOCX File
Create a new MS Word document and use the previously defined variables (elements in the logic) with this syntax {variableName}. You can find the full syntax at https://cloudconvert.com/blog/syntax-for-the-docx-pptx-templating-feature.
You may test the template options at the Docxtemplater website: https://docxtemplater.com/demo/#simple
Notes:Â
When creating the document in Open Office XML, the generated text needs to be a valid XML. For example, if you have "&" in the text, it needs to be escaped.
If the template is to be used for e-signature, pay attention to the necessary placeholders and their syntax.
When the DOCX template is ready, refer back to the sequence of required steps.
See also Example of Using Variables in DOCX Template.
Found an issue in documentation? Write to us.
Â
Pricefx version 13.1