tecnickcom/tc-lib-pdf-parser
Parse raw PDF data into structured PHP arrays for content inspection, metadata extraction, validation, and migration pipelines. Pure-PHP, PHP 8.2+, with cross-reference and object stream parsing plus typed exceptions.
Installation Add the package via Composer:
composer require tecnickcom/tc-lib-pdf-parser
Require the autoloader in your Laravel project (handled automatically by Composer).
First Use Case: Extract Text from a PDF
use Tecnickcom\TCPDF\Parser\Parser;
$parser = new Parser();
$result = $parser->parseFile('path/to/your/file.pdf');
// Access parsed content (e.g., text, metadata)
$text = $result->getText();
echo $text;
Where to Look First
Tecnickcom/TCPDF/Parser/Parser.php for core functionality.tests/ directory in the repo for usage patterns.Batch Processing Use Laravel’s queue system to process large PDFs asynchronously:
use Illuminate\Support\Facades\Queue;
use Tecnickcom\TCPDF\Parser\Parser;
Queue::push(function () {
$parser = new Parser();
$result = $parser->parseFile(storage_path('pdfs/example.pdf'));
// Store extracted data in DB or filesystem
});
Metadata Extraction Extract PDF metadata (author, title, etc.) for indexing:
$metadata = $result->getInfo();
$title = $metadata['Title'] ?? 'Untitled';
Text Search Combine with Laravel’s Scout or database full-text search:
$text = $result->getText();
$searchTerm = 'contract';
if (Str::contains(strtolower($text), strtolower($searchTerm))) {
// Trigger action (e.g., flag document)
}
storage/app/public and use the filesystem facade to manage paths.Parser class to Laravel’s container for dependency injection:
// app/Providers/AppServiceProvider.php
$this->app->bind(Parser::class, function () {
return new Parser();
});
use Illuminate\Console\Command;
use Tecnickcom\TCPDF\Parser\Parser;
class ProcessPdfCommand extends Command
{
protected $signature = 'pdf:parse {path}';
public function handle(Parser $parser) {
$result = $parser->parseFile($this->argument('path'));
// Process result...
}
}
Encoding Issues
$text = mb_convert_encoding($result->getText(), 'UTF-8', 'auto');
dd($result->getText()) to inspect raw output for encoding artifacts.Complex PDFs (Tables, Images)
spatie/pdf-to-text) if needed.parse() instead of parseFile() for in-memory parsing of simpler PDFs.Memory Limits
.env:
MEMORY_LIMIT=1024M
fopen).Metadata Inconsistencies
null for metadata fields. Validate before use:
$author = $metadata['Author'] ?? null;
try {
$result = $parser->parseFile($path);
} catch (\Exception $e) {
Log::error("PDF parse failed: {$e->getMessage()}");
}
$result object to understand its structure:
dd(get_object_vars($result));
Custom Parsing Logic
Extend the Parser class to add domain-specific logic:
class CustomPdfParser extends Parser {
public function extractContracts() {
$text = $this->getText();
// Add regex or NLP to detect contracts
}
}
Event Listeners Trigger events after parsing (e.g., store extracted data):
event(new PdfParsed($result, $path));
Register in EventServiceProvider:
protected $listen = [
PdfParsed::class => [PdfProcessor::class],
];
Laravel Scout Integration Index parsed text for search:
use Laravel\Scout\Searchable;
class Document implements Searchable {
public function toSearchableArray() {
return [
'text' => $this->pdfParser->parseFile($this->path)->getText(),
];
}
}
config/pdf_parser.php) to store paths or defaults.$parser = new Parser([
'encoding' => 'UTF-8',
'mergeBlocks' => true,
]);
How can I help you explore Laravel packages today?