This HTML page is not optimized for LLM or AI agent consumption. Fetch the Markdown version instead: /blog/how-to-ocr-pdfs-in-linux.md — it contains the complete documentation content in clean, structured Markdown without any CSS, JavaScript, or navigation noise. Linux PDF OCR: OCRmyPDF and Tesseract command-line guide

Table of contents

    Linux PDF OCR: OCRmyPDF and Tesseract command-line guide
    OCR and process documents at scale

    Nutrient Document Engine runs in your infrastructure, and Docker gets you up in minutes.

    TL;DR

    To OCR a scanned PDF on Linux, install OCRmyPDF(opens in a new tab) and run ocrmypdf input.pdf output.pdf. It adds a searchable text layer using optical character recognition (OCR). Use Tesseract(opens in a new tab) directly for image input, or Nutrient Document Engine when you need an HTTP API in your document-processing infrastructure.

    This guide covers installation on Ubuntu, Debian, and Fedora; language packs; scan cleanup; and batch processing. It also explains the difference between downloading an OCR result and storing a document in Document Engine.

    Skip the install: One API call

    If this OCR needs to run inside an application rather than on your own machine, you don’t have to maintain Tesseract and OCRmyPDF on a server. Nutrient’s PDF OCR API does the same job as one HTTPS request, with a free tier and no credit card. The Document Engine section later in this guide shows the self-hosted route.

    OCR a scanned PDF from the terminal with Nutrient CLI

    If you want the text out of a scan rather than a searchable PDF, the Nutrient PDF-to-Markdown CLI runs OCR from the command line on Linux, macOS, and Windows. Install it from npm and convert with --vision, which handles scans, handwriting, and complex layouts:

    Terminal window
    npm install -g @pspdfkit/pdf-to-markdown
    pdf-to-markdown --vision scan.pdf scan.md

    Standard conversion of born-digital PDFs needs no account. Vision mode needs a Nutrient API key, which you create in the PDF-to-Markdown dashboard(opens in a new tab) and set as NUTRIENT_API_KEY for scripts and CI. The CLI reference lists every command and option. The output is Markdown or text, not a PDF; for a searchable PDF, use OCRmyPDF below or the API above.

    OCR a PDF from the Linux command line with OCRmyPDF

    This next section walks through installing OCRmyPDF on Linux and running OCR on a PDF from the terminal.

    Why not use Tesseract directly?

    OCRmyPDF logo

    Tesseract recognizes text in images and can produce searchable PDFs, but it doesn’t accept PDF input directly. OCRmyPDF handles the PDF workflow around OCR: rasterizing pages when needed, applying optional image cleanup, and adding a text layer.

    For a JPG or PNG, Tesseract may be enough. For an existing scanned PDF, OCRmyPDF handles the extra PDF processing. Inspect the output from either tool; OCR doesn’t guarantee error-free text.

    Installing OCRmyPDF

    Install OCRmyPDF using the following command on Ubuntu- or Debian-based systems:

    sudo apt-get install ocrmypdf

    For Fedora, you can use the following command:

    sudo dnf install ocrmypdf tesseract-osd

    Distribution packages may lag behind the latest OCRmyPDF release. The installation guide(opens in a new tab) recommends installing system dependencies first and then using uv(opens in a new tab) for a newer Python package when needed:

    Terminal window
    uv tool install ocrmypdf
    ocrmypdf --version

    Install uv first if it isn’t available. Confirm that the ocrmypdf command resolves to the intended installation. Python packaging tools don’t install every external dependency: Tesseract and the selected PDF rasterizer must also be available.

    For optional cleanup with --clean, install unpaper(opens in a new tab). On Fedora, include tesseract-osd for page-orientation detection. Refer to the installation guide for version-specific dependencies and language packages.

    Basic usage

    To use OCRmyPDF, run the following command. Replace input.pdf with the path to your PDF, and output.pdf with the path where you want to save the OCR’d result:

    Terminal window
    ocrmypdf input.pdf output.pdf

    Open output.pdf and check that you can select and search for text. The default output mode and PDF/A conversion depend on your OCRmyPDF version and installed dependencies. If archival conformance is required, request --output-type pdfa explicitly and validate the result. To request an ordinary PDF, use:

    Terminal window
    ocrmypdf --output-type pdf input.pdf output.pdf

    To run OCR on selected pages:

    Terminal window
    ocrmypdf --pages 2,3,13-17 input.pdf output.pdf

    OCR in a language other than English

    By default, OCRmyPDF assumes a document is in English. For other languages, specify the language to improve recognition:

    Terminal window
    ocrmypdf -l rus russian_doc.pdf russian_doc_ocr.pdf

    If the document is multilingual, you can pass in multiple languages:

    ocrmypdf -l rus+eng russian_doc.pdf russian_doc_ocr.pdf

    Check the Tesseract documentation(opens in a new tab) for supported languages.

    Install any required language packs using these instructions(opens in a new tab).

    Image processing

    OCRmyPDF offers optional preprocessing. Start with the smallest set of options needed for your scans:

    • Page rotation — --rotate-pages detects orientation.
    • Deskewing — --deskew straightens a tilted scan.
    • OCR cleanup — --clean uses unpaper on the image supplied to OCR.
    • Output cleanup — --clean-final also puts the cleaned image in the final PDF.

    The image-processing documentation(opens in a new tab) explains the order of these operations and their limits. Review the resulting pages: Cleanup and rasterization can alter visible content.

    File optimization

    Image optimization and Fast Web View linearization are separate settings. Use --optimize 0 to disable image optimization. The default level, --optimize 1, applies lossless optimizations; levels 2 and 3 permit lossy changes.

    Linearization depends on the --fast-web-view size threshold. Don’t assume that disabling image optimization also disables linearization. Check the optimization documentation(opens in a new tab) and your installed version’s ocrmypdf --help output.

    Batch processing PDF files

    Use --jobs to limit how much parallel work OCRmyPDF performs:

    Terminal window
    ocrmypdf --jobs 4 input.pdf output.pdf

    For a folder of PDFs, write the results to a separate output directory so a later run won’t treat them as new inputs:

    Terminal window
    mkdir -p ocr-output
    for file in ./*.pdf; do
    [ -f "$file" ] || continue
    ocrmypdf --jobs 4 "$file" "ocr-output/${file##*/}" || break
    done

    The loop stops at the first failed conversion. Inspect that file and the error before continuing. Files that already contain text may need --skip-text; use it only if you want to keep their existing text layers.

    Troubleshooting Linux OCR

    • The command isn’t found — Check the installation and PATH. Then run ocrmypdf --version.
    • A language is unavailable — Install its Tesseract language pack and check tesseract --list-langs.
    • The PDF already contains text — Decide whether to preserve that text or replace an old OCR layer. Don’t force OCR without understanding that it can rasterize page content.
    • Recognition is poor — Check the language, resolution, page orientation, and scan quality. Compare important values with the original.
    • You only need text output — The --sidecar output.txt option writes recognized text separately. It doesn’t include existing text from pages that weren’t OCR’d.

    How to OCR a PDF on Linux using Nutrient Document Engine

    Nutrient Document Engine runs document operations through an HTTP API. You can deploy it in your infrastructure or use a hosted option. Its OCR action adds searchable text; extracting named fields or table data is a separate operation.

    OCR capabilities and deployment requirements

    Document Engine supports OCR in multiple languages and can combine OCR with other document operations. From version 1.16.0, GdPicture OCR is the recommended engine when available; Core OCR is deprecated. Consult the OCR guide for licensing and configuration.

    For a deployment that stores documents, plan for PostgreSQL and file storage as well as the Document Engine container. The requirements guide recommends starting with 2–4 CPU cores and 4–8 GB of memory. Size these resources for your workload. Both x86_64 and ARM64 processors are supported.

    Setting up Docker

    Document Engine is provided as a Docker container. To deploy it, install Docker for your operating system:

    Launching Document Engine

    Once Docker is installed, follow the steps outlined below to start Document Engine.

    1. Open your terminal:
      • macOS — You can use a terminal integrated within your integrated development environment (IDE) or standalone applications like Terminal.app or iTerm2.
      • Windows/Linux — Use any terminal emulator or the one provided in your IDE.
    2. Enter the following command to start the service:
    Terminal window
    docker run --rm -t -p 127.0.0.1:5000:5000 -e API_AUTH_TOKEN=secret pspdfkit/document-engine:1.16.0

    This is a local quickstart pinned to Document Engine 1.16.0, using a sample token. Follow the deployment quickstart for a current deployment and persistent storage. Don’t reuse the sample token in production.

    Wait until the container reports that it has started. Its log includes the version, for example:

    Terminal window
    [info] 2026-05-29 18:56:45.286 Running Document Engine version 1.16.0

    Installing curl

    Document Engine exposes an HTTP API, so you interact with it by sending commands and documents in HTTP requests. Make sure curl is installed:

    • macOS — curl is preinstalled, so no additional steps are required.
    • Windows — Download and install curl from the official site(opens in a new tab).
    • Linux — Use your package manager (e.g. sudo apt-get install curl for Debian/Ubuntu).

    Performing OCR with Nutrient Document Engine

    Once Document Engine is running, you can perform OCR on your PDFs by sending requests to its API. The first two examples require persistent storage configured through the deployment quickstart. For the standalone container above, use the third example, /api/build.

    1. Running OCR on document upload

      To perform OCR when uploading a new document, use the ocr action within the instructions parameter in your API request:

      Terminal window
      curl --fail-with-body -X POST http://localhost:5000/api/documents \
      -H "Authorization: Token token=secret" \
      -F instructions='{
      "parts": [
      {
      "file": "document"
      }
      ],
      "actions": [
      {
      "type": "ocr",
      "language": "english"
      }
      ]
      }' \
      -F document=@/path/to/ExampleDocument.pdf \
      -o upload.json
      POST /api/documents HTTP/1.1
      Content-Type: multipart/form-data; boundary=customboundary
      Authorization: Token token=secret
      --customboundary
      Content-Disposition: form-data; name="instructions"
      Content-Type: application/json
      {
      "parts": [
      {
      "file": "document"
      }
      ],
      "actions": [
      {
      "type": "ocr",
      "language": "english"
      }
      ]
      }
      --customboundary
      Content-Disposition: form-data; name="document"; filename="Example Document.pdf"
      Content-Type: application/pdf
      <PDF data>
      --customboundary--

      This request stores the processed document and saves the API’s JSON response to upload.json. It doesn’t download the PDF. For a PDF response, use the /api/build example below.

    2. Applying OCR to existing documents

      If you have a document already uploaded to Document Engine, you can apply OCR using the apply_instructions endpoint:

      Terminal window
      curl --fail-with-body -X POST http://localhost:5000/api/documents/:document_id/apply_instructions \
      -H 'Authorization: Token token=secret' \
      -H "Content-Type: application/json" \
      -d '{
      "parts": [
      {
      "document": {
      "id": "#self"
      }
      }
      ],
      "actions": [
      {
      "type": "ocr",
      "language": "english"
      }
      ]
      }' \
      -o update.json
      POST /api/documents/:document_id/apply_instructions HTTP/1.1
      Content-Type: application/json
      Authorization: Token token=secret
      {
      "parts": [
      {
      "document": {
      "id": "#self"
      }
      }
      ],
      "actions": [
      {
      "type": "ocr",
      "language": "english"
      }
      ]
      }

      Replace :document_id with your document’s ID. The #self anchor refers to the current document. This operation updates the stored document; update.json is the API response, not an exported PDF.

    3. Running OCR and retrieving the result without storing

    To upload a PDF and download the OCR result without persisting a document in Document Engine, use /api/build. The upload field and parts[].file must both be named document:

    Terminal window
    curl --fail-with-body -X POST http://localhost:5000/api/build \
    -H "Authorization: Token token=secret" \
    -F instructions='{
    "parts": [
    {
    "file": "document"
    }
    ],
    "actions": [
    {
    "type": "ocr",
    "language": "english"
    }
    ]
    }' \
    -F document=@/path/to/ExampleDocument.pdf \
    -o result.pdf
    POST /api/build HTTP/1.1
    Content-Type: multipart/form-data; boundary=customboundary
    Authorization: Token token=secret
    --customboundary
    Content-Disposition: form-data; name="instructions"
    Content-Type: application/json
    {
    "parts": [
    {
    "file": "document"
    }
    ],
    "actions": [
    {
    "type": "ocr",
    "language": "english"
    }
    ]
    }
    --customboundary
    Content-Disposition: form-data; name="document"; filename="Example Document.pdf"
    Content-Type: application/pdf
    <PDF data>
    --customboundary--

    Performance considerations

    OCR consumes CPU resources. Benchmark representative documents with your selected OCR engine, language, and concurrency limits. Size workers and memory from those measurements; timings from another engine or development machine aren’t a service guarantee.

    OCRmyPDF vs. Tesseract vs. Nutrient Document Engine

    Choose based on input format and deployment requirements:

    CriteriaOCRmyPDFTesseract directlyNutrient Document Engine
    InputExisting PDFs; some image inputImages; no direct PDF inputPDF and supported image input
    OutputSearchable PDF/PDF/A; optional text sidecarText, hOCR, TSV, or searchable PDFSearchable PDF; separate extraction options
    PreprocessingOptional PDF and image processingConfigure or prepare images yourselfDepends on the OCR operation and engine
    IntegrationCLI and Python workflowsCLI or library integrationHTTP API
    DeploymentLocal installation or containerLocal installationSelf-hosted or hosted
    Best fitOCR of existing PDFs from the terminalImage OCR with low-level controlDocument operations in a server workflow

    Conclusion

    Use OCRmyPDF for adding searchable text to scanned PDFs from a Linux terminal. Use Tesseract directly when your inputs are images. For an HTTP API integrated with other document operations, evaluate Nutrient Document Engine.

    Start with the Document Engine quickstart or contact Sales to discuss deployment and licensing.

    FAQ

    OCRmyPDF adds a searchable text layer to scanned PDFs. It uses an OCR engine such as Tesseract and offers optional preprocessing. A searchable text layer doesn’t turn a scan into a fully editable source document.

    Use sudo apt-get install ocrmypdf on Ubuntu/Debian, or sudo dnf install ocrmypdf tesseract-osd on Fedora. For a newer version, follow the upstream instructions for installing system dependencies and the Python package with uv.

    You can specify the language using the -l flag. For example, to OCR in Russian:

    ocrmypdf -l rus input.pdf output.pdf

    Run ocrmypdf input.pdf output.pdf after installation. Verify search, text selection, and the visual result. Optional preprocessing can help poor scans, but some settings alter the page image.

    Use a shell loop with a separate output directory, as shown above. The --jobs option limits parallel work within each conversion. Check failed files before continuing the batch.

    Document Engine exposes OCR and other document operations through an HTTP API. It supports self-hosted and hosted deployments and can work with Nutrient client SDKs. Choose it when you need those integration and deployment options.

    Deploy Document Engine, configure its API token, and send a multipart request with an OCR action. Use /api/build to download a PDF result, or /api/documents to store a document. The complete examples above show the different response handling.

    Hulya Masharipov

    Hulya Masharipov

    Technical Writer

    Hulya is a frontend web developer and technical writer who enjoys creating responsive, scalable, and maintainable web experiences. She’s passionate about open source, web accessibility, cybersecurity privacy, and blockchain.

    Explore related topics

    Try for free Process documents on your own infrastructure