Skip to content

Fill tags on the backend → PDF ​

The editor stores as a VariableNode. The backend does not parse HTML. Discover inject points with reportSlots (velo-text/api-report), then send AST + data + assets.

json
{
  "document": { "...editor.getDocument()..." },
  "data": { "name": "Ada Lovelace", "total": 1280, "customer": { "name": "Acme" } }
}

Same body for:

js
/**
 * Browser: send the editor AST + the values for every {{tag}}.
 * Works with the Vite plugin, Express, or Vercel — same JSON body.
 */
export async function fillTagsToPdf(document, data, assets) {
  const res = await fetch("/api/pdf", {
    method: "POST",
    headers: { "content-type": "application/json" },
    body: JSON.stringify({ document, data, assets }),
  });
  if (!res.ok) throw new Error(await res.text());
  return res.blob();
}
ts
import { defineConfig } from "vite";
import { vitePdfPlugin } from "velo-text/backend";

export default defineConfig({
  plugins: [vitePdfPlugin("/api/pdf")],
});
ts
/**
 * Express — POST /api/pdf
 * Body: { document, data, assets? }
 * `document` is editor.getDocument(); `data` fills every {{tag}} from the frontend.
 */
import express from "express";
import { expressPdfHandler } from "velo-text/backend";

const app = express();
app.use(express.json({ limit: "8mb" }));
app.post("/api/pdf", expressPdfHandler);
app.listen(Number(process.env.PORT) || 3000);
ts
/** Vercel Node function: api/pdf.ts */
export { vercelPdfHandler as default } from "velo-text/backend";

Zero runtime dependencies — TypeScript strict — MIT