Complete Example
This page walks through the shortest, runnable PDF → Word synchronous request — from copying your Public Key to downloading the result via downloadUrl.
Goal: convert a local
sample.pdfinto a Word file synchronously and download the result.
Flow
Request Reference
| Item | Value |
|---|---|
| Endpoint (Global) | https://api-server.compdf.com/server/v2/process/pdf/docx |
| Endpoint (China Mainland) | https://api-server.compdf.cn/server/v2/process/pdf/docx |
| Auth Header | x-api-key: YOUR_PUBLIC_KEY |
| Method | POST |
| Content-Type | multipart/form-data |
| Mode | Synchronous |
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
file | File | Yes | The source PDF to convert. For PDF→Word, upload one PDF file in multipart/form-data. |
pageRanges | String | No | Page range string such as 1-3,6. Omit it to convert the entire document. |
enableOcr | Integer (0 / 1) | No | Enable OCR for scanned or image-based PDFs. Use 1 when text is not selectable. |
ocrRecognitionLang | String | No | OCR language code, for example eng. Useful when enableOcr=1. |
enableAiLayout | Integer (0 / 1) | No | Enable AI layout analysis for better layout reconstruction. Default is usually 1. |
pageLayoutMode | String | No | Word layout mode: e_Flow for flow layout or e_Box for box-based layout. |
Request Examples
bash
curl -X POST "https://api-server.compdf.com/server/v2/process/pdf/docx" \
-H "x-api-key: YOUR_PUBLIC_KEY" \
-F "file=@./sample.pdf" \
-F "pageRanges=1-3" \
-F "enableOcr=1" \
-F "ocrRecognitionLang=eng" \
-F "enableAiLayout=1" \
-F "pageLayoutMode=e_Flow"python
import requests
url = "https://api-server.compdf.com/server/v2/process/pdf/docx"
headers = {"x-api-key": "YOUR_PUBLIC_KEY"}
files = {"file": open("sample.pdf", "rb")}
data = {
"pageRanges": "1-3",
"enableOcr": "1",
"ocrRecognitionLang": "eng",
"enableAiLayout": "1",
"pageLayoutMode": "e_Flow"
}
resp = requests.post(url, headers=headers, files=files, data=data, timeout=120)
result = resp.json()
print(result)
if result.get("code") == "200" and result.get("data", {}).get("downloadUrl"):
download = requests.get(result["data"]["downloadUrl"], timeout=120)
open("sample.docx", "wb").write(download.content)java
OkHttpClient client = new OkHttpClient();
RequestBody body = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", "sample.pdf",
RequestBody.create(new File("sample.pdf"),
MediaType.parse("application/pdf")))
.addFormDataPart("pageRanges", "1-3")
.addFormDataPart("enableOcr", "1")
.addFormDataPart("ocrRecognitionLang", "eng")
.addFormDataPart("enableAiLayout", "1")
.addFormDataPart("pageLayoutMode", "e_Flow")
.build();
Request request = new Request.Builder()
.url("https://api-server.compdf.com/server/v2/process/pdf/docx")
.addHeader("x-api-key", "YOUR_PUBLIC_KEY")
.post(body)
.build();
try (Response response = client.newCall(request).execute()) {
System.out.println(response.body().string());
}javascript
import fs from 'node:fs'
import FormData from 'form-data'
import axios from 'axios'
const form = new FormData()
form.append('file', fs.createReadStream('./sample.pdf'))
form.append('pageRanges', '1-3')
form.append('enableOcr', '1')
form.append('ocrRecognitionLang', 'eng')
form.append('enableAiLayout', '1')
form.append('pageLayoutMode', 'e_Flow')
const { data } = await axios.post(
'https://api-server.compdf.com/server/v2/process/pdf/docx',
form,
{ headers: { 'x-api-key': 'YOUR_PUBLIC_KEY', ...form.getHeaders() }, timeout: 120_000 }
)
console.log(data)Successful Response
json
{
"code": "200",
"msg": "success",
"data": {
"taskId": "9f1b3a2e8c0d4f7e9a1b2c3d4e5f6789",
"status": "TaskFinish",
"fileName": "sample.pdf",
"outputFileName": "sample.docx",
"downloadUrl": "https://download.compdf.com/xxxx/sample.docx",
"expireTime": "2026-05-09T08:00:00Z"
}
}Field reference:
| Field | Description |
|---|---|
code | Business code. "200" means success; otherwise see the error code page. |
data.taskId | Task ID, used for later queries or closing the task. |
data.status | Task status. TaskFinish means the task is complete. |
data.downloadUrl | Result download URL, time-limited — fetch it promptly. |
data.expireTime | When the download URL expires (UTC). |
Important Notes
HTTP 200 does NOT mean business success
A 200 HTTP status only confirms the request was delivered. The actual outcome is in the code field of the JSON body.
code != "200"→ checkmsgand the error code page.- Auth failures usually surface as
401 / 403: verifyx-api-key, that it's enabled, and that the region matches (compdf.comvscompdf.cn). - Oversized files (default cap 50 MB) return
413.
Next Steps
- Want a different output format? See the API Tool List.
- Large files / batch jobs: switch to asynchronous or pre-signed modes in Request Modes.
- Get notified when a task finishes: see Webhook Events.