evosys21/pdflib
PHP addons for FPDF/TCPDF/tFPDF to build advanced tag-formatted multicells and tables. Supports alignment/justification, mixed fonts/styles/colors, padding, frames/backgrounds, links, tabs, and sub/superscripts for rich PDF layouts.
Installation:
composer require evosys21/pdflib
Choose your PDF engine (FPDF, TCPDF, or tFPDF) by extending the appropriate base class:
use EvoSys21\PdfLib\Tcpdf\Pdf; // Example for TCPDF
class MyPdf extends Pdf { ... }
First Use Case: Generate a simple table with formatted text:
$pdf = new MyPdf();
$pdf->AddPage();
$pdf->AdvancedTable([
['data' => ['Header 1', 'Header 2'], 'header' => true],
['data' => ['<u>Bold</u> Text', 'Normal Text']],
]);
$pdf->Output('output.pdf');
Key Files to Explore:
docs/multicell.md: For advanced text formatting (tags, alignment, colors).docs/table.md: For table-specific features (headers, row spans, page breaks).examples/Tcpdf/: Practical examples (e.g., example-table-1-overview.php).Text Formatting with Tags: Use tag-based strings for dynamic styling (e.g., underlines, colors):
$text = '<u>Underlined</u> and <c=red><b>Colored Bold</b></c> text';
$pdf->AdvancedMulticell($text, 80, 10, '', 'L', false, 0, 0, false, 0, false);
<u>: Underline<b>: Bold<i>: Italic<c=#RRGGBB>: Color<s=size>: Font sizeTable Generation:
Define tables as associative arrays with header and data keys:
$pdf->AdvancedTable([
['data' => ['ID', 'Name'], 'header' => true, 'align' => ['C', 'L']],
['data' => [1, 'John Doe'], 'align' => ['L', 'L']],
]);
L, C, R, J).['colspan' => 2]).Page Management: Disable page breaks for critical content:
$pdf->AdvancedMulticell($text, 80, 10, '', 'L', false, 0, 0, false, 0, true, 50); // Disable break, min height 50
Integration with Laravel: Use Service Providers to bind the PDF class:
// app/Providers/AppServiceProvider.php
public function register() {
$this->app->bind('pdf', function () {
return new \App\Pdf\CustomPdf();
});
}
Inject into controllers:
public function generatePdf() {
$pdf = app('pdf');
$pdf->AddPage();
// ... generate content
return $pdf->stream('document.pdf');
}
EvoSys21\PdfLib\Tcpdf\Pdf once and reuse across projects.'<u><c=blue><b>Critical</b></c> Note</u>'
setDebug(true) to log PDF generation steps (check docs/ for advanced usage).Tag Parsing Quirks:
<u>Text (missing </u>) may crash. Use htmlspecialchars() for dynamic content:
$safeText = htmlspecialchars($userInput, ENT_QUOTES, 'UTF-8');
<u><b>Text</u></b>). Use <u><b>Text</b></u> instead.Table Layout Issues:
$pdf->AdvancedTable([...], false); // Disable header repetition
colWidths to enforce proportions:
$pdf->AdvancedTable([...], null, null, ['colWidths' => [50, 100]]);
Performance:
$pdf->setAutoPageBreak(false);
// Manually handle page breaks
Font Conflicts:
Arial) are available in TCPDF's font directory (tcpdf/font/). Add custom fonts via:
$pdf->AddFont('custom', '', 'customfont.php');
$pdf->setErrorLogging(true);
Output('output.pdf', 'I') to download and manually check the PDF.if (preg_match('/<[^>]+>/', $text)) {
// Log or sanitize
}
Custom Tags:
Extend the parser by overriding parseTags() in your PDF class:
protected function parseTags($text) {
// Add custom logic (e.g., <strike> for strikethrough)
return parent::parseTags($text);
}
Hooks for Tables:
Use onTableStart/onTableEnd callbacks:
$pdf->setTableCallback(function ($pdf, $tableData) {
// Pre-process table data (e.g., add IDs)
});
Laravel Blade Integration: Create a Blade directive for PDF generation:
// app/Providers/BladeServiceProvider.php
Blade::directive('pdf', function ($expression) {
return "<?php echo app('pdf')->{$expression}; ?>";
});
Usage:
@pdf->AdvancedTable([...])
AddPage() defaults to A4. Override with:
$pdf->AddPage('L', 'Letter'); // US Letter size
AdvancedTable with images key:
['data' => ['<img=logo.png>'], 'images' => ['logo.png']]
public/ or provide full paths.storage/app/public/ and link via:
return response()->file(storage_path('app/public/document.pdf'));
GeneratePdfJob::dispatch($data)->onQueue('pdfs');
// Job class
public function handle() {
$pdf = new MyPdf();
$pdf->generate($this->data)->save(storage_path('app/public/output.pdf'));
}
How can I help you explore Laravel packages today?