OCR API – Create Searchable PDFs from Images and Scans
The MaraDocs OCR API creates searchable PDFs with a text layer. The original document is preserved – text is selectable and searchable. Not just extracted text.
Scanned documents and photos contain text that can be neither selected nor searched. Many OCR APIs return only extracted text, not the original document with an invisible text layer. What you want is the same PDF – same layout and same appearance – but with selectable, searchable content. That is what a proper OCR API for searchable PDFs should deliver.
Why Building Your Own Searchable PDF OCR Solution Takes Weeks
Anyone who wants to build this themselves quickly discovers that Tesseract, EasyOCR, or cloud OCR services return only plain text and bounding boxes. To create a searchable PDF, the text has to be overlaid invisibly on top of the original image or original PDF. This requires coordinating coordinate systems, fonts, encodings, and PDF structure. Different languages, scripts, and layouts add further complexity. A robust OCR API that leaves your document intact requires considerable development effort.
How the MaraDocs OCR API Solves This in Minutes
The MaraDocs API performs OCR and outputs a PDF with an invisibly overlaid text layer. You get the original document – layout, images, appearance – with selectable and searchable text. No separate text file. No stripped-down version. The same document, improved.
OCR Workflow: Validate, OCR, Optimize
For images: validate, then img.ocrToPdf. For PDFs: validate, then pdf.ocrToPdf (optionally after pdf.orientation to correct rotated pages beforehand). The high-level flow.ocrImg and flow.ocrPdf combine orientation, OCR, and optimization in a single call. The output is always a PDF handle – the same document with an invisible text layer – which can be downloaded or passed on to composition, compression, or email workflows. The pipeline stays server-side; there is no re-uploading between steps.
Get your API key in under a minute
Register for a free account and get your API key in under a minute. Of course we'll provide you with some developer credits.
Try MaraDocs API now →What Sets MaraDocs Apart: Workspaces, Webview, and German Data Protection Law
Most document APIs force you to upload, process, download, and re-upload for the next step. With MaraDocs, OCR runs in your own workspace. Chain it with document extraction, composition, or compression – simply pass the PDF handle directly to the next operation. No re-uploading, fewer round trips.
If OCR results need to be corrected manually – misrecognized characters, complex layouts, or low-quality scans – you can open app.maradocs.io for manual review and editing. Users get full manual control when automation reaches its limits.
All processing takes place in Germany (Maramia GmbH), encrypted at rest and in transit. Workspaces expire after 7 days. No data leaves the EU. For privacy-sensitive OCR workloads, this matters.
TypeScript Code for Creating Searchable PDFs with OCR
API reference: data/upload, img/validate, pdf/validate, img/ocr/to/pdf, pdf/ocr/pdf, data/download/pdf
import { MaraDocsClient } from "@maramia/maradocs-sdk-ts";
import { okImg } from "@maramia/maradocs-sdk-ts/models/img";
import { okPdf } from "@maramia/maradocs-sdk-ts/models/pdf";
const client = new MaraDocsClient({ workspaceSecret: workspace_secret });
// High-Level: hochladen, validieren, vollständige Pipeline, herunterladen
const pdfHandle = await client.flow.ocrImg(imageFile);
const blob = await client.data.downloadPdf({ pdf_handle: pdfHandle });
// Low-Level: Bild – hochladen, validieren, OCR, herunterladen
const uploaded = await client.data.upload(imageFile);
const validated = await client.img.validate({ unvalidated_file_handle: uploaded.unvalidated_file_handle });
const imgHandle = okImg(validated);
const ocrPdf = await client.img.ocrToPdf({
img_handle: imgHandle,
options: { embed_in_blank_page: { size: { width: 210, height: 297 }, position: "center" } },
});
const blob2 = await client.data.downloadPdf({ pdf_handle: ocrPdf.pdf_handle });
// PDF: hochladen, validieren, ocrToPdf, herunterladen
const pdfUploaded = await client.data.upload(pdfFile);
const pdfValidated = await client.pdf.validate({ unvalidated_file_handle: pdfUploaded.unvalidated_file_handle });
const pdfOcr = await client.pdf.ocrToPdf({ pdf_handle: okPdf(pdfValidated) });
const blob3 = await client.data.downloadPdf({ pdf_handle: pdfOcr.pdf_handle });
Python Code for OCR to Searchable PDF
API reference: data/upload, img/validate, img/ocr/to/pdf, pdf/ocr/pdf, data/download/pdf
# pip install python-decouple requests
"""OCR an image to a searchable PDF using MaraDocs. Set ACCOUNT_SECRET in .env or environment."""
import sys
import time
from pathlib import Path
import requests
from decouple import config
API_URL = "https://api.maradocs.io/v1"
def create_workspace() -> dict:
"""Create a workspace and return auth headers."""
r = requests.post(
f"{API_URL}/workspace",
headers={"Authorization": f"Bearer {config('ACCOUNT_SECRET')}"},
json={"subaccount": None},
)
ws = r.json()
return {"Authorization": f"Bearer {ws['workspace_secret']}"}
def upload_file(path: Path, auth: dict) -> dict:
"""Upload a file via two-step flow (signed URL + S3 POST). Returns unvalidated_file_handle."""
data = path.read_bytes()
resp = requests.post(
f"{API_URL}/data/upload",
headers={**auth, "Content-Type": "application/json"},
json={"name": path.name, "size": len(data)},
).json()
requests.post(
resp["post_url"],
data=resp.get("post_header", {}),
files={"file": (path.name, data, "image/png")},
)
return resp["unvalidated_file_handle"]
def run_job(path: str, payload: dict, auth: dict, timeout: int = 60) -> dict:
"""Run a job and poll until complete. Returns the result (unwraps response if present)."""
url = f"{API_URL}/{path}"
r = requests.post(url, headers=auth, json=payload).json()
job_id = r["job_id"]
start = time.time()
while time.time() - start < timeout:
poll_r = requests.get(f"{url}/{job_id}", headers=auth)
if poll_r.status_code == 200:
return poll_r.json()
raise TimeoutError(f"Job {path} timed out")
def download_pdf(pdf_handle: str, auth: dict) -> bytes:
"""Request signed URL and fetch PDF bytes."""
r = requests.post(
f"{API_URL}/data/download/pdf",
headers=auth,
json={"pdf_handle": pdf_handle},
)
info = r.json()
dl = requests.get(info["url"], headers=info.get("headers", {}))
return dl.content
def main() -> None:
img_path = Path(sys.argv[1]) if len(sys.argv) > 1 else Path("photo.png")
auth = create_workspace()
handle = upload_file(img_path, auth)
validate = run_job("img/validate", {"unvalidated_file_handle": handle}, auth)
img_handle = validate["response"]["img_handle"]
ocr_result = run_job("img/ocr/to/pdf", {"img_handle": img_handle}, auth)
pdf_handle = ocr_result.get("pdf_handle") or ocr_result.get("response", {}).get("pdf_handle")
Path("searchable.pdf").write_bytes(download_pdf(pdf_handle, auth))
print("searchable.pdf created")
if __name__ == "__main__":
main()
Summary and Next Steps
An OCR API that creates searchable PDFs – the original document with an invisible text layer – is available. MaraDocs preserves the original layout and adds selectable, searchable text. More related workflows: document scanner, PDF processing, and image on blank page.
Useful links
Try it now: MaraDocs API | TypeScript SDK
Subscribe to our newsletter
Stay up to date with us and receive the latest news, articles, and resources by email.