Guide · REST API
Convert HTML to DOCX with one API call
The layout lives in your application as HTML. Send the body to the API and receive a Word document whose headings, paragraphs, lists and tables are real Word formatting your users can keep editing. The same call with format=PDF returns the paginated A4 version.
Why not build the Word file in code
- An HTML-to-Word library in your process binds you to one runtime and one HTML subset; the next service in another stack starts over.
- Converting through Word on a server adds an Office installation to your deployment and is not supported for unattended use.
- Users want to change what your application rendered: a PDF freezes it, a DOCX hands it back to them.
The request
Multipart transport: the HTML is the html part, the format a plain form value; format=DOCX returns the Word file, PDF the paginated pages. An optional watermark part stamps a text on every page. External images and stylesheets are never fetched, so embed images as data URIs.
TOKEN=$(curl -sS https://auth.xgenerate.io/realms/xgenerate/protocol/openid-connect/token \
-d grant_type=client_credentials \
-d client_id="$XGENERATE_CLIENT_ID" \
-d client_secret="$XGENERATE_CLIENT_SECRET" | jq -r .access_token)
# The HTML body is the html part; format=DOCX returns the Word file, PDF the paginated pages.
curl -sS https://api.xgenerate.io/v2/documents/generate-from-html \
-H "Authorization: Bearer $TOKEN" \
-F "html=@summary.html;type=text/html" \
-F "format=DOCX" \
-o summary.docxFrom .NET
using System.Net.Http.Json;
using var http = new HttpClient();
http.DefaultRequestHeaders.Authorization = new("Bearer", token);
// JSON transport: the HTML travels as text. Inline styles and <style> blocks are kept;
// external images and stylesheets are never fetched, so embed images as data URIs.
var html = """
<style>h1 { color: #1f2937; } td { padding: 4px 8px; }</style>
<h1>Quarterly summary</h1>
<p>Revenue grew by <b>12 %</b> against the previous quarter.</p>
<table>
<tr><th>Region</th><th>Revenue</th></tr>
<tr><td>DACH</td><td>CHF 1.2 M</td></tr>
<tr><td>France</td><td>CHF 0.8 M</td></tr>
</table>
""";
var request = new { filename = "summary.html", html, format = "DOCX" };
var response = await http.PostAsJsonAsync(
"https://api.xgenerate.io/v2/documents/generate-from-html", request);
response.EnsureSuccessStatusCode();
// result.Filename is "summary.docx": the extension follows the requested format.
var result = await response.Content.ReadFromJsonAsync<GenerateResult>();
await File.WriteAllBytesAsync(result!.Filename, Convert.FromBase64String(result.Content));
record GenerateResult(string Filename, string Content, string Format);The response
The multipart call streams the DOCX back with Content-Type application/vnd.openxmlformats-officedocument.wordprocessingml.document. The JSON transport returns filename, content, format and warnings, with the file base64-encoded in content. Warnings are rendering notes, such as a font substitution or a watermark text truncated at 64 characters; the request succeeds either way.
What XGenerate does not do
HTML renders through the Word engine with a supported subset: headings, paragraphs, inline formatting, lists, tables, inline style attributes and style blocks, images as data URIs and paragraph-level CSS page breaks. Complex CSS layouts and JavaScript are not interpreted and external resources are never fetched; for a layout that must match an existing document, the Word template is the path.
Try it on your own template
Send us a Word file you use today. We render it with your data and return the PDF together with the adapted template, so your evaluation does not have to wait on us.
Request a demo