Embedding the template editor
Your users can prepare and change Oomplo templates inside your own application: the editor runs in an iframe of your page, takes your data and JSON Schema, and tells your page when the template changes, is saved or is published. The template stays in Oomplo; your server makes PDFs from it with the API.
1. Allow your pages #
Start Oomplo with the origins of the pages that may frame the editor:
OOMPLO_EMBED_ORIGINS="https://crm.example.com https://intranet.example.com"
Without it the editor cannot be framed at all (frame-ancestors 'none').
Set OOMPLO_PUBLIC_URL to the address your users reach Oomplo at.
2. Get a token on your server #
Never put an API key in a web page. Your server asks for a short-lived
token with its API key and gives the page only the returned url:
POST /api/embed
Authorization: Bearer oomplo_…
Content-Type: application/json
{ "origin": "https://crm.example.com", "template": "offer", "ttl": 3600 }
origin: your page's origin, one ofOOMPLO_EMBED_ORIGINS.template: the template to open, by its id or API name; leave it out to start a new template (titlenames it; theoomplo:readyevent tells its id).ttl: seconds the token lasts (1 hour by default, 8 hours at most).
The answer is { "token": "oomplo_embed_…", "url": "https://oomplo…/?embed=…",
"template": "…", "expires": … }.
What the token allows. The page is the editor of the token's one template until the token expires: editing, data, saving and publishing that template (PDF and DOCX are made in the browser). It cannot open or remove other templates, see stored files, runs or workflows, or reach accounts, API keys and settings, and cannot make other tokens. A token without a template belongs to the new template the editor saves first. Revoking the API key that asked for a token ends the token. Give the editor only to people who may change that template.
3. Frame the editor #
Copy embed/oomplo-embed.js (one file, no dependencies) to your application:
<div id="editor" style="height: 80vh"></div>
<script type="module">
import { mountEditor } from './oomplo-embed.js';
const { url } = await fetch('/my-backend/oomplo-token').then(r => r.json());
const editor = mountEditor(document.getElementById('editor'), {
url,
onReady: ({ template }) => console.log('open', template),
onChange: () => console.log('changed'),
onSaved: () => console.log('saved'),
});
await editor.ready;
await editor.setSchema(await fetch('/my-backend/order.schema.json').then(r => r.json()), 'order');
// … the user inserts fields and lays the template out …
const { version } = await editor.publish('from CRM');
</script>
| Call | What it does |
|---|---|
setSchema(schema, name?) | Your JSON Schema becomes the template's data name as sample values (names, dates, three rows per array), so the user can insert fields without real data |
setData(data, name?) | Your data (one JSON object) as the template's data name (default: its first JSON data) |
save() | Saves now (the editor also saves by itself half a second after a change) |
publish(label?) | Saves and publishes a version: POST /api/templates/{id}/render makes PDFs from it |
getPdf(), getDocx() | The PDF with the template's data, the template as a Word file (Uint8Array) |
destroy() | Removes the frame |
Events: onReady({ template, title }), onChange({ template }),
onSaved({ template }), onPublished({ template, version }),
onError(error).
Messages (without the helper) #
The editor talks only to its parent window and only with the token's
origin; the helper checks the editor's origin the same way. Requests
(postMessage to the frame, any of them with a requestId that comes back
in the answer):
| Request | Answer |
|---|---|
{ type: "oomplo:setData", data, name? } | oomplo:dataSet |
{ type: "oomplo:setSchema", schema, name? } | oomplo:dataSet |
{ type: "oomplo:save" } | oomplo:saved { template } |
{ type: "oomplo:publish", label? } | oomplo:published { template, version } |
{ type: "oomplo:getPdf" }, { type: "oomplo:getDocx" } | oomplo:pdf { bytes }, oomplo:docx { bytes } |
An error answers oomplo:error { message, requestId }. The editor sends on
its own oomplo:ready, oomplo:changed (after changes, at most every 300 ms)
and oomplo:saved (after each save).