Get expert guidance in a few days with a Laravel code review

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
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
Shift logo

Shift

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

Shift
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Lucky Media logo

Lucky Media

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

Lucky Media
Curotec logo

Curotec

World class Laravel experts with GenAI dev skills. LATAM-based, embedded engineers that ship fast, communicate clearly, and elevate your product. No bloat, no BS.

Curotec
Tinkerwell logo

Tinkerwell

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

Tinkerwell