Learn how to generate multi-page PDFs with consistent headers and footers using SnappyPDF (wkhtmltopdf) in Laravel. Step-by-step guide with real examples and tips.
Generating PDFs is a common requirement in web applications whether you're building invoices, certificates, reports, or printable forms. While DomPDF is beginner-friendly, it falls short in terms of CSS compatibility and performance on larger documents.
That's where SnappyPDF comes in. Built on wkhtmltopdf, this Laravel package provides powerful PDF generation capabilities with full CSS support, fast rendering, and smooth handling of multi-page documents with support for consistent headers and footers.
In this guide, you'll learn how to:
Install SnappyPDF and dependencies
Set up Blade views for dynamic PDF content
Add custom headers and footers
Generate multi-page PDFs with full control
For Windows:
Download the installer from:
https://wkhtmltopdf.org/downloads.html
Make sure to choose the patched Qt version, which is required for headers/footers.
After installation, add (if not added) the binary path to your system environment variables (e.g., C:\Program Files\bin
).
For Ubuntu:
sudo apt install wkhtmltopdf
Note: Ensure it includes the patched Qt for header/footer support.
Run the following Composer command:
composer require barryvdh/laravel-snappy
If you're using Laravel 8 & 9 or earlier, you need to register the service provider manually in config/app.php
:
'providers' => [
Barryvdh\Snappy\ServiceProvider::class,
],
'aliases' => [
'PDF' => Barryvdh\Snappy\Facades\SnappyPdf::class,
],
Then publish the config file:
php artisan vendor:publish --provider="Barryvdh\Snappy\ServiceProvider"
This will generate a config/snappy.php
file where you can tweak default settings.
We'll be creating three views:
resources/views/pdf/main.blade.php
→ PDF content
resources/views/pdf/header.blade.php
→ Header
resources/views/pdf/footer.blade.php
→ Footer
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Multi-page PDF</title>
<style>
body { font-family: sans-serif; margin: 0; padding: 0; }
.content { margin: 100px 50px 100px 50px; }
.page-break { page-break-after: always; }
</style>
</head>
<body>
<div class="content">
<h1>Title</h1>
<p>Content for the first page...</p>
</div>
<div class="page-break"></div>
<div class="content">
//content
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: sans-serif; font-size: 12px; text-align: center; }
</style>
</head>
<body>
<div>
<strong>Document</strong> | Page Header
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: sans-serif; font-size: 12px; text-align: center; }
</style>
</head>
<body>
<div>
Page [page] of [topage] | © {{ date('Y') }} Company Name
</div>
</body>
</html>
In your controller, use the following method to generate the PDF:
php
use Barryvdh\Snappy\Facades\SnappyPdf as SPDF;
public function generatePdf()
{
$pdf = SPDF::loadView('pdf.main')
->setPaper('a4')
->setOption('margin-top', '25mm')
->setOption('margin-bottom', '20mm')
->setOption('header-html', view('pdf.header')->render())
->setOption('header-spacing', 5)
->setOption('footer-html', view('pdf.footer')->render())
->setOption('footer-spacing', 5);
return $pdf->download('report.pdf');
}
Add this to routes/web.php
:
Route::get('/generate-pdf', [YourController::class, 'generatePdf']);
header-html => Sets HTML content for the header (every page).
footer-html => Sets HTML content for the footer (every page).
margin-top => Reserves space for the header.
margin-bottom => Reserves space for the footer.
page-break-after => Forces.content to start on a new page.
[page], [topage] => Built-in placeholders for current and total pages.
You can pass dynamic data like this:
SPDF::loadView('pdf.main', ['name' => 'Muammad Ishaq']);
And in your Blade view:
<p>Generated for: {{ $name }}</p>
SnappyPDF, powered by wkhtmltopdf, is an excellent tool for generating production-grade multi-page PDFs in Laravel. With support for HTML, CSS, and repetitive headers/footers, it's a powerful alternative to DomPDF.
Whether you're building invoices, certificates, or reports, SnappyPDF gives you control, performance, and flexibility.