További eszközök

CLI-példák

Hívja az InkSmith programot Pythonból, Node.js-ből vagy parancssorból.

A CLI asztal egy hordozható .inks sablont és JSON adatot fogad el. Ezek a példák kihagyják az adatokat a parancssori argumentumokból, és kezelik a fordított JSON eredményt.

Python: render egy ügyfél 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"])

Minden renderhez más kimeneti nevet kell használni. A InkSmith nem helyettesíti a meglévő PDF-et.

Node.js: érvényesítés a renderelés előtt

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);

A Windows NSIS telepítő a inksmith-cli.exe-et a InkSmith.exe mellé helyezi; használja ezt a teljes útvonalat, ha a végrehajtható nincs a PATH-on.

Shell: JSON fájl használata

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

Egy sikeres parancs egyetlen JSON objektumot bocsát ki a stdout-on. A hiba egyetlen JSON objektumot bocsát ki a Stderr-en, és nem nulla státuszú kilép. A határértékeket és a kilépési kódokat lásd a Asztali CLI-ben.

A Adatvédelmi tipp: --data --en keresztüli adatátvitel kihagyja a JSON-t a folyamatparaméterek listájából, és elkerüli az ideiglenes adatállományt.