Docx to PDF
Overview
Build a PDF document from a ‘.docx’ template file and data. You can call this document from a logix function, it returns base64 (text) so it can output to a text parameter.
Watch this video recorded with the awesome Planet No Code team to see how it works.
All you need to do is:
- Create a document layout in Microsoft Word or Libre Office (free) and save it as a docx
- Map your data to the docx template by injecting it with
{instructions and variable placeholders}
- Upload it to your Comnoco
Docx to PDF
file
This will embed the document with it’s instructions and variable placeholders into your Comnoco template and merge it at runtime with the data you pass into the template allowing you to create customised transactional PDFs that you can then return in your API or send elsewhere (for example store or email them).
The Docx to PDF
block (like functions) allows you to define input variables. These allow you to pass data in from wherever you use/call the template. It also allows you to set internal variables for use when transforming building your data into the shape you need.
Data that the template uses must be written (by you/your process) to the Template Data
variable.
Example template file and data
You can find an example template and data here which you can import to your workspace and use to play with document generation.
Template Placeholders
To add {template variable placeholders}
into your docx document please refer to the below:
Data Mapping - Fields
{Path-to-Data}
in dot notation.
For example, if your data looks like this (either json input data or data in a Data Structure
block):
Then placeholders for this data to access objects, nested objects, and items in an array using dot notation would be:
-
Accessing Items:
- To access the name of the person:
{name}
(This will giveJohn Doe
). - To access the age of the person:
{age}
(This will give30
).
- To access the name of the person:
-
Accessing Nested Items:
- To access the street of the address:
{address.street}
(This will give123 Main St
). - To access the city of the address:
{address.city}
(This will giveAnytown
). - To access the country of the address:
{address.country}
(This will giveUSA
).
- To access the street of the address:
-
Accessing Items in an Array:
- Arrays are zero-indexed, meaning the first item is at position
0
, the second at position1
, and so on. - To access the first hobby:
{hobbies[0]}
(This will givereading
). - To access the second hobby:
{hobbies[1]}
(This will givehiking
). - To access the third hobby:
{hobbies[2]}
(This will givephotography
).
- Arrays are zero-indexed, meaning the first item is at position
-
Accessing Items Nested Within an Array:
-
To access the title of the first project:
{data.projects[0].title}
(This will giveProject A
).- To access the description of the first project:
{data.projects[0].description}
(This will giveDescription for Project A
). - To access the title of the second project:
{data.projects[1].title}
(This will giveProject B
). - To access the description of the second project:
{data.projects[1].description}
(This will giveDescription for Project B
).
- To access the description of the first project:
Data Mapping - Conditional Parts
To control whether or not something shows in your template depending on a condition, wrap your data in an object that contains a boolean (true/false flag) and use that to control your output.
For example, to conditionally control the output of a date and associated title, put the following into your template:
…and control it with this data:
Data Mapping - Loops
Where you have a list or array of repeating data, you can output it as repeating items.
For example, with the below data:
…output it as a repeating list of the dest
field with {#items}{dest}{/items}
. If you want a new line, ensure you add a new line between the tags.
Tables
Comnoco will automatically find and repeat table lines for you if you put a repeating item into a table row:
Lists
Comnoco will automatically find and repeat bullet points or numbered lists for you if you define a repeating item and tell it to search for the p
constructor with this format:
Data Mapping - Images & Links
Images require a special object in your data that contains the details Comnoco requires to inject and render them. The data structure is created automatically for you when you choose the Image helper from the add point when inside a document template.
These will add data as required:
And can be inserted into your template with {c_spo}{Image1.comnoco_image}{c_spc}
for images or {c_spo}{link1.comnoco_link}{c_spc}
for links where Image1
and link1
are the paths to your corresponding data objects.
Parameters
Parameter | Parameter | Valid Blocks | Required |
---|---|---|---|
Set a Dynamic Name | Show the Dynamic Name slot in the tree so that it can be set statically or dynamically | Boolean Block Group | No |
Dynamic Name | The dynamic name of this item | Text Block Group | No |
Attributes | Metadata values for this block. Used in XML and multipart/form-data. | Text Block Group | No |
Input Parameters | Define any input parameters which you want to pass in to this template (eg, user data, flags for display certain sections, etc.) | Basic Type Block Group | No |
Internal Variables | Define any variables which you want to use throughout this template which are only to be used in this template here (eg, reusable text strings, number of rows processed). | Basic Type Block Group | No |
Template file | The .docx template file to use for this document | Template File Block Group | No |
Template Data Variables | These parameters are required to generate your document. You can read and write to them. Use 'Set' blocks to set their values. | Basic Type Block Group | No |
Build Template Data | Transform your incoming data to create data to feed your template | Process Block Group | No |