Function populateDocumentTemplate
configuration: StandaloneConfiguration,
templateData: TemplateDataToPopulateDocument,
): Promise<ArrayBuffer>
Type Declaration
Parameters
configurationStandaloneConfigurationConfiguration object containing the document source and license key
templateDataTemplateDataToPopulateDocumentTemplate data object containing the optional delimiter config and model values
Returns
Promise that resolves to an ArrayBuffer containing the populated DOCX file
Pipeline overview
- Prepare a DOCX file containing placeholder tags (e.g.
{{name}}) - Call
populateDocumentTemplate()with your configuration and template data - Load the returned
ArrayBufferdirectly withload(), or pass it toconvertToPDF()first
Template syntax
Placeholders let users substitute a marker with some text, loops generate repetitions of a given pattern, and image markers insert images into the output DOCX.
Delimiters
Delimiters wrap every placeholder in the DOCX template. When
configis omitted, delimiters default to single braces:{and}. To use custom delimiters such as{{and}}, pass them explicitly inconfig:config: {
delimiter: {
start: '{{',
end: '}}',
},
}By default, dotted markers such as
{{account.name}}are treated as object path navigation, where.separates each level. To treat dots as literal characters in a key name instead, setobjectDelimiterto a different character — that character then becomes the path separator, and dots are treated as literals:config: {
delimiter: {
start: '{{',
end: '}}',
objectDelimiter: '|',
},
}With this config,
{{account.name}}resolves the literal key"account.name", while{{account|name}}navigates toaccount.namein the model.Simple substitution
{{name}}is replaced with the value ofnamein the model. Supported value types include strings, numbers, booleans, null, objects, and arrays. For the full type definition see TemplateDataToPopulateDocument.Conditionals and loops
The
#and/tags serve double duty depending on the model value type:- When the value is a boolean or truthy scalar, the block acts as a conditional:
{{#isActive}}This appears when isActive is true.{{/isActive}}- When the value is an array, the block acts as a loop, repeating once per item:
{{#items}}{{name}} — {{price}}{{/items}}- The
^prefix renders the block when the value is falsy (the inverse/else branch):
{{^isActive}}This appears when isActive is false.{{/isActive}}Example combining conditionals and inverse sections:
{{#isCool}}You're cool.{{/isCool}}
{{^isCool}}You're not cool.{{/isCool}}{
model: {
isCool: false
}
}Output:
You're not cool.Loops
For instance if the document contains:
{#ITEMS} {name} {price} {/ITEMS}Here,
ITEMSis the name of the loop template marker, andnameandpriceare regular placeholder template markers over which the SDK iterates replacing thenameplaceholder with correspondingnamevalue inmodel, and similarly thepriceplaceholder is replaced by the correspondingpricevalue inmodel.{
model: {
items: [
{
name: "A",
price: 10
},
{
name: "B",
price: 15
}
]
}
}Loops can be nested to any depth:
{
model: {
level0: [
{
label: "Group A",
level1: [
{
label: "Item 1",
level2: [
{ label: "Sub-item 1a" },
{ label: "Sub-item 1b" }
]
}
]
}
]
}
}Image markers
You can author image markers in DOCX templates using:
{{%name}}for inline image placement{{%%name}}for centered image placement
Supported sources in standalone mode:
source: "base64"with raw base64 payload (data) or data URL payloadsource: "dataUrl"as an alias forbase64(normalized at runtime)source: "url"for HTTP(S) URLs (fetched and normalized to base64 at runtime)
Unsupported in public APIs:
source: "file"(there is no filesystem semantic for public standalone usage)format: "svg"/image/svg+xmlpayloads
For
source: "url", cross-origin requests require proper CORS headers from the image host. If CORS is not configured, pass base64/data URL payloads instead.When using
source: "url", image format is resolved in the following order:- Explicit
formatproperty - MIME type from the HTTP response
- File extension from the URL
If none of these can be determined, the request will fail.
Supported format values:
png,jpg/jpeg,gif,bmp,tif/tiff. The aliasesjpegandtiffare normalized tojpgandtifrespectively.Sizing validation rules:
sizing: "original"requires no dimensions.sizing: "fixed"andsizing: "fit-max"requirewidthandheight(> 0).sizing: "fit-width"requireswidth(> 0).sizing: "fit-height"requiresheight(> 0).
Additional image properties:
Property Purpose borderWidthBorder width in pixels borderColorBorder color (hex or rgb) borderStyleBorder style (e.g. "dash")rotationRotation in degrees linkHyperlink URL applied to the image captionCaption text captionLabelCaption prefix text (e.g. "Figure")captionPositionCaption placement: "above"or"below"altTextAccessibility alt text titleImage title metadata pathLegacy alias for urlExample image model value:
{
_type: "image",
source: "base64",
data: "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQIHWP8z8DwHwAF+wJ/lxS6RQAAAABJRU5ErkJggg==",
format: "png",
sizing: "fit-width",
width: 96,
caption: "Company logo",
altText: "Logo"
}Multipage image payloads can use
pageNumber(1-based index).The API rejects the whole request when any image payload fails validation or URL resolution.
Cancellation
This operation respects the
signalproperty in the configuration object. Pass anAbortSignalto cancel a long-running template population:const controller = new AbortController()
const arrayBuffer = await NutrientViewer.populateDocumentTemplate(
{
document: '/sales-report.docx',
licenseKey: 'YOUR_LICENSE_KEY',
signal: controller.signal,
},
{ model: { name: 'Alex' } },
)
// To cancel:
controller.abort()Error reference
The promise rejects with a NutrientViewer.Error in the following cases:
- Invalid configuration
- Any image payload failing validation or URL resolution
- CORS failure when using
source: "url" - Operation cancelled via
AbortSignal
Example
const arrayBuffer = await NutrientViewer.populateDocumentTemplate(
{
document: '/sales-report.docx',
licenseKey: 'YOUR_LICENSE_KEY',
},
{
config: {
delimiter: {
start: '{{',
end: '}}',
},
},
model: {
products: [
{
title: 'Duk',
name: 'DukSoftware',
reference: 'DS0',
},
{
title: 'Tingerloo',
name: 'Tingerlee',
reference: 'T00',
},
],
},
},
)- Prepare a DOCX file containing placeholder tags (e.g.
Populates a DOCX template with data, replacing placeholders in the document with values from the provided model.
Returns a Promise resolving to an
ArrayBufferof a DOCX, or rejecting with a NutrientViewer.Error.The resulting
ArrayBuffercan be loaded directly with NutrientViewer.load(), or first converted to PDF with NutrientViewer.convertToPDF().If the configuration is invalid, the promise will be rejected with a NutrientViewer.Error.