その他のツール

CLI の使用例

Python、Node.js、シェルから InkSmith を呼び出します。

デスクトップ CLI はポータブル .inks テンプレートと JSON データを受け付けます。 これらの例は、コマンドライン引数からデータを保存し、バージョン化された JSON 結果を処理する。

Python: 1つの顧客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"])

各レンダーごとに異なる出力名を使用します。 InkSmithは既存のPDFを取り替えることを拒否します。

Node.js: レンダリング前に検証

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

Windows NSIS インストーラーは、InkSmith.exe の横にある inksmith-cli.exe を配置します。実行可能が PATH ではない場合、フルパスを使用します。

Shell: JSONファイルを使用する

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

成功したコマンドは、stdout で 1 つの JSON オブジェクトを出力します。 失敗は、stderr の 1 つの JSON オブジェクトを出し、ゼロでないステータスで終了します。 制限コードと終了コードについては、デスクトップCLI を参照してください。

プライバシーのヒント: --data - でデータを渡すと、顧客の JSON はプロセスの引数に現れず、一時データファイルも不要です。