Read the text out of many scanned PDFs with Tesseract and Poppler, one task run per document, on a Docker-based workflow service.
- A workflow service that builds from a
Dockerfile, because Tesseract and Poppler are system binaries - A small orchestrator task on
flexand a CPU-heavy OCR task on2c-4g - Passing documents by reference (a URL or a file name), not as bytes
- Classifying errors: bad input is returned as a failed result, a killed process or a network error is raised so Render retries it
- Fanning out in chunks with
ctx.runandasyncio.gather, without failing the batch on one document - Triggering the tasks from another service with the Render SDK client
ocr_batch (flex, fans out one chunk at a time, no retries)
└── ocr_document (2c-4g, one per document, retries twice)
├── fetch the PDF ← https:// URL, or a file in samples/
├── pdfinfo ← page count
├── pdftoppm ← one PNG per page
└── tesseract ← text of each page, two pages at a time
How ocr_document handles each failure:
| What happened | Example | What the task does |
|---|---|---|
| The input is bad | Corrupt PDF, a tool exits with code > 0, HTTP 404, too many pages | Returns {"doc_ref", "error"}. No retry |
| The tool isn't installed | tesseract missing when you run outside Docker |
Returns {"doc_ref", "error"}. No retry |
| A tool was killed by a signal | The kernel's out-of-memory killer sends SIGKILL to tesseract |
Raises. Render retries the run |
| The run ran out of memory | The plan's memory is used up and Render ends the attempt with Task terminated by OOM killer |
Nothing to catch: the attempt is gone. Render retries the run |
| The network failed | DNS error, connection reset, timeout, HTTP 429 or 5xx | Raises. Render retries the run |
A task has no "don't retry this error" option: any exception it raises uses up an attempt. So errors a retry can't fix are returned as results instead.
- Python 3.11+
- The Render CLI
- Tesseract and Poppler, to run locally (
brew install tesseract poppler, orapt-get install tesseract-ocr poppler-utils). On Render, theDockerfileinstalls them. - No API keys
Render's local task server doesn't build Dockerfiles, so run the service with the tools installed on your machine:
brew install tesseract poppler
pip install -r requirements.txt
render workflows dev -- python main.pyIn another terminal, OCR one document. Task input is a JSON array of the task's arguments, and a document is a file name in samples/ or an https:// URL:
render workflows tasks runs start ocr_document --local --input='["invoice-1042.pdf"]'OCR four of the samples at once. corrupt.pdf isn't a valid PDF, so it comes back under failed after one attempt:
render workflows tasks runs start ocr_batch --local \
--input='[["invoice-1042.pdf", "delivery-receipt-2207.pdf", "plan-amendment.pdf", "corrupt.pdf"]]'To see a retry, OCR a URL that can't resolve. The run raises a network error on all three attempts, then fails:
render workflows tasks runs start ocr_document --local --input='["https://scans.example.invalid/invoice.pdf"]'Optional settings, with their defaults, are in .env.example. Copy it to .env to change them.
Docker-based workflow services can only be created in the Render Dashboard, not with the CLI or API. They build from a Dockerfile in your repository; they can't pull a prebuilt image from a registry.
-
Push this folder to a Git repository Render can access.
-
In the Render Dashboard, click New > Workflow and connect the repository.
-
Set these fields:
Field Value Language Docker Root Directory batch-ocr(the folder that holds theDockerfile). Leave it empty if this folder is the repository root.Dockerfile Path ./DockerfileStart Command python main.py(required for Docker-based workflows, even though theDockerfilehas aCMD)Region Any Environment variables None required. Optionally the settings in .env.exampleThere's no Build Command for Docker-based workflows: the
Dockerfiledoes the build. -
Create the workflow. Once the first version is ready, run
ocr_batchfrom the Dashboard with[["invoice-1042.pdf", "delivery-receipt-2207.pdf", "plan-amendment.pdf", "corrupt.pdf"]]. -
To see an out-of-memory retry, run
ocr_documentwith["site-plan-oversized.pdf"]. That PDF is 3 KB, but its two pages are 60 inches square: at 300 DPI each one is an 18,000 × 18,000 pixel image, and Tesseract needs about 3.5 GB to read it. Two pages at once don't fit in2c-4g's 4 GB. On Render, the attempt ends withTask terminated by OOM killerand Render retries the run; every attempt runs out of memory the same way, so the run fails after three attempts. WithOCR_PAGE_WORKERS=1, the same document fits and returns its text.
ocr_document runs on the 2c-4g plan, which bills per second of each run. See compute plans.
client.py triggers both tasks with the Render SDK:
export RENDER_API_KEY=your-render-api-key
export WORKFLOW_SLUG=your-workflow-slug # defaults to batch-ocr
python client.pyThe core of it:
from render import Render
render = Render() # reads RENDER_API_KEY
run = render.workflows.run_task(f"{SLUG}/ocr_batch", [["invoice-1042.pdf", "https://..."]])
if run.status == "completed":
batch = run.results[0] # results is a list holding the return value
print(batch["succeeded"], batch["failed"])In your own app, upload each scan to object storage and pass a presigned https:// URL. run_task waits for the run to finish. To start a run and check on it later, use start_task instead.
- Pass references, not files. The combined arguments of a task run can't exceed 4 MB, so pass URLs or keys, not PDF bytes. See limits.
- Results stay on Render for 30 days.
ocr_documentreturns the text of every page, andocr_batchreturns all of it. For large documents, write the text to your own storage and return its key. See state retention. - Chained runs are rate limited. A workspace can start 1,000 chained runs per minute. Over that,
ctx.runfails instead of queueing.ocr_batchstartsBATCH_CHUNK_SIZEruns (default 20), waits for them, then starts the next chunk. A document whose run couldn't start is listed underfailed. See rate limits. - Timeouts apply per attempt. Each attempt of
ocr_documentgets 600 seconds and each retry gets a fresh 600.ocr_batchgets 3,600 seconds for the whole batch: every chunk lasts as long as its slowest document, retries included. Raise it for bigger batches. See timeouts. - A retry reruns the whole document. Retries start over on a new instance, so a 50-page PDF that fails on page 49 is OCRed again from page 1. Reading and OCR have no side effects, so that's safe. See retries.
- Retries only help when the next attempt can pass. An out-of-memory kill caused by huge pages happens again on the same plan, as
site-plan-oversized.pdfshows. If a document fails on every attempt withTask terminated by OOM killerorkilled by signal 9, lowerOCR_DPI, lowerOCR_PAGE_WORKERSor moveocr_documentto a plan with more memory. - OCR isn't exact. Tesseract misreads some characters, especially on noisy scans. Check the fields you depend on.