Invoking reporting templates from template script perspectives

To invoke a reporting template from your script, you need to:

  • Get a unique identifier of the template.

  • Update your script to invoke a template identifier and trigger the download of the report.

Obtaining the template identifier

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

  2. Click the Link button and copy the last part of the link containing the identifier: Copying a template identifier

Calling jsreport from a template script "binary" perspective

  1. Open the script created earlier.

  2. Create a function that returns a binary object representation with a templateId property set to the identifier of the template and a data property with the data to pass to the template:

    function buildPdfDownload({ computedData }) {
      // The identifier of the pdf template
      const templateId = 'r10S7Ky7Y';
      const filename = `${computedData.companyName}.pdf`;
    
      return { filename, templateId, data: computedData };
    }
  3. Add the function to a perspective with a name that identifies it:

    context.registerPerspectives({
      binary: {
        'Pdf report': {
          initialData: getInitialData,
          enrich: enrichRecordData,
          render: buildPdfDownload
        }
      }
    });
  4. In the record preview, from Download as…​, click the download pdf button to download a pdf with the contents from the template created previously in jsreport.

Calling jsreport using the Reporting API

To create reports outside of binary perspectives, you can use the Reporting API. For example, you can create a view perspective with a custom button that creates a report:

function download() {
  // The identifier of the pdf template
  const pdfTemplateId = 'r10S7Ky7Y';

  sirenapi.Reporting.download(pdfTemplateId, { value: 123 }, `report.pdf`);
}

context.registerPerspectives({
  view: {
    'Create report button': {
      render() {
        return <button onClick={download}>Download report</button>
      }
    }
  }
});

Next steps

For information about using 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, see Scripting API documentation.