File size: 3,091 Bytes
623e14e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# Enhanced DOCX to PDF Converter API Documentation

## Overview
This is a professional FastAPI-based service for converting DOCX files to PDF with perfect formatting preservation, especially optimized for Arabic RTL text.

## Base URL
```
http://localhost:8000
```

## Endpoints

### 1. Health Check
**GET** `/health`

Check if the service is running.

**Response:**
```json
{
  "status": "healthy",
  "version": "2.0.0"
}
```

### 2. Convert DOCX to PDF
**POST** `/convert`

Convert a single DOCX file to PDF. Supports two input methods:

#### Method 1: Multipart File Upload
**Form Parameters:**
- `file` (required): The DOCX file to convert

#### Method 2: Base64 Encoded Content
**Form Parameters:**
- `file_content` (required): Base64 encoded DOCX file content
- `filename` (required): Original filename with .docx extension

**Response:**
```json
{
  "success": true,
  "pdf_url": "/download/abc123/document.pdf",
  "message": "Conversion successful"
}
```

**Error Response:**
```json
{
  "success": false,
  "error": "Error description"
}
```

### 3. Batch Convert DOCX to PDF
**POST** `/convert/batch`

Convert multiple DOCX files to PDF in a single request.

**Request Body:**
```json
{
  "files": [
    {
      "file_content": "base64_encoded_content_1",
      "filename": "document1.docx"
    },
    {
      "file_content": "base64_encoded_content_2",
      "filename": "document2.docx"
    }
  ]
}
```

**Response:**
```json
[
  {
    "success": true,
    "pdf_url": "/download/abc123/document1.pdf",
    "message": "Conversion successful"
  },
  {
    "success": false,
    "error": "Error description"
  }
]
```

### 4. Download PDF
**GET** `/download/{temp_id}/{filename}`

Download a converted PDF file.

**Path Parameters:**
- `temp_id`: Temporary directory ID from conversion response
- `filename`: PDF filename from conversion response

## Error Handling

The API uses standard HTTP status codes:

- `200` - Success
- `400` - Bad Request (invalid input)
- `404` - Not Found (file not found)
- `413` - Payload Too Large (file too big)
- `500` - Internal Server Error (conversion failed)

## File Size Limits

- Maximum file size: 50MB
- Supported file type: DOCX only

## CORS Support

The API includes full CORS support for direct browser integration.

## Example Usage

### Using cURL (File Upload)
```bash
curl -X POST "http://localhost:8000/convert" \
  -H "accept: application/json" \
  -H "Content-Type: multipart/form-data" \
  -F "file=@document.docx"
```

### Using cURL (Base64)
```bash
curl -X POST "http://localhost:8000/convert" \
  -H "accept: application/json" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "file_content=$(base64 document.docx)" \
  -d "filename=document.docx"
```

### Using JavaScript (Fetch API)
```javascript
const formData = new FormData();
formData.append('file', fileInput.files[0]);

fetch('http://localhost:8000/convert', {
  method: 'POST',
  body: formData
})
.then(response => response.json())
.then(data => {
  if (data.success) {
    window.open('http://localhost:8000' + data.pdf_url, '_blank');
  }
});
```