# Document generation (templates)

Source: https://mroffice.ai/en/developers/embed/templates

POST /api/v1/embed/templates/fill fills a .docx, .xlsx or .pptx template with JSON data and answers the file (or a PDF of it, output_format: "pdf"). The tags are the Mustache syntax docxtemplater made the common one, with double braces; a tag Word split across differently formatted runs is joined and takes its first character's formatting.

A Word template:

```text
Dear {{customer.name}},

{{#items}}
{{name}} × {{qty}}: {{price}}
{{/items}}

| Item               | Price        |
| {{#items}}{{name}} | {{price}}{{/items}} |

{{#vip}}Thank you for being a VIP.{{/vip}}{{^vip}}Join our club.{{/vip}}
{{%logo}}
```

| Tag | Means |
| --- | --- |
| {{name}} {{customer.address.city}} | The value as text (a \n is a line break). Looked up in the current loop item first, then outwards. Missing: empty, listed in result.missing (options.strict refuses instead). |
| {{#items}} … {{/items}} | A loop over a list (each item the scope of its copy), or a condition: a truthy value renders once (an object as the scope), false, empty or missing removes it. |
| {{^items}} … {{/items}} | The inverse: rendered only when the value is false, empty or missing. |
| {{.}} | The current item itself (a list of strings). |
| {{%logo}} | A picture (Word only): { url } or { data_base64 }, or a data: URI; width and height in pixels, else its own size up to the text width. PNG, JPEG or GIF, at most 10 MB. |

- Both loop tags in one paragraph: what is between them repeats inside the paragraph.
- In two cells of a table row (or in two rows of one table): the row(s) repeat.
- In two paragraphs: the paragraphs (and tables) from the first to the last repeat; a paragraph holding only a loop tag is removed with it.

Workbooks: tags in cells. A cell that is only {{n}} with a number or boolean value becomes a number or boolean. {{#lines}} in a cell of one row and {{/lines}} in the same or a later row repeat those rows once per item, as Excel's Insert Rows does: the rows below move down and every formula, merged cell, table, conditional format, data validation, defined name and chart range follows; a range that ended in the template rows grows over all copies (SUM(D5:D5) sums every line); relative references in the template rows move with each copy (=B5*C5 becomes =B6*C6). The workbook recalculates when opened.

Presentations: tags in every slide's text, loops in a text box's paragraphs and in table rows. No pictures and no repeated slides yet.

Fill it and write a PDF:

```bash
curl https://mroffice.ai/api/v1/embed/templates/fill \
  -H "Authorization: Bearer $MROFFICE_SECRET_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "template": { "upload_id": "1b0e…" },
    "data": {
      "customer": { "name": "Ada & Co" },
      "items": [ { "name": "Pen", "qty": 2, "price": "1.50" }, { "name": "Ink", "qty": 1, "price": "9.00" } ],
      "vip": true,
      "logo": { "url": "https://cdn.example.com/logo.png", "width": 120 }
    },
    "output_format": "pdf"
  }'
```

Mail merge: data as a list of records. merge.mode "zip" (default) answers a ZIP with one file per record, named by merge.file_name rendered with the record ("invoice-{{number}}"); "single" (Word templates) one .docx in which every record is its own section starting on a new page, as Word's Finish & Merge > Edit Individual Documents writes it (headers and footers are the first record's). At most 500 records; as PDFs one by one, 50 (or use single).

A mail merge (Node):

```js
const letters = await mro.fillTemplate({
  template: { uploadId },
  data: customers,                       // a list: one letter per record
  merge: { mode: "zip", file_name: "letter-{{id}}" },
});
const zip = await mro.download(letters);  // letter-1.docx, letter-2.docx, …

const one = await mro.fillTemplate({ template: { uploadId }, data: customers, merge: { mode: "single" } });
// one .docx, each record its own section (Word's Finish & Merge > Edit Individual Documents)
```

POST /api/v1/embed/templates/inspect with { template } lists the tags a template uses, in order, with their kind (value, section, inverted_section, image): what your form or data must fill. A broken template (an unclosed tag, a loop closed by another name, two loops that overlap) is refused at once with embed_template_invalid and the problems.
