Skip to content

royalgillz/pdf-translator

Repository files navigation

PDF Translator

A modern desktop app that extracts, translates, and exports PDF documents. It works online with no API key, or fully offline for privacy. It preserves the original page layout, handles scanned PDFs with on-device OCR, and was built Indian-languages-first (34 languages, on-screen keyboards for 13 scripts).

Try it in your browser, no install: https://sehajgill-pdf-translator.hf.space

PDF Translator

Why this exists

Walk into almost any government office in India and the form in your hand is printed in English. The application for a pension, a ration card, a loan under a welfare scheme, a caste or income certificate: all of it assumes you can read English. Most people cannot. Crores of citizens are fluent in Hindi, Tamil, Bengali, Marathi, or Punjabi but were never taught to read the language their own paperwork is written in.

So a small economy grows around that gap. People pay agents to read forms out loud, to tell them which box is asking for a father's name and which one wants an income figure. A mistranslation by a middleman, or a blank left in the wrong place, can mean a rejected application and another trip to the office.

The honest fix is not to retype the form. It is to keep the exact same form, in the exact same layout, with the exact same boxes and the official heading and the photo square in the corner, and simply swap the English words for the citizen's own language. That is what this app does. It reads each line of the PDF with its position on the page, removes the English text, and draws the translated text back into the very same spot. A digital form is rewritten precisely; a scanned form is read with OCR, the old text painted over, and the translation written on top. What comes out is a PDF that looks and prints like the original, except a person can finally read it.

It runs fully offline too, which matters when the document is a private financial or identity form that should never leave the device.

Highlights

  • Layout-preserving ("in-place") translation. The output is a translated PDF that keeps the original layout. Text is read with its geometry, removed, and the translation drawn back in the same place. This works for digital PDFs (exact) and scanned PDFs (OCR boxes painted over and rewritten). Coloured panels keep their background, so light text on a dark sidebar stays readable.
  • Side-by-side preview with editable blocks. Generate the layout-preserved result and see it next to a table of every translated block. Fix any translation, or edit a source line to re-translate it, then click Update preview and your corrections flow into the saved PDF.
  • Correct complex-script rendering. Devanagari and other Indic scripts are shaped with HarfBuzz so matras and conjuncts render correctly in the output, instead of landing on the wrong side as they do with naive text insertion. Every supported script ships its own Noto font, and mixed lines (a Gurmukhi field with English labels and digits) are drawn run by run with the right font.
  • Real-time bidirectional translation. Type in either pane and the other updates live (on Space, on Enter, or after a short pause). Toggle it off for large documents. Editing the translation back-translates to the source.
  • Three engines, one toggle.
    • Google (free, no key) is the default. It uses Google's free endpoint, needs no API key or billing, and auto-detects the source. It just needs internet.
    • Google Cloud (API key) uses a service-account key, for higher volume.
    • Argos (offline) is fully on-device for privacy or air-gapped use. Models download once, then no internet is needed.
  • On-device OCR. Digital pages are read natively (fast and exact); scanned pages are OCR'd. The backend is pluggable: RapidOCR (ONNX, installs on Python 3.14) or PaddleOCR (broadest language coverage).
  • 34 languages, on-screen virtual keyboards for Indian scripts, right-to-left rendering, and editable side-by-side panes.
  • Export to PDF, DOCX, or TXT, batch translation, drag-and-drop, dark and light themes, a threaded UI that never freezes, and persisted settings.

Install and run

python -m venv .venv
.venv\Scripts\activate              # Windows  (source .venv/bin/activate on Unix)
pip install -r requirements.txt

python main.py                      # or:  python -m pdf_translator

Requires Python 3.10 or newer. On first run the free engine works out of the box. To use the offline engine, download models once (see below).

Verify your setup

python diagnose.py

Checks dependencies, the OCR backend, all three engines (with a real test translation), and the full in-place pipeline on a generated PDF.

Offline engine (Argos)

python download_models.py           # English to and from Hindi
python download_models.py ta te bn  # English to and from Tamil, Telugu, Bengali

On air-gapped machines, copy *.argosmodel files into the app's argos-models/ folder (under the per-user data dir) and they install automatically.

Online with a Google Cloud key (optional)

