dreadnip/chrome-pdf-bundle
Symfony bundle that uses chrome-php/chrome to render HTML to PDF via Chrome/Chromium. Configure the Chrome binary via env, then generate PDFs from HTML with the PdfGenerator service or customize via the BrowserFactory for advanced options.
Installation: Add the bundle via Composer in your Laravel project (using Symfony bridge if needed):
composer require dreadnip/chrome-pdf-bundle
Register the bundle in config/bundles.php:
return [
// ...
Dreadnip\ChromePdfBundle\ChromePdfBundle::class => ['all' => true],
];
Configure Chrome Path:
Set the Chrome/Chromium binary path in .env:
CHROME_BINARY="/usr/bin/chromium-browser" # Linux
CHROME_BINARY="C:\Program Files\Google\Chrome\Application\chrome.exe" # Windows
First Use Case: Generate a PDF from a Twig template in a controller:
use Dreadnip\ChromePdfBundle\Service\PdfGenerator;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
public function generatePdf(PdfGenerator $pdfGenerator)
{
$html = $this->render('pdf_template.twig');
$path = $pdfGenerator->generate($html, 'storage/app/public/report.pdf');
return new BinaryFileResponse($path);
}
PdfGenerator for simple use cases and BrowserFactory for advanced configurations.@ChromePdf/base.html.twig for consistent PDF layouts.Basic PDF Generation:
Use PdfGenerator for straightforward HTML-to-PDF conversion:
$pdfGenerator->generate($html, 'path/to/file.pdf');
Dynamic Templates: Combine with Laravel’s Blade or Twig to render dynamic content:
$html = view('invoices.pdf', ['invoice' => $invoice])->render();
$pdfGenerator->generate($html, 'storage/invoices/invoice_'.$invoice->id.'.pdf');
Custom Print Options: Pass Chrome-specific print options for fine-grained control:
$printOptions = [
'printBackground' => true,
'headerTemplate' => '<div>Custom Header</div>',
'footerTemplate' => '<div>Page <span class="pageNumber"></span></div>',
];
$pdfGenerator->generate($html, 'file.pdf', $printOptions);
Browser Configuration: Adjust Chrome’s behavior (e.g., headless mode, timeout):
$browserOptions = [
'headless' => false, // Debugging
'args' => ['--no-sandbox', '--disable-setuid-sandbox'],
'timeout' => 60000,
];
$pdfGenerator->generate($html, 'file.pdf', [], $browserOptions);
Batch Processing: Queue PDF generation jobs (e.g., using Laravel Queues) for large datasets:
PdfJob::dispatch($userId, $template)->onQueue('pdfs');
// In PdfJob.php
public function handle()
{
$html = view('reports.dashboard', ['user' => $this->userId])->render();
$pdfGenerator->generate($html, "storage/reports/user_{$this->userId}.pdf");
}
Invoice Generation:
$pdfPath = $pdfGenerator->generate($html, 'temp/invoice.pdf');
Mail::send([], [], function ($message) use ($pdfPath) {
$message->attach($pdfPath);
});
Report Scheduling:
$schedule->command('reports:generate')->daily();
php artisan reports:generate
Dynamic Dashboards:
$html = view('dashboards.pdf', ['data' => $dashboardData])->render();
$pdfGenerator->generate($html, 'temp/dashboard.pdf', [
'javascriptEnabled' => true,
]);
Laravel-Specific:
Storage facade to manage PDF paths:
use Illuminate\Support\Facades\Storage;
$path = Storage::path('pdfs/report.pdf');
Response helpers for seamless downloads:
return response()->download($path, 'report.pdf');
Symfony Bridge:
spatie/laravel-symfony-bundle, ensure the bundle is registered before Laravel’s service providers.Testing:
PdfGenerator in unit tests:
$mock = Mockery::mock(PdfGenerator::class);
$mock->shouldReceive('generate')->andReturn('fake/path.pdf');
$this->app->instance(PdfGenerator::class, $mock);
CI/CD:
- name: Install Chrome
run: |
sudo apt-get update
sudo apt-get install -y chromium-browser
Chrome Binary Path:
.env and log errors:
if (!file_exists($this->config['chrome_path'])) {
Log::error('Chrome binary not found at: '.$this->config['chrome_path']);
}
Headless Mode:
headless: true).headless: false for development:
$browserOptions = ['headless' => env('APP_DEBUG') ? false : true];
Timeouts:
$pdfGenerator->generate($html, 'file.pdf', [], [], 120000); // 120s
Resource Limits:
$pdfGenerator->generate($html, 'file.pdf', [], ['args' => ['--single-process']]);
CSS/JS Dependencies:
localFileSystem or userDataDir in browser options:
$browserOptions = [
'userDataDir' => sys_get_temp_dir().'/chrome-pdf-user-data',
];
Logs:
args:
$browserOptions = [
'args' => ['--headless=new', '--disable-gpu', '--log-level=DEBUG'],
];
tail -f storage/logs/laravel.log | grep -i chrome
Network Issues:
$browserOptions = [
'args' => ['--no-zygote', '--disable-dev-shm-usage', '--disable-gpu'],
];
Memory Dumps:
$browserOptions = [
'args' => ['--js-flags="--expose-gc"'],
];
Environment Variables:
CHROME_BINARY_LIVE="/usr/bin/chromium-live"
CHROME_BINARY_STAGING="/usr/bin/chromium-staging"
env() helper:
$chromePath = env('CHROME_BINARY_'.config('app.env'));
Default Options:
config/packages/chrome_pdf.yaml:
chrome_pdf:
default_options:
printBackground: true
timeout: 60000
Symfony vs. Laravel:
config/autoload.php:
$container->setParameter('chrome_pdf.chrome_path', env('CHROME_BIN
How can I help you explore Laravel packages today?