Beyond the editor

CLI examples

Call InkSmith from Python, Node.js, or a shell.

The Desktop CLI accepts a portable .inks template and JSON data. These examples keep the data out of command-line arguments and handle the versioned JSON result.

Python: render one customer PDF

import json
import subprocess

customer = {"customer": {"name": "Ada"}}
result = subprocess.run(
    [
        "inksmith-cli", "render",
        "--template", "invoice.inks",
        "--data", "-",
        "--output", "invoice-ada.pdf",
    ],
    input=json.dumps(customer),
    capture_output=True,
    text=True,
    check=False,
)

payload = json.loads(result.stdout if result.returncode == 0 else result.stderr)
if result.returncode != 0:
    raise RuntimeError(f"{payload['error']['code']}: {payload['error']['message']}")

print(payload["output"])

Use a different output name for each render. InkSmith refuses to replace an existing PDF.

Node.js: validate before rendering

import { spawnSync } from "node:child_process";

function runInkSmith(args, data) {
  const result = spawnSync("inksmith-cli", args, {
    encoding: "utf8",
    input: data === undefined ? undefined : JSON.stringify(data),
  });
  if (result.error) throw result.error;
  const payload = JSON.parse(result.status === 0 ? result.stdout : result.stderr);
  if (result.status !== 0) {
    throw new Error(payload.error.code + ": " + payload.error.message);
  }
  return payload;
}

runInkSmith(["validate", "--template", "invoice.inks"]);
const output = runInkSmith(
  ["render", "--template", "invoice.inks", "--data", "-", "--output", "invoice-ada.pdf"],
  { customer: { name: "Ada" } },
);
console.log(output.output);

The Windows NSIS installer places inksmith-cli.exe beside InkSmith.exe; use that full path if the executable is not on PATH.

Shell: use a JSON file

inksmith-cli validate --template invoice.inks
inksmith-cli render --template invoice.inks --data customer.json --output invoice-ada.pdf

A successful command emits a single JSON object on stdout. A failure emits a single JSON object on stderr and exits with a non-zero status. See Desktop CLI for limits and exit codes.

Privacy tip: Passing data through --data - keeps customer JSON out of the process argument list and avoids a temporary data file.