metolabs / text-extractor-php
PHP wrapper for extracting text from PDF documents and images
Requires
- php: >=8.0
- spatie/pdf-to-image: ^1.2
- spatie/pdf-to-text: ^1.54
- thiagoalessio/tesseract_ocr: ^2.13
Requires (Dev)
- phpunit/phpunit: ^9.5
Suggests
- imagick: Optional. Required for some image processing features.
- poppler-utils: Required for pdftotext and pdfimages binaries (pdf to text and images).
- tesseract-ocr: Required for OCR extraction via Tesseract. Install Tesseract on your system.
This package is auto-updated.
Last update: 2025-06-12 07:03:57 UTC
README
A PHP library to extract text from PDFs and images using Spatie's PDF utilities and Tesseract OCR. Supports multiple extraction modes: text-based, OCR, or automatic.
Features
- Extract text from PDF using
spatie/pdf-to-text
- Convert PDF to images using
spatie/pdf-to-image
and apply OCR - Extract from images directly using
thiagoalessio/tesseract_ocr
- Automatic mode: falls back to OCR if text extraction fails
- Configurable extraction settings
Installation
composer require metolabs/text-extractor
Usage
Basic usage
use MetoLabs\TextExtractor\TextExtractor; $text = TextExtractor::from('example.pdf'); echo $text;
Extract from image
$text = TextExtractor::from('example.png');
Use specific mode
$extractor = TextExtractor::make('example.pdf', TextExtractor::MODE_TEXT); $extractor->extract(); echo $extractor->getText();
Configure OCR and resolution
use MetoLabs\TextExtractor\Configurations\PdfToImageConfiguration; use MetoLabs\TextExtractor\Configurations\TesseractConfiguration; $config = [ 'pdftoimage' => (new PdfToImageConfiguration())->setResolution(300), 'tesseract' => (new TesseractConfiguration())->set('options', [ 'lang' => 'eng', ]), ]; $extractor = TextExtractor::make('example.pdf', TextExtractor::MODE_OCR, $config); $extractor->extract(); echo $extractor->getText();
Extract specific pages
use MetoLabs\TextExtractor\Configurations\PdfToImageConfiguration; $config = [ 'pdftoimage' => (new PdfToImageConfiguration())->setPages([1, 3, 5]), ]; $extractor = TextExtractor::make('example.pdf', TextExtractor::MODE_AUTO, $config); $extractor->extract(); echo $extractor->getText();
Modes
TextExtractor::MODE_TEXT
– Usespdftotext
for extraction.TextExtractor::MODE_OCR
– Converts PDF to images and uses OCR.TextExtractor::MODE_AUTO
– Triespdftotext
, falls back to OCR if it fails.
Exceptions
MissingInputFileException
InvalidModeException
TextExtractionFailedException
- Any exceptions from Spatie or Tesseract OCR libraries
Requirements
- PHP 8.0+
pdftotext
andpdftoppm
must be installed and accessible in system pathtesseract
OCR must be installed
License
MIT License
Copyright (c) 2025 Gerardo Ramirez
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
**THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.