Extract text from a single image
This guide explains how to use UniPDF's OCR capabilities to extract text from a single image.
This guide demonstrates how to extract text from a single image file using UniPDF’s OCR feature. The OCR process is performed by an external OCR service that UniPDF communicates with via HTTP. For this guide, we will use the open-source UniDoc OCR server.
Sample Input

Before you begin
You should get your API key from your UniCloud account.
If this is your first time using UniPDF SDK, follow this guide to set up a local development environment.
You also need to have the UniDoc OCR server running.
First, clone the ocrserver repository:
git clone https://github.com/unidoc/ocrserver.git
Then, navigate to the ocrserver directory and run the server using Docker Compose:
cd ocrserver
docker-compose up
Clone the project repository
In your terminal, clone the examples repository. It contains the Go code we will be using for this guide.
git clone https://github.com/unidoc/unipdf-examples.git
Navigate to the ocr folder in the unipdf-examples directory.
cd unipdf-examples/ocr
How it works
The example ocr_sample.go shows how to extract text from a single image.
The init function in lines 23-29 loads the metered license key before running the program.
The main function starts by checking if the input file path is provided as a command-line argument.
In lines 41-54, the OCROptions struct is configured to connect to the OCR server. The Url is set to http://localhost:8080/file, which is the endpoint of the local OCR server.
An OCRClient is created using ocr.NewOCRHTTPClient with the specified options.
The client.ProcessFile method is called to process the image file. This method sends the image to the OCR server and returns the extracted text.
Finally, the extracted text is printed to the console.
Run the code
Run the code with an image file as input:
go run ocr_sample.go /path/to/your/image.png
Sample Output
Extracted text: {
"result": "Secure by\n\n°\ndesign\nEvery release of our libraries is automatical-\nly tested against known vulnerabilities and\ndo not pass unless everything is remediat-\ned. All changes are carefully reviewed by\nour team.",
"version": "0.2.0"
}