Document processing - Image to Word API

Updated at:

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 Method

POST

Endpoint

https://cloud-iqs.aliyuncs.com/document/process

Request Format

Request header: Content-Type: application/json

Authentication

Request header: X-API-Key

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

engineType

string

Yes

None

Must be ScanDocConvertWord. The value is case-sensitive.

ScanDocConvertWord

dataUrl

string

Either dataUrl or dataBase64 is required.

None

The URL of a downloadable image. We recommend using a public HTTPS URL. You can leave this parameter empty if you use dataBase64.

See request example.

dataBase64

string

Either dataUrl or dataBase64 is required.

None

The standard Base64 encoding of a single image, without the data: prefix. The maximum size of the original image is 10 MiB (10,485,760 bytes).

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

requestId

string

Always

The request ID. Provide this ID when contacting technical support.

searchInformation

object

Always

Timing information for the document processing task.

searchInformation.searchTime

int64

Always

The document processing time, in milliseconds.

functionOption

string

Always

Fixed value: word. This value is case-sensitive and is different from the engineType request parameter.

fileBase64

string

Always

The Base64-encoded content of the converted Word file.

fileName

string

Always

Fixed value: result.docx.

mimeType

string

Always

Fixed value: application/vnd.openxmlformats-officedocument.wordprocessingml.document.

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

400

MissingParameter

An image source or a required parameter is missing.

Check the engineType parameter and provide a non-empty image source.

400

InvalidParameter

A parameter is invalid or the image is too large.

Verify the case of ScanDocConvertWord, image source mutual exclusion rules, Base64 padding, and original image size. Do not pass inputConfig or other additional parameters.

403

Retrieval.InvalidAPIKey

The API key is invalid.

Check the X-API-Key request header and the credential.

403

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.

415

As per response

The request format is not supported. The response body might be empty.

Set the request header Content-Type: application/json.

429

Retrieval.Throttling.scan (common)

Too many requests.

Reduce the call frequency and implement a backoff and retry strategy.

502

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.

503

As per response

The service is temporarily unavailable.

Try again later. If the issue persists, contact technical support.

500

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 the engineType parameter has an incorrect case (for example, if ScanDocConvertWord is written as scandocconvertword) 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.

Related documents