Installation:
composer require prinsfrank/pdfparser
Ensure your project meets the PHP 8.2+ requirement.
Basic Text Extraction:
use PrinsFrank\PdfParser\PdfParser;
$document = (new PdfParser())->parseFile('path/to/file.pdf');
$text = $document->getText();
First Use Case: Extract metadata from a PDF:
$title = $document->getInformationDictionary()?->getTitle();
$author = $document->getInformationDictionary()?->getAuthor();
Document class and its methods (getText(), getPages(), getInformationDictionary()).Batch Processing:
$parser = new PdfParser();
$files = glob('path/to/pdf/*.pdf');
foreach ($files as $file) {
$document = $parser->parseFile($file);
$text = $document->getText();
// Process text (e.g., save to DB, search for keywords)
}
Extracting Images:
foreach ($document->getPages() as $page) {
foreach ($page->getImages() as $image) {
$content = $image->getContent();
$extension = $image->getImageType()?->getFileExtension();
file_put_contents("images/{$page->getPageNumber()}_{$extension}", $content);
}
}
Handling Encrypted PDFs:
$document = (new PdfParser())->parseFile('encrypted.pdf', password: 'user_password');
Positioned Text Extraction:
foreach ($document->getPages() as $page) {
$textElements = $page->getPositionedTextElements();
foreach ($textElements as $element) {
echo "Text: {$element->getText()}, BBox: {$element->getBoundingBox()}\n";
}
}
Laravel Service Provider: Bind the parser as a singleton for reusable access:
$this->app->singleton(PdfParser::class, function ($app) {
return new PdfParser();
});
Queue Jobs for Large Files: Use Laravel queues to process large PDFs asynchronously:
ParsePdfJob::dispatch('path/to/large_file.pdf')->onQueue('pdf-processing');
Store Extracted Data: Save metadata or text to a database:
$metadata = [
'title' => $document->getInformationDictionary()?->getTitle(),
'author' => $document->getInformationDictionary()?->getAuthor(),
'text' => $document->getText(),
];
PdfMetadata::create($metadata);
Custom Decorators:
Extend existing decorators (e.g., Page) to add domain-specific logic:
class CustomPageDecorator extends Page {
public function getCustomData() {
// Add logic here
}
}
Memory Usage:
useInMemoryStream for large files to reduce memory footprint:
$document = (new PdfParser())->parseFile('large_file.pdf', useInMemoryStream: false);
useFileCache for string parsing to avoid high memory usage:
$document = (new PdfParser())->parseString($pdfString, useFileCache: true);
Encrypted PDFs:
$document = (new PdfParser())->parseFile('encrypted.pdf', password: 'correct_password');
CONTRIBUTING.md for encryption-related issues.Text Extraction Quirks:
Object Retrieval:
$document->getPage(0).$page = $document->getObject(42, Page::class); // Correct
$page = $document->getObject(42); // May return a generic object
Image Handling:
$image->isImage() before processing.getImageType() returns null). Handle gracefully:
if (!$image->isImage() || !$image->getImageType()) continue;
Log Parsing Issues: Enable debug mode in the parser (if available) or log raw objects for inspection:
$document = (new PdfParser())->parseFile('problematic.pdf');
$rawObject = $document->getObject(123); // Inspect problematic object
Check PDF Validity: Use tools like PDF.js or PDFtk to validate PDF structure before parsing.
Memory Profiling: Use Laravel Telescope or Xdebug to monitor memory usage during parsing.
Custom Object Decorators:
Extend existing decorators (e.g., Page, XObject) to add domain-specific methods:
class CustomPage extends Page {
public function getHighlightedText(string $keyword) {
// Implement custom logic
}
}
Override Parsing Logic:
Subclass PdfParser to modify parsing behavior:
class CustomPdfParser extends PdfParser {
protected function parseObject(Object $object) {
// Custom parsing logic
}
}
Add New Object Types:
Implement new decorators for unsupported PDF objects by extending the base Object class.
Hooks for Post-Processing: Use Laravel events or observer patterns to process extracted data after parsing:
event(new PdfParsed($document));
php -v to verify compatibility.ext-fileinfo is enabled for image type detection.useInMemoryStream and useFileCache based on file size and server resources.How can I help you explore Laravel packages today?