OCRmyPDF: Local Deployment and Usage Tutorial 248

小编 ·

OCRmyPDF: Local Deployment and Usage Tutorial

OCRmyPDF is a tool mainly used to add a searchable and copyable OCR text layer to scanned PDF files. It supports multiple languages and offers features like image optimization, deskewing, page cleaning, and automatic rotation. Below is a guide to deploying OCRmyPDF locally:

OCRmyPDF: Local Deployment and Usage Tutorial

OCRmyPDF is a tool primarily used to add a searchable and copyable OCR text layer to scanned PDF files. It supports multiple languages and offers features such as image optimization, deskewing, page cleaning, and automatic orientation rotation. Below is the method for deploying OCRmyPDF locally:

OCRmyPDF Local Deployment and Usage Tutorial.webp

OCRmyPDF Local Deployment Method

OCRmyPDF offers multiple installation methods, depending on your operating system and requirements.

1. Install via System Package Manager

Debian/Ubuntu:

apt install ocrmypdf

Fedora:

dnf install ocrmypdf tesseract-osd

macOS (Homebrew):

brew install ocrmypdf

Windows Subsystem for Linux:

apt install ocrmypdf

2. Install Using Python pip

If you need to install the latest version or if the version provided by your system package manager is outdated, you can use pip:

pip install ocrmypdf

Alternatively, use pipx to create an isolated Python environment:

pipx install ocrmypdf

3. Install Using Docker

If you use Docker, you can install the OCRmyPDF Docker image with the following command:

docker pull jbarlow83/ocrmypdf

When running, you can mount the input and output files into the container:

docker run -v /path/to/input.pdf:/input.pdf -v /path/to/output.pdf:/output.pdf jbarlow83/ocrmypdf /input.pdf /output.pdf

4. Install from Source Code

If you need to install from source code (for example, to develop or test the latest features), you can clone the GitHub repository and install:

git clone https://github.com/ocrmypdf/OCRmyPDF.git
cd OCRmyPDF
pip install -e .

Dependencies

  • Python 3.10 or higher

  • Ghostscript 9.54 or higher

  • Tesseract 4.1.1 or higher

  • jbig2enc 0.29 or higher (optional, used to optimize output file size)

  • pngquant 2.5 or higher (optional, used to optimize PNG images)

  • unpaper 6.1 or higher (optional, used to clean images)

Important Notes

  • 32-bit support: OCRmyPDF does not support 32-bit systems. It is recommended to use a 64-bit version.

  • Windows users: It is recommended to use Windows Subsystem for Linux or Docker to avoid compatibility issues.

  • macOS users: You can install via Homebrew, but you need to additionally install Tesseract language packs (such as tesseract-lang).

Verify Installation

After installation, you can verify whether OCRmyPDF was installed successfully with the following command:

ocrmypdf --version

OCRmyPDF Usage Tutorial

1. Add OCR Text Layer

Core feature: Adds a searchable text layer to scanned PDF files, making the document content copyable and searchable.

Example command:

ocrmypdf input.pdf output.pdf

2. Multi-language Support

Feature description: Supports over 100 languages. Users can choose the appropriate language pack based on the document's language to improve OCR accuracy.

Example command:

ocrmypdf --language chi_sim input.pdf output.pdf  # Simplified Chinese
ocrmypdf --language eng input.pdf output.pdf      # English

3. Image Optimization

Feature description: Optimizes images in the PDF, including adjusting resolution and compressing image size, producing smaller files while maintaining image quality.

Example command:

ocrmypdf --optimize 3 input.pdf output.pdf  # Strongest optimization

4. Deskew and Clean

Feature description: Before performing OCR, deskews (corrects skew) and cleans (removes stains, noise, etc.) images to improve OCR accuracy.

Example command:

ocrmypdf --deskew --clean input.pdf output.pdf

5. Auto-rotate Pages

Feature description: Automatically detects page orientation and rotates pages, ensuring consistent orientation across all pages for easier reading and processing.

Example command:

ocrmypdf --rotate-pages input.pdf output.pdf

6. Convert to PDF/A Format

Feature description: Converts the output file to PDF/A format, which is an international standard archiving format suitable for long-term storage.

Example command:

ocrmypdf --output-type pdfa input.pdf output.pdf

7. Remove Duplicate Pages

Feature description: Automatically detects and removes duplicate pages in the PDF, reducing file size.

Example command:

ocrmypdf --remove-background input.pdf output.pdf

8. Background Removal

Feature description: Removes the background from PDF pages, making text clearer and easier to read.

Example command:

ocrmypdf --remove-background input.pdf output.pdf

9. Generate PDF/A Format

Feature description: Generates files that comply with the PDF/A standard, suitable for long-term archiving and storage.

Example command:

ocrmypdf --output-type pdfa input.pdf output.pdf

10. Skip Pages Already OCRed

Feature description: If certain pages in the PDF already contain a text layer, they can be skipped to avoid redundant processing.

Example command:

ocrmypdf --skip-text input.pdf output.pdf

11. Custom DPI

Feature description: Allows specifying the image resolution (DPI) during OCR processing. The default value is 300 DPI.

Example command:

ocrmypdf --dpi 350 input.pdf output.pdf

12. Convert to Monochrome Images

Feature description: Converts color or grayscale images to monochrome images, reducing file size.

Example command:

ocrmypdf --output-type pdf --monochrome input.pdf output.pdf

13. Generate Log File

Feature description: Generates a detailed log file for debugging and recording the processing process.

Example command:

ocrmypdf --log-file output.log input.pdf output.pdf

14. Batch Processing

Feature description: Combined with scripts or command-line tools (such as GNU Parallel), multiple PDF files can be processed in batch.

Example command:

for file in *.pdf; do
ocrmypdf "$file" "output_$file"
done

15. Support for Multiple Input Formats

The file generates PDF, including TIFF, PNG, etc.

Example command:

ocrmypdf input.tiff output.pdf

16. Compress PDF

Description: After OCR processing, the PDF file can be further compressed to reduce file size.

Example command:

ocrmypdf --optimize 3 input.pdf output.pdf

17. Convert to PDF/A-2b or PDF/A-2u

Description: Generate files that comply with the PDF/A-2b or PDF/A-2u standards, suitable for different needs.

Example command:

ocrmypdf --output-type pdfa-2b input.pdf output.pdf
ocrmypdf --output-type pdfa-2u input.pdf output.pdf

18. Unicode Support

Description: Supports Unicode characters, ensuring correct processing of multilingual documents.

Example command:

ocrmypdf --language chi_sim --language eng input.pdf output.pdf

19. Customize Output Filename

Description: The output file name can be customized for easier management and identification.

Example command:

ocrmypdf input.pdf "output_with_date_$(date +%Y%m%d).pdf"

20. Command-Line Arguments Support

Description: Through command-line arguments, various features of OCRmyPDF can be flexibly configured.

Example command:

ocrmypdf --help

OCRmyPDF Local Deployment and Usage Tutorial:https://ocrmypdf.readthedocs.io/en/latest/installation.html