pontedilana/php-weasyprint
Laravel-friendly PHP wrapper for WeasyPrint to render HTML/CSS into high-quality PDFs (and images) via a simple API. Ideal for invoices, reports, and templated documents, with options for assets, headers/footers, and configuration.
Installation
composer require pontedilana/php-weasyprint
Ensure weasyprint is installed system-wide (check official docs for OS-specific instructions).
Basic Usage Generate a PDF from HTML:
use Pontedilana\WeasyPrint\WeasyPrint;
$html = '<h1>Hello, PDF!</h1><p>Generated with WeasyPrint.</p>';
$pdf = WeasyPrint::fromHtml($html)->toPdf();
file_put_contents('output.pdf', $pdf);
First Use Case Convert a Blade view to PDF in a Laravel controller:
use Pontedilana\WeasyPrint\WeasyPrint;
use Illuminate\Support\Facades\View;
$html = View::make('pdf.template', ['data' => $model])->render();
$pdf = WeasyPrint::fromHtml($html)->toPdf();
return response($pdf, 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'inline; filename="document.pdf"'
]);
From URLs
$pdf = WeasyPrint::fromUrl('https://example.com')->toPdf();
With CSS Styling
$pdf = WeasyPrint::fromHtml($html)
->setCss('path/to/styles.css')
->toPdf();
Streaming Responses
return WeasyPrint::fromHtml($html)
->streamPdf('document.pdf');
Dynamic Content Loop through models and generate PDFs in batches:
foreach ($models as $model) {
$html = View::make('pdf.template', compact('model'))->render();
$pdf = WeasyPrint::fromHtml($html)->toPdf();
Storage::put("pdfs/{$model->id}.pdf", $pdf);
}
Integration with Queues Dispatch a job for async PDF generation:
GeneratePdfJob::dispatch($html, 'output.pdf');
Service Provider Binding Bind the package for dependency injection:
$this->app->bind(WeasyPrint::class, function ($app) {
return new WeasyPrint(config('weasyprint.defaults'));
});
Config Overrides Publish the config file:
php artisan vendor:publish --provider="Pontedilana\WeasyPrint\WeasyPrintServiceProvider"
Customize config/weasyprint.php for default options (e.g., timeout, CSS paths).
Middleware for Authenticated PDFs Protect PDF endpoints:
Route::get('/pdf/{id}', function ($id) {
return WeasyPrint::fromHtml($html)->streamPdf("document_{$id}.pdf");
})->middleware('auth');
WeasyPrint System Dependency
Command 'weasyprint' not found.brew install weasyprint on macOS or apt-get install weasyprint on Ubuntu).weasyprint --version in terminal.Memory Limits
Allowed memory size exhausted for large HTML.ini_set('memory_limit', '1G')) or optimize HTML/CSS.CSS Inline vs. External
$pdf = WeasyPrint::fromHtml($html)
->setCss('https://example.com/styles.css')
->toPdf();
Timeouts
'timeout' => 30, // seconds
Log WeasyPrint Output Enable verbose logging in config:
'verbose' => true,
Check Laravel logs for WeasyPrint CLI output.
Test with Minimal HTML
Isolate issues by testing with a simple <div> before complex templates.
Custom Headers/Footers
Use CSS @page rules:
@page {
size: A4;
margin: 2cm;
}
header {
position: fixed;
top: 0;
left: 0;
right: 0;
height: 1cm;
}
Post-Processing
Modify PDFs after generation with libraries like setasign/fpdf:
$pdfContent = WeasyPrint::fromHtml($html)->toPdf();
$pdf = \FPDF::parse($pdfContent);
// Add watermark, etc.
Caching Cache generated PDFs for static content:
$pdf = Cache::remember("pdf_{$key}", now()->addHours(1), function () {
return WeasyPrint::fromHtml($html)->toPdf();
});
Use ->toSnapshot() for Images
Generate screenshots of URLs:
$image = WeasyPrint::fromUrl('https://example.com')->toSnapshot();
file_put_contents('screenshot.png', $image);
Laravel Mix Integration Compile CSS for WeasyPrint in your build process:
// webpack.mix.js
mix.postCss('resources/css/pdf.css', 'public/css', [
require('postcss-import'),
require('tailwindcss')
]);
How can I help you explore Laravel packages today?