A Node.js tool for processing PDF documents with AI-powered image and text analysis.
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.

- 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)
- 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
Install Ghostscript using Homebrew:
brew install ghostscriptVerify installation:
which gs
gs --versionsudo apt-get update
sudo apt-get install ghostscript# CentOS/RHEL
sudo yum install ghostscript
# Fedora
sudo dnf install ghostscript- Download Ghostscript from https://www.ghostscript.com/download/gsdnld.html
- Install the executable
- Add Ghostscript to your system PATH
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.
-
Clone the repository:
git clone https://github.com/mb-mal/pdf-aiparser.git cd pdf-aiparser -
Install dependencies:
npm install
-
Make sure Ollama is running with the required model (default: gemma3:27b-it-qat).
node pdf-aiparser.js /path/to/your/document.pdfnode pdf-aiparser.js /path/to/your/document.pdf 5 10This will process pages 5 through 10 inclusive.
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')
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
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
Error: Command failed: execvp failed, errno = 2 (No such file or directory)
Cause: Ghostscript is not installed or not in system PATH
Solution:
- Install Ghostscript following the instructions in the Prerequisites section
- Verify installation with
gs --version - Restart your terminal after installation
Error: gm convert: Postscript delegate failed
Cause: GraphicsMagick cannot find or use Ghostscript
Solution:
- Ensure Ghostscript is installed and working
- Check GraphicsMagick configuration with
gm version - Look for "Ghostscript (Library)" in the feature support list
Error: Connection timeout or refused
Solution:
- Verify Ollama is running:
curl http://localhost:11434/api/version - Check if the specified model is available:
ollama list - Pull the required model if missing:
ollama pull gemma3:27b-it-qat
Error: Out of memory errors during processing
Solution:
- Process smaller page ranges using the range parameters
- Reduce image quality settings in the script
- Free up system memory before processing large documents
- 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
Contributions are welcome! Please feel free to submit a Pull Request.