Installation:
composer require niklasravnsborg/laravel-pdf
For Laravel < 5.5, manually add the service provider and facade to config/app.php.
Publish Config:
php artisan vendor:publish --provider="niklasravnsborg\LaravelPdf\PdfServiceProvider"
(Note: The package is archived; verify config compatibility with your Laravel version.)
First Use Case:
Create a Blade view (resources/views/pdf/invoice.blade.php) and generate a PDF in a controller:
use PDF;
public function generateInvoice() {
$data = ['invoice' => Invoice::find(1)];
return PDF::loadView('pdf.invoice', $data)->stream('invoice.pdf');
}
View-Based PDFs:
loadView) for dynamic content.PDF::loadView('pdf.report', ['users' => User::all()])->stream();
Direct HTML Strings:
$html = '<h1>Hello</h1><p>World</p>';
PDF::loadHtml($html)->stream('dynamic.pdf');
File Storage:
$pdf = PDF::loadView('pdf.manual', ['data' => $manual]);
$pdf->save(storage_path('app/manual.pdf'));
Queueing for Async Generation:
GeneratePdfJob::dispatch($userId, 'receipt');
(Note: Requires custom job class with PDF::loadView() logic.)
Laravel Mail: Attach PDFs to emails:
Mail::send([], [], function($message) use ($pdf) {
$message->subject('Your PDF');
$message->attachData($pdf->output(), 'document.pdf');
});
Laravel Storage:
Use Storage::disk('s3')->put() to upload PDFs to cloud storage.
Dynamic Filenames: Use timestamps or model IDs:
$filename = "invoice_{$invoice->id}_{$invoice->created_at->format('Y-m-d')}.pdf";
PDF::loadView('pdf.invoice', ['invoice' => $invoice])->stream($filename);
Custom mPDF Config: Override defaults via config (e.g., paper size, margins):
PDF::loadView('pdf.document')->setOption('default', 'P')->setOption('format', 'A4');
Deprecated Package:
misterspelik/laravel-pdf for active maintenance.mPDF Version Conflicts:
mpdf/mpdf version. Conflicts may arise with other packages using mPDF.composer require mpdf/mpdf:^8.0 and configure the package to use the global mPDF instance.Memory Limits:
memory_limit.memory_limit in php.ini or chunk data processing.Font Issues:
setFontDir() or use @font-face in CSS (limited support).Caching:
->stream() with unique filenames or disable caching for PDF routes.Check mPDF Logs:
Enable mPDF debugging in config/laravel-pdf.php:
'debug' => true,
Logs appear in Laravel’s storage/logs/laravel.log.
Inspect HTML: Save the generated HTML to a file before PDF conversion to debug rendering:
file_put_contents(storage_path('debug.html'), $pdf->getHtml());
Common Errors:
composer dump-autoload.loadView vs. loadHtml).Custom mPDF Instance: Override the default mPDF instance in a service provider:
$this->app->bind('mpdf', function() {
$mpdf = new \Mpdf\Mpdf(['mode' => 'utf-8', 'format' => 'A4']);
return $mpdf;
});
Middleware for PDFs: Add headers or auth checks:
Route::get('/pdf', function() {
return PDF::loadView('pdf.document')->stream();
})->middleware('auth');
PDF Metadata: Set document properties (title, author) via mPDF:
PDF::loadView('pdf.document')
->setOption('title', 'My Document')
->setOption('author', 'Laravel App');
Barcode/QR Codes:
Use mPDF’s extensions (e.g., mpdf/barcodes) for dynamic codes:
$pdf->setOption('barcode_width', '0.2');
$pdf->setOption('barcode_height', '25');
How can I help you explore Laravel packages today?