OCR via API
import axios from "axios";
import FormData from "form-data";
import { readFileSync } from "fs";
const data = new FormData();
data.append('image', readFileSync('path/to/image.png'), {
filename: 'image.png',
contentType: 'image/png'
});
const options = {
method: 'POST',
url: 'https://text-to-anything.p.rapidapi.com/ocr',
params: {
language: 'eng',
mode: 'text'
},
headers: {
'x-rapidapi-key': '{Your-RapidAPI-Key}',
...data.getHeaders(),
},
data: data
};
const response = await axios.request(options);
console.log(response.data);
The body of the request should be a multipart/form-data object with as "image" the image or pdf that you want to extract text from. The API will return the text in the specified format.
Query parameters:
Option | Type | Description |
---|---|---|
language | string | The language of text, for example "eng" for English. |
mode | string | Can be "text", "hocr" or "blocks". The default is "text". |
Output
Depending on the mode you choose, the output will be different:
- Text - will return plain text
- HOCR - will return a (HTML like structure)
- Blocks - will return a detailed JSON object