Jump24 - Laravel Developers that Click into Place. Never outsourced. Never offshored. Always exceptional.

Laravel Snappy

barryvdh/laravel-snappy image

Laravel Snappy stats

Downloads
12.9M
Stars
2,457
Open Issues
37
Forks
290

View on GitHub →

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 SnappyPdf and SnappyImage provide 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

  1. Install wkhtmltopdf:

    • Download and install from wkhtmltopdf downloads or use as a composer dependency.
    • Ensure necessary dependencies such as libXrender are installed.
  2. Install the Laravel Package:

    composer require barryvdh/laravel-snappy
  3. Register Service Provider and Facades (Laravel 5.5+ supports auto-discovery):

    • For manual setup, add Barryvdh\Snappy\ServiceProvider::class to config/app.php.
    • Optionally, add SnappyPdf and SnappyImage facades for easier use.
  4. Publish Configuration:

    php artisan vendor:publish --provider="Barryvdh\Snappy\ServiceProvider"

    Configure paths to the wkhtmltopdf binaries in config/snappy.php.

  5. Lumen Configuration:

    • Register the service provider and facades in bootstrap/app.php.

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.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Barryvdh Laravel Snappy Related Articles

Laravel Debugbar Gets a Big UI Update image

Laravel Debugbar Gets a Big UI Update

Read article
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $3200/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy