laravel-snappy

repository·master·Indexed 25 days ago

https://github.com/barryvdh/laravel-snappy

A Laravel ServiceProvider wrapper for the KnpLabs/snappy library that provides an interface for generating PDFs and images using wkhtmltopdf and wkhtmltoimage. It supports loading content from HTML strings, files, or Laravel Blade views, and includes facades for PDF and image generation, as well as testing utilities like PDF::fake() for intercepting generation and performing assertions.

Tokens
2.8K
Snippets
4
Records
23
Agent score
83%

What's inside laravel-snappy

  1. Configure wkhtmltopdf binaries for Vagrant

    master

    If you are using Vagrant and installed binaries via Composer, move them to a path that is not in a synced folder (like /usr/local/bin/) to prevent error 126, and make them executable.

    cp vendor/h4cc/wkhtmltoimage-amd64/bin/wkhtmltoimage-amd64 /usr/local/bin/
    cp vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64 /usr/local/bin/
    
    chmod +x /usr/local/bin/wkhtmltoimage-amd64 
    chmod +x /usr/local/bin/wkhtmltopdf-amd64
  2. Configure Lumen integration

    master

    In bootstrap/app.php, register the ServiceProvider and optionally alias the facades:

    $app->register(Barryvdh\Snappy\LumenServiceProvider::class);
    
    class_alias('Barryvdh\Snappy\Facades\SnappyPdf', 'PDF');
    class_alias('Barryvdh\Snappy\Facades\SnappyImage', 'SnappyImage');

    To customize configuration, copy /vendor/barryvdh/laravel-snappy/config/snappy.php to your /config folder.

  3. Install wkhtmltopdf/wkhtmltoimage

    master

    Before using this package, you must install the wkhtmltopdf or wkhtmltoimage binaries on your system.

    Options:

    1. Download binaries directly from wkhtmltopdf.org.
    2. Install via Composer dependencies (e.g., h4cc/wkhtmltopdf-amd64).

    Note: Some systems may require manual installation of dependencies like libXrender.

  4. Configure Laravel integration

    master

    For Laravel 5.5+, package auto-discovery is supported. However, if you use laravel-dompdf, you may need to manually register the ServiceProvider and Facades in config/app.php.

    Register ServiceProvider:

    Barryvdh\Snappy\ServiceProvider::class,

    Register Facades (Optional):

    'PDF' => Barryvdh\Snappy\Facades\SnappyPdf::class,
    'SnappyImage' => Barryvdh\Snappy\Facades\SnappyImage::class,

    Publish Configuration:

    php artisan vendor:publish --provider="Barryvdh\Snappy\ServiceProvider"
  5. Configure Snappy binary paths

    master

    Update config/snappy.php with the correct path to your wkhtmltopdf or wkhtmltoimage binaries.

    Example for Composer-loaded binaries:

    'binary' => base_path('vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64'),

    Example for Vagrant/System paths:

    'binary'  => '/usr/local/bin/wkhtmltopdf-amd64',

    Example for Windows (requires double quotes):

    'binary' => '"C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf"',
  6. Generate PDFs and Images

    master

    You can generate PDFs or images using the App container, the wrapper, or the Facade. You can load HTML strings, files, or views.

    Using the Facade (Recommended):

    // From a view
    return PDF::loadView('pdf.invoice', $data)->download('invoice.pdf');
    
    // From HTML string with options
    return PDF::loadHTML($html)
        ->setPaper('a4')
        ->setOrientation('landscape')
        ->setOption('margin-bottom', 0)
        ->inline('myfile.pdf');
    
    // From a file
    return PDF::loadFile('http://www.github.com')->inline('github.pdf');

    Using the App Container (Direct Snappy instance):

    $snappy = App::make('snappy.pdf');
    $html = '<h1>Bill</h1>';
    $snappy->generateFromHtml($html, '/tmp/bill-123.pdf');
    $pdf = App::make('snappy.pdf.wrapper');
    $pdf->loadHTML('<h1>Test</h1>');
    return $pdf->inline();
  7. Test PDF generation with PDF::fake()

    master

    Instead of mocking, use PDF::fake() to intercept PDF generation and perform assertions on the views and content used.

    public function testPrintOrderShipping()
    {
        PDF::fake();
        
        // Perform order shipping...
        
        PDF::assertViewIs('view-pdf-order-shipping');
        PDF::assertSee('Name');
    }
  8. Configure Snappy PDF and Image settings

    master

    The package uses a configuration file located at config/snappy.php. You can publish this file to your application's config directory using the config tag.

    Settings are divided into pdf and image groups. Each group supports the following keys:

    • enabled: (bool) Enables the PDF or Image service.
    • binary: (string) The path to the wkhtmltopdf or wkhtmltoimage binary. Defaults to /usr/local/bin/wkhtmltopdf for PDF and /usr/local/bin/wkhtmltoimage for Image.
    • options: (array) Additional options to pass to the binary.
    • env: (array) Environment variables to pass to the binary.
    • timeout: (mixed) A timeout value. If set to a non-false value, it will be applied to the service.
  9. Reference: PDF Facade Assertions

    master

    When using PDF::fake(), use the following assertions to verify PDF generation:

    • PDF::assertViewIs($value)
    • PDF::assertViewHas($key, $value = null)
    • PDF::assertViewHasAll(array $bindings)
    • PDF::assertViewMissing($key)
    • PDF::assertSee($value)
    • PDF::assertSeeText($value)
    • PDF::assertDontSee($value)
    • PDF::assertDontSeeText($value)
    • PDF::assertFileNameIs($value)
  10. Configure PDF output (Save, Download, or Inline)

    master

    Once content is loaded, you can produce the PDF in several formats:

    • Save to file: Use save($filename, $overwrite = false) to write the PDF to the local filesystem.
    • Download: Use download($filename = 'document.pdf') to return an Illuminate\Http\Response that triggers a file download in the user's browser.
    • Inline display: Use inline($filename = 'document.pdf') to return an Illuminate\Http\Response that displays the PDF directly in the browser.
    • Get raw string: Use output() to return the raw PDF binary data as a string.