Generate Documents (Multiple) API
What this endpoint does: The PDF4me Generate Documents (Multiple) REST API produces a batch of personalised documents from a single template in one API call. Send a DOCX, HTML, or PDF template with mustache-style placeholders alongside a JSON or XML data file containing one record per output document. The API processes every record, merges it with the template, and returns an array of fully populated documents in your chosen output format — PDF, Word, Excel, or HTML. One call can generate invoices, certificates, contracts, labels, or any mail-merge-style document at scale.
Authenticating Your API Request
Every PDF4me REST API call must include your API key in the Authorization header. Obtain your key from the developer dashboard.
Important Facts You Should Not Miss
templateFileType, templateFileName, templateFileData, documentDataType, documentDataFile, and outputType are all mandatory. Omitting any one causes a 400 error. You can substitute documentDataText for documentDataFile when passing data inline as a string, but at least one data source must be provided.
The number of generated documents equals the number of data records in your JSON or XML file. A data file with 500 customer objects produces 500 separate output documents. For very large batches (1 000+ records), split the data file into smaller chunks and make multiple API calls to avoid request timeouts and improve reliability.
{{customerName}} in the template is only replaced if your JSON record contains a key named exactly "customerName" — case-sensitive. A mismatch leaves the placeholder unreplaced in the output. Use consistent field naming conventions across your template design and data preparation. Test with a single-record data file first before running large batches.
REST API Endpoint
| Method | Endpoint |
|---|---|
| POST | /api/v2/GenerateDocuments |
Base URL: https://api.pdf4me.com
Parameters
⚠️ templateFileType, templateFileName, templateFileData, documentDataType, documentDataFile, and outputType are all required. documentDataText is an optional alternative to documentDataFile.
| Parameter | Required | Type | What it does | Example |
|---|---|---|---|---|
| templateFileType | Required | Enum | Format of the template file. Accepted values: Word (DOCX) · HTML · PDF | Word |
| templateFileName | Required | String | Filename of the template including extension. Used for processing identification. Must reflect the actual format specified in templateFileType. | invoice-template.docx |
| templateFileData | Required | Base64 | The complete template file encoded as Base64. The template should contain mustache-style {{placeholders}} matching your data field names. | JVBERi0xLjQK... |
| documentDataType | Required | Enum | Data format of the source data file. Accepted values: JSON · XML. JSON is recommended for most integrations. | JSON |
| documentDataFile | Required | Base64 | The data file containing the records to merge — one record per output document — encoded as Base64. Each record's fields must match the placeholder names in the template. | W3sibmFtZSI6... |
| outputType | Required | Enum | Output document format for the generated files. Accepted values: PDF · DOCX · XLSX · HTML. Independent of the template format — a DOCX template can output PDF. | |
| documentDataText | Optional | String | Inline data as a JSON or XML string — an alternative to documentDataFile when you want to pass data directly without Base64-encoding a file. Used instead of documentDataFile, not in addition to it. | [{"name":"John","total":"$120.00"}] |
| metaDataJson | Optional | String | Optional metadata JSON string for document classification, versioning, or processing tracking. Does not affect document content. | {"version":"1.0","batch":"may2026"} |
Output Fields
| Field | Type | Description |
|---|---|---|
| Output Documents | Array | Array of generated document objects — one per data record. Each object contains the document content (Base64) and filename. |
Request Example
Headers
Content-Type: application/json
Authorization: YOUR_API_KEY
JSON Payload
{
"templateFileType": "Docx",
"templateFileName": "invoice-template.docx",
"templateFileData": "JVBERi0xLjQK...",
"documentDataType": "Json",
"documentDataFile": "W3sibmFtZSI6...",
"outputType": "PDF",
"documentDataText": "[{\"customerName\":\"Acme Corp\",\"invoiceTotal\":\"$1,200.00\"}]",
"metaDataJson": "{\"version\":\"1.0\",\"batch\":\"may2026\"}"
}
Response
{
"Output Documents": [
{
"docContent": "BASE64_PDF_CONTENT_DOCUMENT_1",
"docName": "invoice-001.pdf"
},
{
"docContent": "BASE64_PDF_CONTENT_DOCUMENT_2",
"docName": "invoice-002.pdf"
}
]
}
Code Samples
Common Use Cases
Pull all invoice records for the billing period from your database, build a JSON array with one object per invoice (customerName, items, total, dueDate), and generate personalised PDF invoices in a single API call. Output 500 invoices in under a minute instead of mail-merging them manually.
Generate personalised course completion certificates from a DOCX template. Supply a JSON array with recipient names, course titles, and completion dates — the API produces one signed, formatted PDF certificate per recipient ready for email delivery or digital archiving.
Pre-populate NDA or service agreement templates with each client's name, address, effective date, and contract value. Generate a batch of DOCX contracts (so legal can review and edit) or final PDF contracts (for e-signature) in one call, replacing the manual mail-merge step entirely.
Frequently Asked Questions
▶ What template formats does the Generate Documents (Multiple) API support?
The API supports DOCX (Word), HTML, and PDF as template formats via the templateFileType parameter. DOCX with mustache-style merge fields gives the richest formatting control for most document types and is the recommended format for invoices, certificates, and contracts.
▶ How many documents can I generate in a single API call?
The number of output documents equals the number of records in your data file. There is no fixed hard limit per call, but for batches larger than 500 records, split your data file into smaller chunks and make multiple calls. This avoids request timeouts and keeps each call's response size manageable.
▶ What data formats does the API accept?
JSON and XML via the documentDataType parameter. JSON is recommended for most integrations as it is easier to generate dynamically from databases or APIs. You can also pass a data string inline via documentDataText instead of Base64-encoding a separate file.
▶ What output formats can the API generate?
Set outputType to PDF, DOCX, XLSX, or HTML. PDF is the most common output for distribution and archiving. DOCX is useful when recipients need to edit the document. The output format is completely independent of the template format — a DOCX template can produce PDF output.
▶ How do mustache placeholders work in the template?
Add {{fieldName}} placeholders in your DOCX or HTML template. The field name inside the braces must exactly match (case-sensitive) the key in your JSON data record. For sections and loops (e.g. invoice line items), use {{#items}}...{{/items}} block syntax. Test with a single-record data file first to verify all placeholders resolve correctly before running large batches.