Create a Word document named invoice-template.docx with the following content. Template tags are enclosed in << and >>, and order is the name under which the data source is passed to the engine.
Invoice for <<[order.Customer]>>
Date: <<[order.Date]:"yyyy-MM-dd">>
Below the text, add a table with a header row, one data row and a total row:
Product
Quantity
Amount
<<foreach [item in order.Items]>><<[item.Product]>>
The foreach tag repeats the table row for every item, and the sum extension method calculates the total. See Template Syntax for the complete syntax reference.
Create app.js next to the template and the data file:
'use strict';constgroupdocs=require('@groupdocs/groupdocs.assembly');// Load data from the JSON file. "order" is the data source name used in the template.
constdataSource=newgroupdocs.JsonDataSource("orders.json");constdataSourceInfo=newgroupdocs.DataSourceInfo(dataSource,"order");// Build the report from the template and the data
constassembler=newgroupdocs.DocumentAssembler();assembler.assembleDocument("invoice-template.docx","invoice.docx",dataSourceInfo);console.log("The report is saved to invoice.docx");process.exit(0);
Number and date formats follow the default locale of the Java virtual machine. Without a license, the report also contains an evaluation watermark; see Licensing and evaluation.
Save the report as PDF
To save the report in another format, pass LoadSaveOptions with the target FileFormat. This overload takes the data sources as a Java array, which you can create with the java bridge package:
'use strict';constjava=require('java');constgroupdocs=require('@groupdocs/groupdocs.assembly');constdataSource=newgroupdocs.JsonDataSource("orders.json");constdataSourceInfo=newgroupdocs.DataSourceInfo(dataSource,"order");constdataSources=java.newArray('com.groupdocs.assembly.DataSourceInfo',[dataSourceInfo]);// Save the report as PDF
constsaveOptions=newgroupdocs.LoadSaveOptions(groupdocs.FileFormat.PDF);constassembler=newgroupdocs.DocumentAssembler();assembler.assembleDocument("invoice-template.docx","invoice.pdf",saveOptions,dataSources);process.exit(0);