Il Desktop CLI accetta un modello .inks portatile e dati JSON. Questi esempi tengono i dati fuori da argomenti di riga di comando e gestiscono il risultato JSON versioned.
Python: rendere un cliente 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"])
Utilizzare un nome di output diverso per ogni rendering. InkSmith si rifiuta di sostituire un PDF esistente.
Node.js: convalidare prima del 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);
L'installatore Windows NSIS posiziona inksmith-cli.exe accanto a InkSmith.exe; utilizza il percorso completo se l'eseguibile non è su PATH.
Shell: utilizzare un file JSON
inksmith-cli validate --template invoice.inks
inksmith-cli render --template invoice.inks --data customer.json --output invoice-ada.pdf
Un comando di successo emette un singolo oggetto JSON su stdout. Un guasto emette un singolo oggetto JSON su stderr ed esce con uno stato non zero. Vedere Desktop CLI per limiti e codici di uscita.
Punta della privacy: Passare i dati attraverso
--data -mantiene il cliente JSON fuori dall'elenco degli argomenti di processo ed evita un file di dati temporaneo.