Templates
Use templates with context data to generate dynamic PDF content
The template rendering feature allows you to generate dynamic HTML content from predefined templates and context data before converting it to PDF.
Overview
Templates provide a powerful way to create dynamic, data-driven PDFs. Instead of sending complete HTML or URLs, you can reference a template and provide the context data to be injected into it. This approach is particularly useful for documents that follow a consistent structure but contain variable data, such as invoices, certificates, or personalized reports.
Parameter details
When using templates in the parts
array, you need to specify a content object with the following structure:
Must be set to template
to use template rendering
Usage
How templates work
- You create and store templates in your Vortex PDF account (via the dashboard or template management API)
- Each template contains HTML markup with placeholders for dynamic content
- When rendering a PDF, you reference a template by its ID and provide context data
- The API processes the template, injects your context data, and renders the resulting HTML into the PDF
Template syntax
Vortex PDF supports the Liquid template language for creating dynamic HTML templates. Liquid provides a flexible way to incorporate variables, logic, and loops into your templates. For comprehensive documentation on Liquid syntax, features, and usage examples, see LiquidJS.
Liquid basics
- Variables: Access context data using double curly braces:
{{ variable_name }}
- Filters: Transform data with pipe characters:
{{ variable_name | upcase }}
- Tags: Control flow with curly brace percentage signs:
{% if condition %}...{% endif %}
- Loops: Iterate over arrays with
for
tags:{% for item in items %}...{% endfor %}
Example template
Here’s an example of an invoice template using Liquid syntax:
This template demonstrates several Liquid features:
- Variable output with
{{ variable }}
- Conditional rendering with
{% if condition %}...{% endif %}
- Loops with
{% for item in items %}...{% endfor %}
- Filters for formatting values like
round
and mathematical operations - Variable assignment with
{% assign variable = value %}
Benefits of using templates
- Separation of concerns: Keep your presentation logic (templates) separate from your data
- Consistency: Ensure a consistent look and feel across all your generated documents
- Efficiency: Reduce payload size by sending only the data instead of complete HTML
- Maintainability: Update templates centrally without changing your application code
- Reusability: Use the same template with different data sets for various purposes
Template context
The context object can contain any valid JSON data, including:
- Simple key-value pairs
- Nested objects
- Arrays of data
- Numbers, booleans, strings, and null values
This flexibility allows you to provide complex data structures that can be rendered using conditional logic and loops in your templates.
Use cases
Templates are ideal for generating:
- Invoices and receipts: Use customer information and line items to create professional invoices
- Certificates: Generate personalized certificates with recipient details and achievements
- Reports: Create data-driven reports with dynamic charts, tables, and text
- Legal documents: Populate legal templates with specific case details and parties involved
- Personalized communications: Generate customized letters, statements, or notices
Example: Combining templates with other content parts
You can combine template-based content with other content types in a single PDF:
This example generates a PDF with a cover page from a URL, a dynamic report from a template, and a static appendix from HTML content.