Building a full-featured DOCX template

In this tutorial, we will show you how to build a full-featured DOCX template and generate a document from it using a set of data and the Inkit API. Be sure to understand how to create a DOCX template and how to generate documents from the Inkit API before you start.

First, in Microsoft Word (or a similar word processor capable of creating DOCX files), create the following invoice template (or download it), which has examples of many of the types of fields you can embed in a DOCX template:

Save this document as invoice.docx.

Next, create a DOCX template by doing the following:

  1. In the Inkit web app, select Templates in the left sidebar and then click + Create.

  2. In the Create Template page, select DOCX from the File Type dropdown box, enter Invoice in Template Name and an optional description of the template in Description, and click Click or Drag a File to Area to Upload and select the invoice.docx that you previously created.

    Then click Save.

Finally, write a script in either Python or Node.js, which creates merge parameters for the template that we created from a set of data and generates an invoice from it:

# Import the Inkit Python package
import inkit
from inkit.exceptions import InkitResponseException
# Import json package
import json

# Replace the string below with your API key
inkit.api_token = "ENTER YOUR API KEY"

try:
   # Create a PDF document from a template
   resp = inkit.Render.create(
      # Specify the ID of the template
      template_id = "ENTER YOUR TEMPLATE ID",
      # Specify the data for the merge
      merge_parameters = {"company": {"name": "Acme Templates", "address": "1234 Main St., New York, NY 10001", "phone": "212.555.1000", "email": "[email protected]", "tax": "0.00%" },
                          "invoice": {"number": "A234324", "date": "7/15/23", "payment": "7/15/23", "items": [{"name": "Template A", "quantity": 2, "total": "$10.00", "amount": "$20.00"},{"name": "Template B", "quantity": 3, "total": "$5.00", "amount": "$15.00"},{"name": "Template C", "quantity": 1, "total": "$20.00", "amount": "$20.00"},{"name": "Template D", "quantity": 5, "total": "$2.00", "amount": "$10.00"},], "subtotal": "$65.00", "tax": "$0.00", "fees": "$0.00", "total": "$65.00"},
                          "LOGO": "{#IMAGE::https://2906690.fs1.hubspotusercontent-na1.net/hubfs/2906690/Inkit%20Template%20Images/logistica%20black.png::W=59,H=26,WRAP=INLINE#}",
                          "department": "A/R", "CCs": ["TC", "BV", "ER"],},
      # Specify the name of the PDF file
      destinations = {"inkit_storage": {"name": "New Invoice"}}
   )
   # Print the JSON repsonse of the API call
   print(json.dumps(resp.data, indent = 3))
except InkitResponseException as err:
   # Print any error
   print(err.response.data)
// Import the Inkit Node.js package
const Inkit = require("inkit");

// Replace the string below with your API key
Inkit.apiToken = "ENTER YOUR API KEY";

// Call the  PDF renderer
createRender();

// Create a PDF document from a template
async function createRender() {
    try {
        const result = await Inkit.Render.create({
            // Specify the ID of the template
            templateId: 'ENTER YOUR TEMPLATE ID',
            // Specify the data for the Name field
            mergeParameters:
                {"company": {"name": "Acme Templates", "address": "1234 Main St., New York, NY 10001", "phone": "212.555.1000", "email": "[email protected]", "tax": "0.00%" },
                "invoice": {"number": "A234324", "date": "7/15/23", "payment": "7/15/23", "items": [{"name": "Template A", "quantity": 2, "total": "$10.00", "amount": "$20.00"},{"name": "Template B", "quantity": 3, "total": "$5.00", "amount": "$15.00"},{"name": "Template C", "quantity": 1, "total": "$20.00", "amount": "$20.00"},{"name": "Template D", "quantity": 5, "total": "$2.00", "amount": "$10.00"},], "subtotal": "$65.00", "tax": "$0.00", "fees": "$0.00", "total": "$65.00"},
                "LOGO": "{#IMAGE::https://2906690.fs1.hubspotusercontent-na1.net/hubfs/2906690/Inkit%20Template%20Images/logistica%20black.png::W=59,H=26,WRAP=INLINE#}",
                "department": "A/R", "CCs": ["TC", "BV", "ER"],},
            // Specify the name of the PDF file
            destinations: {"inkit_storage": {"name": "New Invoice"}}
        });
        // Print the JSON response of the API call
        console.log(JSON.stringify(result.data, null, 3));
    } catch (error) {
        // Print any error
        console.error(error.response.status, error.response.statusText);
    }
}

In a terminal or command prompt, execute the script by running one of the following commands:

  • python [script] (Python)
  • node [script] (Node.js)

When viewing the generated invoice in the Inkit web app, you should see something like this: