Installation:
Add the bundle and dependencies to composer.json as per the README, then run:
composer update
Enable the bundle in config/bundles.php (Symfony) or AppKernel.php (Laravel via Symfony bridge):
Dlin\SnappyBundle\DlinSnappyBundle::class => ['all' => true],
Configuration: Publish the default config:
php artisan vendor:publish --tag=snappy-config
Update config/snappy.php with your wkhtmltopdf binary path (e.g., /usr/local/bin/wkhtmltopdf).
First Use Case:
Inject the SnappyPdf service into a controller or command:
use Dlin\SnappyBundle\Service\SnappyPdf;
class InvoiceController extends Controller
{
public function generatePdf(SnappyPdf $snappyPdf)
{
$html = '<h1>Invoice #123</h1><p>Generated at '.now().'</p>';
$pdf = $snappyPdf->getPdf($html);
return response($pdf, 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'inline; filename="invoice.pdf"'
]);
}
}
PDF Generation from HTML:
$snappyPdf->getPdf($htmlString, [
'orientation' => 'Portrait',
'margin-top' => '10mm',
'margin-right' => '10mm',
'margin-bottom' => '10mm',
'margin-left' => '10mm',
]);
PDF from URL:
$snappyPdf->getPdfFromUrl('https://example.com/report', [
'encoding' => 'UTF-8',
'quiet' => true,
]);
Image Thumbnails:
Use the SnappyImage service (if available in the bundle or via KnpLabs/snappy):
$snappyImage->getImage($html, $targetPath, [
'width' => 800,
'height' => 600,
]);
Laravel-Specific:
Bind the Symfony service to Laravel’s container in AppServiceProvider:
$this->app->bind('snappy.pdf', function ($app) {
return $app->make('snappy.pdf.writer');
});
Then inject snappy.pdf into controllers.
Queueable Jobs:
Offload PDF generation to a queue (e.g., snappy:generate-pdf job) to avoid timeouts:
use Dlin\SnappyBundle\Service\SnappyPdf;
class GeneratePdfJob implements ShouldQueue
{
public function handle(SnappyPdf $snappyPdf)
{
$pdf = $snappyPdf->getPdf($this->html);
Storage::put('pdfs/invoice.pdf', $pdf);
}
}
Twig Integration: Create a Twig extension to generate PDFs from templates:
class SnappyExtension extends \Twig_Extension
{
public function getFunctions()
{
return [
new \Twig_SimpleFunction('generate_pdf', [$this->snappyPdf, 'getPdf']),
];
}
}
Usage in Twig:
{{ generate_pdf(html_content) }}
Binary Path Issues:
Command not found or Failed to execute command.wkhtmltopdf is installed and the path in config/snappy.php is correct.
which wkhtmltopdf # Linux/Mac
where wkhtmltopdf # Windows
wkhtmltopdf-i386 package if on 32-bit systems.Memory Limits:
Out of memory or timeouts for large HTML.memory_limit in php.ini) or optimize HTML/CSS (e.g., inline styles, avoid heavy JS).Encoding Problems:
$snappyPdf->getPdf($html, ['encoding' => 'UTF-8']);
Deprecated Dependencies:
spatie/pdf.Log Output:
Enable verbose logging in config/snappy.php:
'logging' => true,
Check storage/logs/laravel.log for wkhtmltopdf command output.
Test Locally:
Use a local wkhtmltopdf binary first to avoid deployment issues. Test with:
wkhtmltopdf --version
Custom Commands:
Extend the bundle to add custom wkhtmltopdf flags:
// In a service or config override
$snappyPdf->setOption('custom-header', ['Accept-Language' => 'en-US']);
Event Listeners: Listen for PDF generation events (if the bundle supports them) to log or modify output:
// Example (hypothetical)
$snappyPdf->addListener(function ($event) {
if ($event->getStatus() === 'generated') {
Log::info('PDF generated', ['size' => $event->getSize()]);
}
});
Fallback for Missing Binary:
Implement a fallback (e.g., DOMPDF) if wkhtmltopdf fails:
try {
return $snappyPdf->getPdf($html);
} catch (\Exception $e) {
$fallbackPdf = PDF::loadHTML($html)->output();
return $fallbackPdf;
}
SnappyPdf.afterCommit() or queue batching to prevent server overload.How can I help you explore Laravel packages today?