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

  1. Go to http://localhost:5606/reporting and click on your template.

  2. Click on the Link button and then copy the last part of the URL, which contains the identifier: Copying a template identifier

Calling jsreport from a template script

  1. Open the script created in the previous section.

  2. 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';
  3. 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}`);
      }
    }
  4. 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
     }
    });
  5. 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.

    The PDF report

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.