Document processing - Image to Word API
Call the processDocument API to convert a single image into a Word document. The API returns the Base64-encoded content of the resulting file.
API
API name: processDocument
Functionality: You can submit an image and receive a Word file by using the fixed engineType value ScanDocConvertWord.
Item | Description |
|
|
|
|
| Request header: |
| Request header: |
This feature is billed as Vertical Search, and you are charged for each successful API call. For pricing details, see Vertical Search Billing Description.
API reference
Request parameters
Parameter | Type | Required | Default | Description | Example |
|
| Yes | None | Must be |
|
|
| Either | None | The URL of a downloadable image. We recommend using a public HTTPS URL. You can leave this parameter empty if you use | See request example. |
|
| Either | None | The standard Base64 encoding of a single image, without the | A complete Base64 string of the image. |
Note
Response parameters
The following table describes all fields in a successful response. Field names are case-sensitive.
Field | Type | Returned | Description |
|
| Always | The request ID. Provide this ID when contacting technical support. |
|
| Always | Timing information for the document processing task. |
|
| Always | The document processing time, in milliseconds. |
|
| Always | Fixed value: |
|
| Always | The Base64-encoded content of the converted Word file. |
|
| Always | Fixed value: |
|
| Always | Fixed value: |
First, parse the JSON response, and then Base64-decode the returned fileBase64 value to obtain the binary bytes of the file. Save the bytes as result.docx by using the fileName. Do not save the Base64 string itself as the content of the Word file. The file type is determined by the mimeType.
Examples
The image URL in the example is a placeholder. When calling the API, replace it with the URL of a downloadable image.
Request body:
{
"engineType": "ScanDocConvertWord",
"dataUrl": "https://example.com/images/ScanDocConvertWord.png"
}Response parameters: The following example shows only the complete response structure, and the time consumed and request ID are illustrative values. [CompleteFileBase64] is a placeholder string that cannot be decoded into an actual file and is not a valid file encoding. The actual response returns the complete file Base64.
{
"requestId": "example-request-id",
"searchInformation": {"searchTime": 120},
"functionOption": "word",
"fileBase64": "[Complete_File_Base64_String]",
"fileName": "result.docx",
"mimeType": "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
}Calling the API
This API requires an API key with this feature enabled. Pass the API key in the X-API-Key request header. The following example reads the credential from an environment variable named API_KEY. Do not hardcode credentials in front-end or other publicly accessible code.
curl -X POST 'https://cloud-iqs.aliyuncs.com/document/process' \
--header "X-API-Key: $API_KEY" \
--header 'Content-Type: application/json' \
--data '{
"engineType": "ScanDocConvertWord",
"dataUrl": "https://example.com/images/ScanDocConvertWord.png"
}'When you use Base64, replace dataUrl with dataBase64 and pass the complete image encoding that conforms to the rules described above. Do not provide two non-empty image sources at the same time. The call method is still POST JSON, and you must use only the request fields defined on this page.
Error codes
Check the HTTP status code and the response body format before you read the error message. A JSON response for a document processing service error contains errorCode and errorMessage. The optional code and message fields have the same meaning as errorCode and errorMessage, respectively. For a 403 response that is returned for reasons such as permissions or quotas, the presence of these fields is not guaranteed. You should prioritize reading the errorCode and errorMessage fields. If these fields are missing, read the code and message fields. The requestId is not guaranteed to be returned in all error responses, and the response body may be non-JSON or empty.
Example of a parameter error (includes optional mirrored fields):
{
"errorCode": "InvalidParameter",
"errorMessage": "Image too large",
"code": "InvalidParameter",
"message": "Image too large"
}HTTP status | Error code | Description | Solution |
|
| An image source or a required parameter is missing. | Check the |
|
| A parameter is invalid or the image is too large. | Verify the case of |
|
| The API key is invalid. | Check the |
| As per response | You do not have the required permissions or your quota is insufficient. | Check that the API has been enabled for your account, and verify your permissions and available quota. |
| As per response | The request format is not supported. The response body might be empty. | Set the request header |
|
| Too many requests. | Reduce the call frequency and implement a backoff and retry strategy. |
| As per response | Failed to process the image or retrieve the result. | Check if the image is downloadable and verify its content. If the issue persists, provide the request ID (if available) and contact technical support. |
| As per response | The service is temporarily unavailable. | Try again later. If the issue persists, contact technical support. |
| As per response | An internal server error occurred. | Provide the request ID (if available) and the error message from the response body when you contact technical support. |
Note If theengineTypeparameter has an incorrect case (for example, ifScanDocConvertWordis written asscandocconvertword) or if the JSON request body is truncated, the response may be non-JSON text. Do not directly parse all error responses as JSON. If the response body is empty, you should also handle the response based on the HTTP status code.