PDF Generation in Directus (Template)

How to generate PDF`s based on templates inside flows or extensions using TTA in Directus.

PDF template overview.

TTA PDF template overview
When opening the TTA module, you have a tab called 'Templates'. This is where you can create and manage your templates. You can create a new template by clicking on the top right '+' button. This will open a popup.
TTA PDF template settings

Template settings

OptionDescription
Template name The name of the template.
Description A short description of the template.
Format In most usecases A4 is the best option. You can select another option if needed.
Orientation Should the PDF be landscape or portrait?
Collection The collection that will be used to generate the PDF.
TTA PDF template editor

Template editor

After opening (or creating) a template, the template editor will open up.
In here you can type HTML on the left and you will see the output on the right. It allows the usage of LiquidJS for templating. This means you can use variables, loops, and conditionals. You can also use CSS to style the template.

Lets go into detail about all options.

Topbar

The top bar contains the following options (from left to right):
  • {Template name} - Clicking on the name will open the template settings.
  • Preview mode - Select a preview mode, this can be:
    • HTML - Provides a live preview of your template's rendered HTML
    • PDF (preview) - Displays the preview PDF (free but watermarked)
    • PDF - final version, without watermark
    • Code - preview without test input

    While the HTML and Code refresh the preview instantly, the PDF preview will require a button. The watermarked version is free to generate (check your plan on the limitations). While the final version might charge you depending on your plan.

  • ? - Displays a info popup about the preview mode options.
  • Align - Align your html.
  • Save - Saves the current template.
  • Resize - Resize the width of the code/preview window.
  • Close - Will close the template editor and return to the templates overview.

Code editor

The code editor is where you can write your HTML. It supports using LiquidJS for templating.
There is a floating dropdown that allows you to switch between the header/body/footer. It also has an option for Test input.

Test input

You can choose between 2 options:
  • Fixed - This is the JSON data that will be used to generate the preview.
  • Flow - This is allows you to select a Webhook flow and set the input values of that flow. The last operation of the flow will be used as the input for the template.

Header/footer properties

The header and footer have support for the following tags:
  • date - formatted print date
  • title - document title
  • pageNumber - current page number
  • totalPages - total pages in the document
Example html:
<span class="pageNumber"/> / <span class="totalPages"/>
This would show:
1 / 10
Note that the header and footer currently do not support rendering images from a URL. However, you can embed images by converting them to Base64 format. Tools like Base64 Guru can help you convert your images into Base64.