POST con:
POST https://yodocs.yodo.app/api/generate-doc
true per convertire in PDF (solo per i docx)Per sostituire i segnaposto:
/**
* 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);
}
?>