Convert a Photo of a Document into Text using JavaScript

Cloudmersive
2 min readMay 18, 2022

These days it’s common for people to upload important physical documents to the internet (including tax forms, medical records, etc.) by taking a photo of the document, rather than by scanning it in a traditional scanner. If your website is accumulating that type of content from your users, you’ve probably noticed that people don’t always take perfect images, which makes the process of using OCR to pull text from that document rather difficult. Our Photo to Text OCR API is designed specifically to handle such photographed documents, and with its help your application will be able to work around various imperfections in the shot.

To structure your API call with JavaScript, start by installing the jQuery Library. Let’s run the below command:

bower install jquery

After that, let’s include the code snippets to call the OCR API:

var form = new FormData();
form.append("imageFile", fileInput.files[0], "file");
var settings = {
"url": "https://api.cloudmersive.com/ocr/photo/toText",
"method": "POST",
"timeout": 0,
"headers": {
"recognitionMode": "<string>",
"language": "<string>",
"Content-Type": "multipart/form-data",
"Apikey": "YOUR-API-KEY-HERE"
},
"processData": false,
"mimeType": "multipart/form-data",
"contentType": false,
"data": form
};
$.ajax(settings).done(function (response) {
console.log(response);
});

And that’s it — no further code required.

Make sure to include your API key where indicated in the documentation — if you don’t have one currently, register for a free account on our website (Home — Cloudmersive APIs) and you’ll receive one shortly thereafter.

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.