LaravelPackages.net
Acme Inc.
Toggle sidebar
spatie/laravel-pdf

Create PDFs in Laravel apps

4.615.509
1.028
2.12.0
About spatie/laravel-pdf

spatie/laravel-pdf is a Laravel package for create pdfs in laravel apps. It currently has 1.028 GitHub stars and 4.615.509 downloads on Packagist (latest version 2.12.0). Install it with composer require spatie/laravel-pdf. Discover more Laravel packages by spatie or browse all Laravel packages to compare alternatives.

Last updated

Logo for laravel-pdf

Create PDFs in Laravel apps

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This package provides a simple way to create PDFs in Laravel apps. It supports multiple drivers: Browsershot (Chromium), Gotenberg (Docker-based), Cloudflare Browser Run, WeasyPrint (Python-based), DOMPDF (pure PHP), and chrome-php/chrome (Chromium). You can use modern CSS features like grid and flexbox with the Chromium-based drivers, use WeasyPrint for excellent CSS Paged Media support, or choose DOMPDF for a zero-dependency setup.

Here's a quick example:

use Spatie\LaravelPdf\Facades\Pdf;

Pdf::view('pdfs.invoice', ['invoice' => $invoice])
    ->format('a4')
    ->save('invoice.pdf')

This will render the Blade view pdfs.invoice with the given data and save it as a PDF file.

You can also return the PDF as a response from your controller:

use Spatie\LaravelPdf\Facades\Pdf;

class DownloadInvoiceController
{
    public function __invoke(Invoice $invoice)
    {
        return Pdf::view('pdfs.invoice', ['invoice' => $invoice])
            ->format('a4')
            ->name('your-invoice.pdf');
    }
}

You can use also test your PDFs:

use Spatie\LaravelPdf\Facades\Pdf;

it('can render an invoice', function () {
    Pdf::fake();

    $invoice = Invoice::factory()->create();

    $this->get(route('download-invoice', $invoice))
        ->assertOk();
        
    Pdf::assertRespondedWithPdf(function (PdfBuilder $pdf) {
        return $pdf->contains('test');
    });
});

Laravel Boost

This package ships with a Laravel Boost skill. After installing the package, run php artisan boost:install to register the skill. This will help AI agents in your project generate correct PDF code.

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Documentation

All documentation is available on our documentation site.

Testing

For running the testsuite, you'll need to have Puppeteer installed. Pleaser refer to the Browsershot requirements here. Usually npm -g i puppeteer will do the trick.

Additionally, you'll need the pdftotext CLI which is part of the poppler-utils package. More info can be found in in the spatie/pdf-to-text readme. Usually brew install poppler-utils will suffice.

Finally run the tests with:

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

Star History Chart