himelali / pdf-generator
Laravel wrapper for Snappy, DomPDF, mPDF, fPDF
Requires
- php: >=7.1
- ext-gd: *
- ext-zlib: *
- dompdf/dompdf: ^3.1
- h4cc/wkhtmltoimage-amd64: 0.12.x
- h4cc/wkhtmltopdf-amd64: 0.12.x
- knplabs/knp-snappy: ^1.4
- mpdf/mpdf: ^8.2
- setasign/fpdf: ^1.8
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- phpstan/phpstan: ^1.0
- phpunit/phpunit: ^10.0
- squizlabs/php_codesniffer: ^3.0
This package is auto-updated.
Last update: 2025-05-09 20:11:50 UTC
README
📄 PdfGenerator for Laravel
PdfGenerator is a flexible and extensible PDF generation wrapper for Laravel that supports multiple drivers including DomPDF, Snappy (wkhtmltopdf), mPDF, and FPDF — all with a unified interface.
🚀 Features
- Multiple driver support:
dompdf
,snappy
,mpdf
,fpdf
- Unified API for all drivers
- Configurable driver options
- Easily extendable with custom drivers
- Laravel service provider and config publishing
- Supports UTF-8, HTML5, CSS3, custom fonts, header/footer, margins, and more
📦 Installation
composer require himelali/pdf-generator
⚙️ Configuration
🧪 Laravel Version Compatibility
Depending on your Laravel version, register the service provider as follows:
Laravel 11+
Edit bootstrap/providers.php
:
return [ // Other providers... \Himelali\PdfGenerator\PdfServiceProvider::class, ];
Laravel 10 and below
'providers' => [ // Other providers... \Himelali\PdfGenerator\PdfServiceProvider::class, ],
For older versions, register the alias manually in config/app.php
:
'aliases' => Facade::defaultAliases()->merge([ 'Pdf' => Himelali\PdfGenerator\Facades\Pdf::class, ])->toArray(),
⚠️ If Facade::defaultAliases() doesn't exist in your Laravel version, just use:
'aliases' => [ // Other aliases... 'Pdf' => Himelali\PdfGenerator\Facades\Pdf::class, ],
The service provider will register the PDF manager and allow you to publish the config file using:
php artisan vendor:publish --tag=config
This will publish config/pdf-generator.php
. Example configuration:
return [ 'default' => env('PDF_DRIVER', 'dompdf'), 'drivers' => [ 'dompdf' => [ 'options' => [ 'is_html5_parser_enabled' => true, 'is_php_enabled' => true, ], ], 'snappy' => [ 'binary' => env('SNAPPY_PDF_BINARY', '/usr/local/bin/wkhtmltopdf'), 'options' => [ 'page-size' => 'A4', 'margin-top' => '10mm', 'margin-bottom' => '10mm', 'footer-left' => 'Your Company Name', 'footer-center' => '[page]', 'footer-right' => 'Confidential', ], ], 'mpdf' => [ 'format' => 'A4', 'orientation' => 'P', 'margin_top' => 10, 'margin_bottom' => 10, 'margin_left' => 10, 'margin_right' => 10, 'temp_dir' => storage_path('app/mpdf/temp'), 'font_dir' => storage_path('app/pdf/fonts'), ], 'fpdf' => [ 'orientation' => 'P', 'unit' => 'mm', 'format' => 'A4', 'font_dir' => storage_path('app/pdf/fonts'), ], ], ];
🧩 Supported Drivers
Driver | Package Dependency | Binary Required |
---|---|---|
dompdf | dompdf/dompdf |
❌ |
snappy | knplabs/knp-snappy + wkhtmltopdf binary link |
✅ |
mpdf | mpdf/mpdf |
❌ |
fpdf | setasign/fpdf |
❌ |
Install the required dependencies based on your driver.
use Pdf; $html = '<h1>Hello PDF</h1>'; Pdf::loadHtml($html)->download('hello.pdf');
Pdf::setPageSize('A4') ->setMargins(10, 15, 10, 15) ->setHeader('<p>Header HTML</p>') ->setFooter('<p>Footer HTML</p>') ->loadHtml('<h1>With Headers and Margins</h1>') ->download('report.pdf');
📂 Available Methods
Method | Description |
---|---|
setPageSize() |
Set paper size (e.g., A4, Letter) |
setMargins() |
Set page margins (top, right, bottom, left) |
setHeader() |
Set HTML for header |
setFooter() |
Set HTML for footer |
setFooterText() |
Set simple text footer (fallback) |
setPageNumbers() |
Enable or disable page numbers |
setFonts() |
Load custom fonts |
loadHtml() |
Load HTML content |
download() |
Download the PDF |
stream() |
Stream the PDF in browser |
Pdf::extend('custom', \App\Pdf\CustomPdfDriver::class); Pdf::driver('custom')->loadHtml('<h1>Custom Driver</h1>')->download();
❗Exceptions
Exception Class | When it’s Thrown |
---|---|
BinaryNotFoundException |
wkhtmltopdf binary missing (Snappy only) |
InvalidDriverException |
Driver not found in config or map |
PdfGenerationException |
General rendering failure |
RenderException |
Library-specific render issues |
✅ Requirements
- Laravel 6+
- PHP 7.1 or higher
- At least one supported PDF library installed (dompdf, mpdf, knplabs/knp-snappy, or setasign/fpdf)
License
This package is open-sourced software licensed under the MIT license.
Please note: This is a wrapper around the following libraries, each of which is maintained under its own license:
- dompdf/dompdf – LGPL-2.1-only
- mpdf/mpdf – GNU GPL v2
- knplabs/knp-snappy – MIT License
- setasign/fpdf – Free to use, non-copyleft
Make sure to review and comply with each library’s license if you use this package in your projects.