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 v4.0.0 is released image

Laravel Debugbar v4.0.0 is released

Read article
Laravel Debugbar Gets a Big UI Update image

Laravel Debugbar Gets a Big UI Update

Read article
Celebian logo

Celebian

Celebian is a social media marketing agency specializing in helping their clients go viral on TikTok. Whether you're looking to reach a bigger audience or gain more Tiktok followers, likes, and views, they've got you covered.

Celebian
Statamic logo

Statamic

The drop-in ready Laravel CMS you’re been waiting for. Go full-stack or headless, flat file or database – it’s up to you.

Statamic
Tinkerwell logo

Tinkerwell

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

Tinkerwell
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
LoadForge logo

LoadForge

Scalable load testing for web apps & APIs. Simulate real-world traffic and identify breaking points and performance limits with powerful, scalable load tests designed for Laravel.

LoadForge
Tighten logo

Tighten

We help companies turn great ideas into amazing apps, products, and services.

Tighten