Skip to content

mb-mal/pdf-aiparser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PDF AI Parser

A Node.js tool for processing PDF documents with AI-powered image and text analysis.

Overview

PDF AI Parser extracts text and images from PDF documents and leverages Ollama's AI capabilities to generate detailed descriptions of each page. The tool is designed to handle complex PDFs, including those with mixed content, and can process documents page by page with robust error handling. pdf-aiparser-example

Features

  • Extract text from PDF pages
  • Convert PDF pages to PNG images
  • Generate AI-powered descriptions of page content using Ollama
  • Resume processing from the last completed page
  • Support for specifying page ranges
  • Comprehensive error handling and retry mechanisms
  • Output in structured JSON format
  • Support for translation to other languages (currently configured for Chinese)

Prerequisites

System Requirements

  • Node.js (v14 or newer recommended)
  • Ghostscript (required for PDF to image conversion)
  • GraphicsMagick or ImageMagick (usually comes pre-installed on most systems)
  • Ollama server running locally or on a remote machine
  • Sufficient disk space for PDF processing and image storage

Installing System Dependencies

macOS

Install Ghostscript using Homebrew:

brew install ghostscript

Verify installation:

which gs
gs --version

Linux (Ubuntu/Debian)

sudo apt-get update
sudo apt-get install ghostscript

Linux (CentOS/RHEL/Fedora)

# CentOS/RHEL
sudo yum install ghostscript

# Fedora
sudo dnf install ghostscript

Windows

  1. Download Ghostscript from https://www.ghostscript.com/download/gsdnld.html
  2. Install the executable
  3. Add Ghostscript to your system PATH

Verification

After installation, verify that all dependencies are available:

node --version    # Should show Node.js version
gs --version      # Should show Ghostscript version
gm version        # Should show GraphicsMagick version (if using GraphicsMagick)

Note: The PDF to image conversion functionality requires Ghostscript to be properly installed and available in your system PATH. If you encounter errors like "Command failed: execvp failed, errno = 2", it typically means Ghostscript is not installed or not found in PATH.

Installation

  1. Clone the repository:

    git clone https://github.com/mb-mal/pdf-aiparser.git
    cd pdf-aiparser
  2. Install dependencies:

    npm install
  3. Make sure Ollama is running with the required model (default: gemma3:27b-it-qat).

Usage

Basic Usage

node pdf-aiparser.js /path/to/your/document.pdf

Process Specific Page Range

node pdf-aiparser.js /path/to/your/document.pdf 5 10

This will process pages 5 through 10 inclusive.

Configuration

Edit the following constants in the script to customize behavior:

  • MAX_RETRIES: Number of retry attempts for API calls (default: 3)
  • RETRY_DELAY: Delay between retries in milliseconds (default: 5000)
  • OLLAMA_HOST: Hostname for Ollama API (default: 'localhost')
  • OLLAMA_PORT: Port for Ollama API (default: '11434')
  • OLLAMA_MODEL: Ollama model to use (default: 'gemma3:27b-it-qat')
  • TARGET_LANGUAGE: Language for AI descriptions (default: 'Chinese')

Output Structure

The tool creates the following directory structure for each processed PDF:

processed_pdfs/
└── [pdf_name]/
    ├── images/
    │   ├── page.1.png
    │   ├── page.2.png
    │   └── ...
    ├── json/
    │   ├── page_1.json
    │   ├── page_2.json
    │   └── ...
    ├── combined_results.json
    └── error_log.txt (if errors occurred)

Each page's JSON file contains:

  • Page number
  • Extracted text
  • AI-generated description of the page content

Error Handling

The tool implements various error handling strategies:

  • Multiple text extraction methods with fallbacks
  • API call retries with configurable delays
  • Detailed error logging to error_log.txt
  • Continuation of processing despite individual page failures

Troubleshooting

Common Issues

PDF to Image Conversion Errors

Error: Command failed: execvp failed, errno = 2 (No such file or directory)

Cause: Ghostscript is not installed or not in system PATH

Solution:

  1. Install Ghostscript following the instructions in the Prerequisites section
  2. Verify installation with gs --version
  3. Restart your terminal after installation

GraphicsMagick/ImageMagick Issues

Error: gm convert: Postscript delegate failed

Cause: GraphicsMagick cannot find or use Ghostscript

Solution:

  1. Ensure Ghostscript is installed and working
  2. Check GraphicsMagick configuration with gm version
  3. Look for "Ghostscript (Library)" in the feature support list

Ollama Connection Issues

Error: Connection timeout or refused

Solution:

  1. Verify Ollama is running: curl http://localhost:11434/api/version
  2. Check if the specified model is available: ollama list
  3. Pull the required model if missing: ollama pull gemma3:27b-it-qat

Memory Issues

Error: Out of memory errors during processing

Solution:

  1. Process smaller page ranges using the range parameters
  2. Reduce image quality settings in the script
  3. Free up system memory before processing large documents

Dependencies

  • pdf-parse: For extracting text from PDFs
  • pdf-lib: For handling PDF documents
  • pdf2pic: For converting PDF pages to images
  • axios: For making HTTP requests to the Ollama API

License

MIT License

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

About

PDF parser both with pdf parse and AI through ollama with visual model

Resources

Stars

7 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors