📄 Document Export API

⚙️ Come funziona

  1. Prepara un template (.docx o .xlsx) rispettando i segnaposto illustrati in fondo alla pagina (pagine dei rispettivi repository).
  2. Invia una richiesta POST con:
    • Il file del template
    • I dati JSON
    • La tua chiave API
  3. Ricevi in risposta il file compilato (Word, Excel o PDF).

🧠 Endpoint

POST https://yodocs.yodo.app/api/generate-doc

📥 Parametri richiesti

📌 Note sui segnaposto

Per sostituire i segnaposto:

I segnaposto devono essere scritti come illustrato nei due repository sopra citati

/**
 * Next.js client-side: esportazione DOCX.
 */
export function generateDocxExport(
  data,
  exportName = "documento.docx",
  templatePath = "/templates/proposta.docx"
) {
  fetch(templatePath)
    .then((templateResponse) => {
      if (!templateResponse.ok)
        throw new Error(\`Errore nel caricamento: \${templateResponse.status}\`);
      return templateResponse.blob();
    })
    .then((templateBlob) => {
      const formData = new FormData();
      formData.append("data", JSON.stringify(data));
      formData.append("template", templateBlob, "template.docx");

      return fetch("https://yodocs.yodo.app/api/generate-doc", {
        method: "POST",
        headers: { "x-api-key": "INSERISCI_LA_TUA_API_KEY" },
        body: formData,
      });
    })
    .then((r) => r.blob())
    .then((blob) => {
      const a = document.createElement("a");
      a.href = URL.createObjectURL(blob);
      a.download = exportName;
      a.click();
      URL.revokeObjectURL(a.href);
    });
}
/**
 * Vanilla JS: esportazione DOCX/XLSX.
 */
function generateDocument(data, exportName = "file.docx", templatePath = "/templates/proposta.docx") {
  fetch(templatePath)
    .then((res) => res.blob())
    .then((templateBlob) => {
      const formData = new FormData();
      formData.append("data", JSON.stringify(data));
      formData.append("template", templateBlob, "template.docx");

      return fetch("https://yodocs.yodo.app/api/generate-doc", {
        method: "POST",
        headers: { "x-api-key": "INSERISCI_LA_TUA_API_KEY" },
        body: formData,
      });
    })
    .then((r) => r.blob())
    .then((blob) => {
      const a = document.createElement("a");
      a.href = URL.createObjectURL(blob);
      a.download = exportName;
      a.click();
    });
}
<?php
/**
 * PHP: invia JSON e template a Document Export API.
 */
function generateDocument($data, $templatePath, $apiKey) {
  $curl = curl_init();
  $cfile = new CURLFile($templatePath, 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', basename($templatePath));

  $payload = [
    'data' => json_encode($data),
    'template' => $cfile,
  ];

  curl_setopt_array($curl, [
    CURLOPT_URL => "https://yodocs.yodo.app/api/generate-doc",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => $payload,
    CURLOPT_HTTPHEADER => ["x-api-key: $apiKey"],
  ]);

  $response = curl_exec($curl);
  $info = curl_getinfo($curl);

  if ($info['http_code'] === 200) {
    file_put_contents('documento.docx', $response);
    echo "✅ Documento salvato come documento.docx";
  } else {
    echo "❌ Errore: " . $info['http_code'];
  }

  curl_close($curl);
}
?>