Create Word Documents
This guide will take you through the process of populating Office DOCX templates with data. The resulting DOCX file can then optionally be converted into a PDF document.
![]()
Populating DOCX templates with data and converting DOCX files into PDF documents require special licenses. Contact Sales for more information.
General Principles
Word templating consists of the following elements:
-
A DOCX file that will be used as the template.
-
A template model that contains the placeholder values to replace in the DOCX template.
-
Configuration for the template/model.
Template Model
The template model must contain:
-
A configuration containing both a start and end delimiter. The default delimiters are
{
and}
, and both can be configured in the request. -
At least one placeholder-value pair. The placeholder name must correspond to the placeholder defined in the DOCX template.
Populating a Document
PSPDFKit supports replacing placeholder text strings, automatic reflow, loops, and dynamic tables.
Text Replacement
Consider the following DOCX content:
Hello my name is {name}. There is {more}.
Use the following model:
"model": { "name": "Petey Eff", "more": "lorem ipsum dolor sit amet." }
The outcome in the output DOCX document will be:
Hello my name is Petey Eff. There is lorem ipsum dolor sit amet.
Loops
Consider the following DOCX content:
{ledger}: {#items} {name} {price} {/items}
Here, items
is the name of the loop, and name
and price
are placeholders for repetitive elements. Consider the following model:
"model": { "ledger": "Tom's groceries", "items": [ { "name": "A", "price": 10 }, { "name": "B", "price": 15 } ] }
The outcome in the output DOCX document will be:
Tom's groceries: A 10 B 15
Request Example with Custom Delimiters
This example performs dynamic population with custom delimiters ({{
and }}
) using a request to the /api/process_office_template
endpoint:
curl -X POST http://localhost:5000/api/process_office_template \ -H 'Authorization: Token token=<API token>' \ -H 'content-type: multipart/form-data' \ -F 'document=@/path/to/template.docx' -F 'model={ "config": { "delimiter": { "start": "{{", "end": "}}" } }, "model": { "placeholder": "replacement value", "loop-name": [ { "placeholder-within-loop": "replacement value", "another-placeholder-within-loop": "replacement value 2" }, { "placeholder-within-loop": "another replacement value", "another-placeholder-within-loop": "another replacement value 2" } ] } }' \ --output result.docx
POST /api/process_office_template HTTP/1.1 Authorization: Token token=<API token> Content-Type: multipart/form-data; boundary=customboundary --customboundary Content-Disposition: form-data; name="document"; template-document --customboundary Content-Disposition: form-data; name="model"; Content-Type: application/json { "config": { "delimiter": { "start": "{{", "end": "}}" } }, "model": { "placeholder": "replacement value", "loop-name": [ { "placeholder-within-loop": "replacement value", "another-placeholder-within-loop": "replacement value 2" }, { "placeholder-within-loop": "another replacement value", "another-placeholder-within-loop": "another replacement value 2" } ] } } --customboundary