Laravel Snappy
Laravel Snappy stats
- Downloads
- 12.9M
- Stars
- 2,457
- Open Issues
- 37
- Forks
- 290
Snappy PDF/Image for Laravel
Snappy PDF/Image Wrapper for Laravel
The Snappy PDF/Image Wrapper for Laravel by Barryvdh is a convenient package that integrates the capabilities of Snappy (powered by wkhtmltopdf) directly into Laravel applications. This package allows developers to generate PDFs and images from HTML using the robust wkhtmltopdf library, enhancing document management capabilities within Laravel projects.
Key Features
- PDF and Image Generation: Convert HTML content directly into PDF files or images.
- Easy Configuration: Includes Laravel and Lumen service providers for simple integration.
- Facade Support: Facades for
SnappyPdfandSnappyImageprovide succinct syntax. - Flexible Content Handling: Load content from HTML strings, files, or views.
- Customization Options: Set paper size, orientation, and other PDF options.
- Testing Support: Includes functionality for faking PDF responses in tests.
Installation Steps
-
Install wkhtmltopdf:
- Download and install from wkhtmltopdf downloads or use as a composer dependency.
- Ensure necessary dependencies such as
libXrenderare installed.
-
Install the Laravel Package:
composer require barryvdh/laravel-snappy -
Register Service Provider and Facades (Laravel 5.5+ supports auto-discovery):
- For manual setup, add
Barryvdh\Snappy\ServiceProvider::classtoconfig/app.php. - Optionally, add
SnappyPdfandSnappyImagefacades for easier use.
- For manual setup, add
-
Publish Configuration:
php artisan vendor:publish --provider="Barryvdh\Snappy\ServiceProvider"Configure paths to the wkhtmltopdf binaries in
config/snappy.php. -
Lumen Configuration:
- Register the service provider and facades in
bootstrap/app.php.
- Register the service provider and facades in
Usage Examples
-
Generate and save a PDF:
$snappy = App::make('snappy.pdf');$snappy->generateFromHtml('<h1>Content</h1>', '/path/to/file.pdf'); -
Stream a PDF using a Facade:
return PDF::loadView('view.name', $data)->stream('document.pdf'); -
Chain methods for customization:
return PDF::loadHTML($html)->setPaper('a4')->setOrientation('landscape')->save('myfile.pdf');
Testing
Utilize the PDF facade’s fake method to simulate PDF generation in test environments, allowing assertions on output and behavior without actual file generation.
Conclusion
The Snappy PDF/Image Wrapper simplifies generating PDFs and images from HTML in Laravel, leveraging the power of wkhtmltopdf with minimal setup. Its integration with Laravel's service container and facades, along with comprehensive testing capabilities, makes it an essential tool for developers needing to handle document generation efficiently.