Skip to main content

PdfOCRer

PdfOCRer is a python script that runs OCR on an input PDF (possibly unsearchable) to produce a searchable PDF.

It uses Paddle OCR as the OCR engine. Compared to the famous OCR engine Tesseract, Paddle OCR shows better results for Chinese language in my experiments. However, it cannot seem to generate searchable PDF from its OCR result, whereas Tesseract can. If that's what you need, PdfOCRer can help.

PdfOCRer processes a PDF in four steps:

  • Step 1: convert each pdf page into an image (default format PNG).
  • Step 2: run OCR on each image to recognize text and bounding boxes.
  • Step 3: add text/bbox on image as hidden layer to make 1-page PDF.
  • Step 4: merge all 1-page pdfs to output the result searchable pdf.

Dependencies

The script is tested in Python 3.10. It should work as long as the dependencies work.

  • Paddle OCR and PaddlePaddle: follow its instruction here.

  • PyPDF2, Pillow, ReportLab: pip install pillow PyPDF2 reportlab

  • Ghostscript: used to convert pdf to images, called from the script as subprocess.

    -- on Linux/Ubuntu: apt-get install ghostscript,

    -- on MacOS machine: brew install ghostscript,

    -- on Windows machine: download 32/64 bit exe, run it to install.

Installation

Simply run in command line:

pip install pdfocrer

It will install all dependencies but Ghostscript.

How to use

To use the script in command line, run it like following:

python pdf_ocrer.py -i <input_pdf> -o <output_pdf> -l <language> -t <temp_dir> [--debug]

Example:

python pdf_ocrer.py -i ../example/scanned_page.pdf -o ../example/scanned_page.ocr.pdf -t ../temp -l ch --debug

To use it in python code, do something like:

import os, sys

from pdfocrer.pdf_ocrer import PdfOCRer

input = './example/scanned_page.pdf'
output = './example/scanned_page.ocr.pdf'
isDebug = True
tempDir = './temp'
language = 'ch'  # or 'en', 'korean', 'japan', 'latin', 'arabic',  etc.

pp = PdfOCRer(isDebug, tempDir)

pp.process_pdf(input, output, language)

Acknowledgement

Thanks to the authors of all the depencenies libraries, the Python and Open Source Community.

Metadata

Release files for PdfOCRer 1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for PdfOCRer 1.2
File Size Uploaded
pdfocrer-1.2.tar.gz 7.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for PdfOCRer 1.2
File Interpreter ABI Platform
PdfOCRer-1.2-py3-none-any.whl Python 3 none any Details

Total release size: 21.3 kB

Release files / pdfocrer-1.2.tar.gz

Download URL pdfocrer-1.2.tar.gz
Size 7.9 kB
Tags Source
SHA-256 checksum
How to use checksums
50cc5d9180b2d899c227dc2b1868b096d050cab3c5eea42856fd1fb181b4b883
BLAKE2b-256 checksum
How to use checksums
d1b46dcba64f9a0b47fcbcc8f07f3d0ddce2a7c138dd63d20e56232af719788b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.10.15

Release files / PdfOCRer-1.2-py3-none-any.whl

Download URL PdfOCRer-1.2-py3-none-any.whl
Size 13.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
da99d2fc2f1c58d56b4f821e7f12ff300254e941b9e353d97413a0b17e7194d1
BLAKE2b-256 checksum
How to use checksums
fd40e41bfefdd95b566c006e3976d718f61365ac58bfd66c15025cc1c482aaf7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.10.15

Release history Release notifications | RSS feed

This release

1.2 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page