Skip to main content
Back to packages
1,000 GitHub stars

spatie/laravel-pdf

View on GitHub

Description

Create PDF files in Laravel apps

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');
});
});

Related Content on Laravel Daily

Video
Video

Recent Courses on Laravel Daily

Laravel 13 Eloquent: Expert Level

41 lessons
1 h 34 min

How to Structure Laravel 13 Projects

16 lessons
1 h 32 min read

[FREE] Laravel 13 for Beginners: 3 Demo Projects

5 lessons
29 min

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.