Using Microsoft Word to create templates
This document describes the usage of version 1 of Template scripts which is deprecated. For step-by-step instructions on how to create templates with version 2 see Templating and reporting. |
jsreport supports the creation of templates from Microsoft Word files. Templates created using Word can also be used to generate reports in .docx format from template scripts.
-
To create a Microsoft Word template, open Word and paste the following contents to the document, using the handlebars syntax
{{<variable name>}}
to refer to your data fields:{{companyName}} Total raised amount: {{raisedAmount}} $
Format the text as desired, for example:
-
Save the document and drag it to the jsreport sidebar:
-
Create a new template named
sample-word
, with the recipe field set todocx
: -
Click on Next and select the existing
sample/sample
as the sample data: -
Click on Next and select the docx file previously uploaded as the Office template:
-
Click on Run and you should get a docx containing the sample data provided:
Obtaining a template identifier
-
Click on the sample-word template in the jsreport sidebar.
-
Click on the Link button and then copy the last part of the URL, which contains the identifier:
Calling jsreport from a template script
-
Open the script created in the previous section.
-
Declare an extra variable to hold the Word template identifier:
const { DataRecord, DataModelEntity, Relation } = sirenapi; const { EuiText, EuiTextColor, EuiIcon } = Eui; const loading = 'Loading...'; // The identifier of the PDF template const pdfTemplateId = 'r10S7Ky7Y'; const wordTemplateId = '1-pEMybJG';
-
Add a function to invoke the template using the
sirenapi.Reporting.download
:function buildDownload(type, templateId) { return async function (input) { const recordData = {}; await fetchRecordData(input, recordData); sirenapi.Reporting.download(templateId, recordData, `${recordData.companyName}.${type}`); } }
-
Declare that your template script can produce a PDF using the new function in the
registerTemplate
invocation:context.registerTemplate({ recordView: buildRecordView, download: { json: buildJsonDownload, // Declare that the script can produce a PDF and what function will take care of the generation process pdf: buildDownload('pdf', pdfTemplateId), // Declare that the script can produce a Word file and what function will take care of the generation process docx: buildDownload('docx', wordTemplateId) // more formats can be added here } });
-
You should now see the button to download a docx file when previewing a record. Click on it and you should get a Word file named after the company with the contents from the template created previously in jsreport.
Next steps
For more information about template scripts see template scripts.
For more information about scripting refer to the Scripting API documentation.