Using Microsoft Word to create templates

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.

  1. 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: Word template contents

  2. Save the document and drag it to the jsreport sidebar: Word template in sidebar

  3. Create a new template named sample-word, with the recipe field set to docx:

    Creating a new Word template Word template options

  4. Click on Next and select the existing sample/sample as the sample data: Word template sample data selection

  5. Click on Next and select the docx file previously uploaded as the Office template:

    Word template file selection

  6. Click on Run and you should get a docx containing the sample data provided: Word template rendering

Obtaining a template identifier

  1. Click on the sample-word template in the jsreport sidebar.

  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 Word template identifier:

    const version = 1;
    const type = 'template';
    
    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';
  3. Add a function to invoke the template using the sirenapi.Reporting.download:

    function buildDownload(type, templateId) {
      return async function (record, search) {
        const recordData = await fetchRecordData(record, search);
        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),
       // 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
     }
    });
  5. 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.

    The Word report

Next steps

For more information about template scripts see template scripts.

For more information about scripting refer to the Scripting API documentation.