A configuration Object
A template data object
Promise that resolves to an ArrayBuffer of a file converted to PDF
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',
},
],
},
},
)
.then(arrayBuffer => {
console.log('Successfully populated the template Document with data', arrayBuffer)
})
.catch(error => {
console.error(error.message)
})
The delimiter object sets the pair of delimiters that encloses a template marker
i.e. placeholder marker that need to be substituted with the data.
The model object associates a template marker with the corresponding substitution in the final, produced document.
=== Supported Template Features === 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.
The syntax for loops is # for the opening tag, and / for the closing one in the docs.
For instance if the document contains:
{#ITEMS} {name} {price} {/ITEMS}
Here, ITEMS is the name of the loop template marker, and name and price are regular placeholder
template markers over which the SDK iterates replacing the name placeholder with corresponding name value
in model, and similarly the price placeholder is replaced by the corresponding price value in model.
{
model: {
items: [
{
name: "A",
price: 10
},
{
name: "B",
price: 15
}
]
}
}
=== Image markers ===
You can author image markers in DOCX templates using:
{{%name}} for inline image placement{{%%name}} for centered image placementSupported sources in standalone mode:
source: "base64" with raw base64 payload (data) or data URL payloadsource: "dataUrl" as an alias for base64 (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+xml payloadsFor source: "url", cross-origin requests require proper CORS headers from the image host.
If CORS is not configured, pass base64/data URL payloads instead.
Sizing validation rules:
sizing: "original" requires no dimensions.sizing: "fixed" and sizing: "fit-max" require width and height (> 0).sizing: "fit-width" requires width (> 0).sizing: "fit-height" requires height (> 0).Example 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.
This is used to Populate the document template (Docx format) with corresponding data.
Returns a https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise|Promise resolving to an
ArrayBufferof a Docx, or rejecting with a NutrientViewer.Error.The resulting
ArrayBuffercan be converted to PDF with (). and finally loaded with ().It requires a configuration object and config object with data TemplateDataToPopulateDocument which contains the data to be populated in document and delimiter marker to know placeholder of the data. If the configuration is invalid, the promise will be rejected with a NutrientViewer.Error.