Generating Docx/PDF documents
In many cases, you might need to generate downloadable documents from the data of your Bobuild app. This powerful feature lets you design a completely custom Docx template and have it filled by Bobuild on-the-fly within your app.
Some examples of documents you might want to generate:
- invoices
- reports
- certificates
Creating a template
Just use Microsoft Word of LibreOffice and design your template with no restrictions. Then, put some placeholders in the document where you want Bobuild to inject data.
In order to inject a simple variable, use this syntax:
{{ myvariable }}
Note: always put a space before and after the variable name!
If you want to use conditional logic, use this syntax:
My house is located {% if living_in_town %} in urban area
{% else %} in countryside {% endif %} and I love it.
If you want to repeat a block, use this syntax:
{% for p in products %}
- Product: {{ p.name }}
{% endfor %}
If you want to do repeat a table row, use this syntax:
Products |
---|
{%tr for p in products %} |
Product: {{ p.name }} |
{%tr endfor %} |
How does this work? If a tag starts with {%tr
instead of just {%
, the entire table row where it is located will be removed from the output. So, in this example, the second and the fourth row will be removed, and just the third row will be repeated for each item.
Configuring the template
Once you've crafted your template, you'll need to go to the Builder and configure the data source for the template. You'll define two things:
- which object fields shall be made available to the template (in the above examples, the
myvariable
andliving_in_town
fields) - which tabular data shall be also made available to the template (in the above example, the
products
variable which holds a collection of records extracted using some criteria)
Using the template
At this point you're all set! Just configure the task wherever you need in the app, and point it to your template. You'll need to configure the following settings:
Setting | Description |
---|---|
File name | The file name to use for the generated file |
Format | .docx or .pdf |
Field | The object field where you want the generated document to be stored. |
Common patterns
A common way to let users generate and download a document is to place an empty form, with just a submit button, that has a "Generate document" task, and then redirect to another page (or even to the same page) where the field holding the generated document is displayed so that users can click on it and download the document.