jstewmc/rtf
PHP library for reading, parsing, and working with Rich Text Format (RTF) documents. Extract text and metadata, traverse document structure, and integrate RTF handling into your applications with a lightweight, dependency-friendly package.
Installation
composer require jstewmc/rtf
Requires PHP 7.4+ and the ext-dom extension.
First Use Case: Reading RTF
use JSTewMc\RTF\RTF;
$rtf = RTF::loadFromFile('document.rtf');
echo $rtf->getText(); // Extract plain text
First Use Case: Writing RTF
$rtf = new RTF();
$rtf->addText('Hello, RTF!');
$rtf->saveToFile('output.rtf');
Key Entry Points
RTF::loadFromFile() / RTF::loadFromString() for parsing.$rtf->getText() for plain-text extraction.$rtf->addText(), $rtf->addImage(), etc., for building documents.$rtf->saveToFile() / $rtf->saveToString() for output.Documentation
$rtf = RTF::loadFromFile('invoices.rtf');
$text = $rtf->getText(); // Full plain-text content
$images = $rtf->getImages(); // Array of embedded images (base64)
Use Case: Scraping structured data (e.g., invoices, reports) where RTF is the source.
$rtf = new RTF();
$rtf->addText('Invoice #12345', ['font' => 'Arial', 'size' => 14, 'bold' => true]);
$rtf->addParagraph('Date: ' . now()->format('Y-m-d'));
$rtf->addTable([
['Item', 'Qty', 'Price'],
['Product A', 2, '$10.00'],
]);
$rtf->saveToFile('invoice.rtf');
Use Case: Dynamic report generation (e.g., PDF alternatives, Word-like exports).
$rtf = RTF::loadFromFile('template.rtf');
$rtf->replaceText('{name}', 'John Doe'); // Simple string replacement
$rtf->saveToFile('personalized.rtf');
Use Case: Templating (e.g., merging RTF templates with user data).
$rtf = new RTF();
$rtf->addImage('logo.png', 'Logo', ['width' => 200, 'height' => 100]);
$rtf->saveToFile('document.rtf');
Use Case: Embedding logos, charts, or signatures in generated documents.
Store RTF in Database:
Use saveToString() to store RTF as a text column, then loadFromString() to retrieve.
$rtfString = $rtf->saveToString();
$model->document = $rtfString;
$model->save();
Queue RTF Generation: Dispatch a job for heavy RTF processing (e.g., large tables or images).
GenerateRtfJob::dispatch($data)->onQueue('rtf');
Blade Templating: Combine with Blade for dynamic content:
$rtf = new RTF();
$rtf->addText(view('emails.rtf_template', ['user' => $user])->render());
RTF class to test interactions with RTF-generated content.files_equal()).getText() for plain-text extraction if full parsing fails.if (!getimagesizefromstring(file_get_contents('image.png'))) {
throw new \InvalidArgumentException('Invalid image');
}
$rtf->addText('Привет!', ['encoding' => 'utf-8']);
memory_limit.memory_limit or process files in chunks.$rtfString = $rtf->saveToString();
file_put_contents('debug.rtf', $rtfString);
Open debug.rtf in a text editor to verify structure.
try {
$rtf->addText('Test'); // May throw exceptions
} catch (\Exception $e) {
\Log::error('RTF generation failed', ['error' => $e->getMessage(), 'trace' => $e->getTraceAsString()]);
}
Extend the package by adding custom style handlers:
$rtf->addText('Custom Style', [
'font' => 'CustomFont',
'size' => 12,
'color' => '\red255\green0\blue0\' // RGB in RTF syntax
]);
RTF class for dependency injection:
$this->app->bind(RTF::class, function () {
return new RTF();
});
php artisan rtf:generate --file=template.rtf --data=user1.json
Trigger events for RTF generation (e.g., logging, notifications):
event(new RtfGenerated($rtf, 'document.rtf'));
RTF class, and publish as a private package..env or config file. All options are method arguments.$rtf->setDefaultFont('Arial');
$rtf->setDefaultSize(12);
How can I help you explore Laravel packages today?