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
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
Shift logo

Shift

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

Shift
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Blastup logo

Blastup

Blastup provides social media enhancement services including buying Instagram likes, followers, and views, with features like instant delivery and a variety of packages to suit different needs.

Blastup