How to Convert a PDF File to Text with OCR using Go

Cloudmersive
2 min readNov 25, 2022

--

While vector PDF files can be converted to text directly by accessing the document’s compressed text contents, rasterized (image-based) PDF files require an Optical Character Recognition (OCR) service to make the same conversion. To make this conversion for free, you can easily take advantage of our PDF to Text OCR API using the complementary code examples provided at the bottom of this article in Go. All you’ll need in addition to these examples is a valid Cloudmersive API key, which you can get for free by registering a free account on our website (this will yield a limit of 800 API calls per month).

Before calling the below function, it’s important to note several optional features which can be configured to customize your conversion. These features include:

  1. Basic, Normal or Advanced recognition mode: these provide increasing fault tolerance for the OCR operation and use 1–2, 26–30 & 28–30 API calls respectively. The Advanced option is enabled by default.
  2. Language selection: you can select which language the API should recognize by including your language’s three-letter abbreviation in the “language” field as a string. Dozens of common languages are supported, including English, German, French, Danish, and much more.
  3. Preprocessing mode: by default, this API will automatically enhance the quality of the input image, but you can turn this off by entering ‘none’ as string into the preprocessing field.

To structure your API call, simply copy & paste the below code examples into your file. Once you parse your file path & API key into their respective fields, you’re ready to make your conversion:

package main

import (
"fmt"
"bytes"
"mime/multipart"
"os"
"path/filepath"
"io"
"net/http"
"io/ioutil"
)

func main() {

url := "https://api.cloudmersive.com/ocr/pdf/toText"
method := "POST"

payload := &bytes.Buffer{}
writer := multipart.NewWriter(payload)
file, errFile1 := os.Open("/path/to/file")
defer file.Close()
part1,
errFile1 := writer.CreateFormFile("imageFile",filepath.Base("/path/to/file"))
_, errFile1 = io.Copy(part1, file)
if errFile1 != nil {
fmt.Println(errFile1)
return
}
err := writer.Close()
if err != nil {
fmt.Println(err)
return
}


client := &http.Client {
}
req, err := http.NewRequest(method, url, payload)

if err != nil {
fmt.Println(err)
return
}
req.Header.Add("recognitionMode", "<string>")
req.Header.Add("language", "<string>")
req.Header.Add("preprocessing", "<string>")
req.Header.Add("Content-Type", "multipart/form-data")
req.Header.Add("Apikey", "YOUR-API-KEY-HERE")

req.Header.Set("Content-Type", writer.FormDataContentType())
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()

body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet