Skip to content
ComPDF

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.pdf into a Word file synchronously and download the result.

Flow

1Prepare PDFLocal file: sample.pdf2Get Public KeyDashboard → API Keys3Build RequestPOST · multipart/form-datax-api-key header4Parse ResponsedownloadUrl returned directly5Download ResultSave sample.docx

Request Reference

ItemValue
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 Headerx-api-key: YOUR_PUBLIC_KEY
MethodPOST
Content-Typemultipart/form-data
ModeSynchronous

Parameters

NameTypeRequiredDescription
fileFileYesThe source PDF to convert. For PDF→Word, upload one PDF file in multipart/form-data.
pageRangesStringNoPage range string such as 1-3,6. Omit it to convert the entire document.
enableOcrInteger (0 / 1)NoEnable OCR for scanned or image-based PDFs. Use 1 when text is not selectable.
ocrRecognitionLangStringNoOCR language code, for example eng. Useful when enableOcr=1.
enableAiLayoutInteger (0 / 1)NoEnable AI layout analysis for better layout reconstruction. Default is usually 1.
pageLayoutModeStringNoWord 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:

FieldDescription
codeBusiness code. "200" means success; otherwise see the error code page.
data.taskIdTask ID, used for later queries or closing the task.
data.statusTask status. TaskFinish means the task is complete.
data.downloadUrlResult download URL, time-limited — fetch it promptly.
data.expireTimeWhen 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" → check msg and the error code page.
  • Auth failures usually surface as 401 / 403: verify x-api-key, that it's enabled, and that the region matches (compdf.com vs compdf.cn).
  • Oversized files (default cap 50 MB) return 413.

Next Steps