How to Generate DOCX from JSON in Node.js Without LibreOffice
For Node.js and TypeScript developers, generating documents programmatically can be a frustrating infrastructure challenge. When an application needs to generate customer agreements, invoices, or analytics reports from JSON payloads, engineers usually start by deploying headless LibreOffice (via libreoffice-convert, unoconv, or soffice) inside a Docker container.
Within weeks, operational problems appear: containers grow to over 2GB, LibreOffice processes crash intermittently under concurrent load, and cold starts severely increase API response times.
This guide shows you how to generate DOCX and PDF documents from JSON in Node.js without LibreOffice, using TRYDOKU's high-throughput cloud API.
Why Headless LibreOffice Fails in Production
Running headless LibreOffice in production environments introduces four main architectural bottlenecks:
- Large Container Images: A minimal Docker image with LibreOffice, fonts, and Java dependencies routinely exceeds 1.5GB to 2.5GB in size.
- Unstable Processes & Unreleased Memory: LibreOffice was designed as a desktop GUI application, not a multi-threaded server daemon. Under sustained concurrent requests, worker processes frequently hang, deadlock, or fail to release system memory.
- Severe Cold Starts: In serverless architectures (AWS Lambda, Google Cloud Run, Vercel), booting a container with LibreOffice takes 5 to 15 seconds, making real-time user document generation impossible.
- Font & Rendering Differences: LibreOffice's rendering engine does not reproduce Microsoft Word layouts with 100% fidelity, leading to unexpected table clipping, font substitutions, and margin shifts.
Moving document processing to TRYDOKU's serverless rendering cluster keeps your Node.js application lightweight, fast, and completely stateless.
Architecture Comparison
| Metric | Headless LibreOffice in Docker | TRYDOKU REST API |
|---|---|---|
| Node.js Memory Footprint | ~500MB–2GB per container | < 30MB (Pure HTTP client) |
| Cold Start Latency | 5,000–12,000 ms | Sub-second HTTP response |
| Concurrency Scaling | Complex process pool management | Auto-scaling serverless cloud |
| Template Editor | Word or LibreOffice | Standard Microsoft Word (.docx) |
| Data Residency | Self-managed | Frankfurt, Germany (EU GDPR) |
Step-by-Step Implementation in Node.js
TRYDOKU lets you maintain Microsoft Word templates with standard placeholders (e.g., {{customer_name}}, {{amount}}, and loop blocks {> items }} ... {< items }}) and fill them through a JSON REST API.
To inspect existing CSV datasets as structured JSON before sending them, use our free browser-based CSV to JSON converter.
Step 1: Install Dependencies
In your Node.js project, install your preferred HTTP client:
npm install axios
Step 2: Implement the Generation Function
Create a service module that sends your JSON payload to TRYDOKU:
import axios from 'axios';
import fs from 'fs';
interface InvoiceData {
invoice_number: string;
issue_date: string;
customer_name: string;
total_amount: string;
items: Array<{
description: string;
quantity: number;
price: string;
}>;
}
async function generateInvoiceDocx(payload: InvoiceData): Promise<void> {
const apiKey = process.env.TRYDOKU_API_KEY;
const response = await axios.post(
'https://api.trydoku.com/v1/generate',
{
template_id: 'tpl_invoice_standard_v1',
output_format: 'docx', // Or 'pdf'
data: [payload]
},
{
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json',
'Accept': 'application/zip'
},
responseType: 'arraybuffer'
}
);
fs.writeFileSync('generated_invoice.docx', response.data);
console.log('Document generated successfully without LibreOffice!');
}
Detailed API reference documentation, authentication details, and webhook event payloads are available in our official API Documentation.
Step 3: Configure Webhooks for Asynchronous Batches
For high-volume batch jobs (e.g., generating 1,000 invoices at month-end), you can pass a webhook_url in your API request. TRYDOKU processes the batch asynchronously in the background and calls your webhook with secure download URLs when the batch archive is ready, leaving your Node.js worker event loops fully available.
To view credit pricing and volume packages, check our pricing page.
Frequently Asked Questions
Can I generate both DOCX and PDF formats from the same JSON payload?
Yes. The API supports output_format: "both", returning a ZIP archive containing both editable Word documents and vector PDFs.
Can I pass raw base64 template files on the fly?
Yes. Instead of using a stored template_id, you can supply a template_base64 string directly in your API request body.
Is TRYDOKU GDPR compliant?
Yes. TRYDOKU operates in Frankfurt, Germany (EU). All API traffic is encrypted via TLS 1.3, processed in temporary memory, and automatically deleted from temporary storage within 24 to 72 hours.