Spanish Total.js
Italiano
Documentazione Per agenti IA
Indice

Generare da Node.js

Genera un template da un'app Node.js e salva l'HTML in un file.

.md GitHub

Questo script genera il template demo/invoice e salva il risultato come invoice.html. Richiede Node.js 18 o successivo, che ha fetch integrato.

1. Salva lo script come render.js:

const fs = require('fs');

const URL = 'https://templates.example.com/';
const TOKEN = process.env.OPENTEMPLATES_TOKEN;

async function render(id, data) {
    const response = await fetch(URL, {
        method: 'POST',
        headers: { 'content-type': 'application/json', 'x-token': TOKEN },
        body: JSON.stringify({ id, data })
    });
    if (!response.ok)
        throw new Error(response.status + ' ' + await response.text());
    return response.text();
}

render('demo/invoice', {
    number: '2026-003',
    customer: 'Jane Doe',
    items: [{ name: 'Consulting', price: 300 }, { name: 'Support', price: 120 }],
    total: 420
}).then(function(html) {
    fs.writeFileSync('invoice.html', html);
    console.log('Saved invoice.html,', html.length, 'characters');
}).catch(function(err) {
    console.error(err.message);
    process.exit(1);
});

2. Eseguilo con il tuo token API:

OPENTEMPLATES_TOKEN=YOUR_TOKEN node render.js
Saved invoice.html, 837 characters

3. Apri invoice.html nel browser.

Se l'ID del template è sbagliato, lo script stampa l'errore, per esempio:

400 [{"error":"Template not found"}]

Leggi Generare un template ed Errori.

Fatto in Europa