Put a service-account JSON in credentials/ (auto-discovered) or set GOOGLE_APPLICATION_CREDENTIALS, then pick Google Cloud (API key) in the app.

Build a standalone Windows executable

pip install pyinstaller
pyinstaller main.spec                # produces "dist/PDF Translator/PDF Translator.exe"

The bundled app ships the free online engine and RapidOCR. The heavy optional engines (Argos and Torch, Google Cloud, Paddle) are excluded to keep the size down, so use the Python install for those.

Website (browser version)

A no-install browser version runs the same core behind a small FastAPI service: https://sehajgill-pdf-translator.hf.space

It mirrors the desktop flow (upload, pick languages with swap, layout-preserving translation, side-by-side preview, per-block edit-and-regenerate, OCR for scans, export to PDF, DOCX or TXT, and batch) using the free Google engine. To use your own Google Cloud key, use the desktop app. Files are processed in a temporary folder and deleted as soon as the result is ready.

Run it locally:

pip install -r server/requirements.txt
python serve.py                      # http://localhost:8000

Deploy to a Hugging Face Docker Space:

hf auth login                        # one-time, token with write access
python deploy_hf.py

Usage

  1. Drag a PDF onto the window (or click the drop area to browse). Text is extracted into the left pane.
  2. Pick the target language (and the source, or leave it on Auto-detect). With Live translate on, the right pane fills as you type or edit.
  3. Click Preview to render the layout-preserved result beside an editable list of every block. Correct any translation there (or edit a source line to re-translate it), then Update preview.
  4. Use Save translated PDF to write the result with the original layout, or Export text (PDF, DOCX, TXT) for a plain reflowed copy. The status bar links straight to the saved file or its folder.
  5. Drop multiple PDFs (or use Batch translate) to process many files into a folder, each kept in its original layout.

Project structure

assets/fonts/                      base NotoSans plus per-script Noto fonts (Gurmukhi, Tamil, Arabic, CJK, ...)
credentials/                       Google service-account JSON (git-ignored)
docs/                              screenshots and report
samples/                           sample input PDFs
src/pdf_translator/
  app.py                           entry point (QApplication)
  config.py                        paths, credential discovery, persisted Settings
  languages.py                     language codes, OCR-model map, keyboards
  diagnose.py                      self-diagnostic (python diagnose.py)
  core/
    extractor.py                   native-first extraction with OCR fallback
    ocr.py                         pluggable OCR (RapidOCR or PaddleOCR)
    layout_translate.py            in-place layout-preserving translation
    shaping.py                     HarfBuzz complex-script glyph shaping
    fonts.py                       per-script font selection for the output
    exporters.py                   PDF, DOCX, TXT writers
    translation/                   pluggable engines: free, google, argos
  ui/
    main_window.py                 window, live translate, preview, block editing
    workers.py                     QThread workers (extract, translate, in-place, batch)
    widgets.py                     drag-drop zone and virtual keyboard
    theme.py                       dark and light stylesheet
web/                               browser frontend (landing page + translator workbench)
server/                            FastAPI backend that reuses the package, plus its requirements
tests/                             pytest suite (no heavy deps needed)
main.py, serve.py, deploy_hf.py, diagnose.py, download_models.py, main.spec, Dockerfile
requirements.txt, pyproject.toml, LICENSE

Run tests

pip install pytest
pytest
pytest tests/test_core.py::test_default_engine_is_free   # a single test

Roadmap and USP ideas

Grounded in current market direction (layout preservation is the top 2025 differentiator, and privacy or offline use is a real edge over cloud-only tools):

  1. Even better layout fidelity: table and column reconstruction, font and colour matching.
  2. Glossary or terminology lock for consistent domain vocabulary.
  3. Translation memory to reuse prior segments, cut cost, and keep wording consistent.
  4. Privacy mode as a headline: your documents never leave this device.
  5. More input formats: DOCX, PPTX, image, and bilingual side-by-side export.

License

MIT. See LICENSE.

About

Desktop PDF translator that keeps the original layout. 34 languages, offline or online, with OCR for scanned forms.

Topics

Resources

License

Stars

0 stars

Watchers

1 watching

Forks

Packages

 
 
 

Contributors