Translate a PDF to Hindi (or any Indic language)

View as Markdown

This guide walks through translating a PDF from English into Hindi using the Document Translation API. The same flow works for Word, Excel, PowerPoint, and HTML — only the upload Content-Type changes.

Need to translate a sentence or paragraph instead of a file? Use the Text Translation API.

Prerequisites

  • A Sarvam API key from Key Management
  • Python 3.10+ with pip install sarvamai httpx
  • A PDF file on disk (example: report.pdf)

Billing: ₹20 per 10,000 billable characters per target language (after parsing). See Pricing.

Steps

1

Create the job

Call document_translation.create with source language, target language(s), and the original filename. You receive a job_id and a signed upload_url.

2

Upload the PDF

PUT the file bytes to upload_url with Content-Type: application/pdf and x-ms-blob-type: BlockBlob. The SDK does not upload for you yet.

3

Start translation

POST start (via client.document_translation.start(job_id=...)). The pipeline parses the PDF, runs OCR where needed, and translates each target language.

4

Poll until complete

Poll get_live_status every 10–15 seconds until each target language’s state is Completed. See Poll and export.

5

Export and download

For each target language, call export, poll export/status, then download from download_url. You get a translated PDF in the same layout as the source.

Example (create, upload, start)

import os
from pathlib import Path
import httpx
from sarvamai import SarvamAI
from sarvamai.core.api_error import ApiError
api_key = os.getenv("SARVAM_API_KEY")
if not api_key:
raise ValueError("Set SARVAM_API_KEY before running this example.")
client = SarvamAI(api_subscription_key=api_key)
pdf = Path("report.pdf")
try:
job = client.document_translation.create(
source_language_code="en-IN",
target_language_codes=["hi-IN"],
original_filename=pdf.name,
)
with pdf.open("rb") as f:
upload = httpx.put(
job.upload_url,
content=f.read(),
headers={
"Content-Type": "application/pdf",
"x-ms-blob-type": "BlockBlob",
},
timeout=120.0,
)
upload.raise_for_status()
client.document_translation.start(job_id=job.job_id)
print("Job started:", job.job_id)
except ApiError as e:
print(f"API error {e.status_code}: {e.body}")
except httpx.HTTPError as e:
print(f"Upload failed: {e}")

Continue with the polling and export loop in Poll and export.