Invoking reporting templates from template scripts
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. |
To invoke a reporting template from your script, you will need to:
-
Obtain the unique identifier of the template.
-
Modify your script invoke a template identifier and trigger the download of the report.
Obtaining a template identifier
-
Go to http://localhost:5606/reporting and click on your template.
-
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 template identifier:
const { DataRecord, DataModelEntity, Relation } = sirenapi; const { EuiText, EuiTextColor, EuiIcon } = Eui; const loading = 'Loading...'; // The identifier of the PDF template const pdfTemplateId = 'r10S7Ky7Y';
-
Add a function to invoke the template using the
sirenapi.Reporting.download
API method: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) // more formats can be added here } });
-
You should now see the button to download a PDF when previewing a record. Click on it and you should get a PDF named after the company with the contents from the template created previously in jsreport.
Next steps
To see how to use Microsoft Word to create templates see Using Microsoft Word to create templates.
For more information about template scripts see template scripts.
For more information about scripting refer to the Scripting API documentation.