- How do I install and set up PhpWeasyPrint in a Laravel project?
- Run `composer require pontedilana/php-weasyprint` to install the package. WeasyPrint (Python-based, v60+) must also be installed on your server. Bind the `Pdf` class in Laravel’s service container (e.g., `app()->singleton(Pdf::class, fn() => new Pdf(config('weasyprint.binary_path')))`) to use dependency injection.
- Can I use PhpWeasyPrint to generate PDFs from Blade templates in Laravel?
- Yes. Render your Blade template to HTML (e.g., `$html = view('invoice')->render()`) and pass it to `getOutput($html)` or `generateFromHtml()`. Stream the output directly in a route or save it to disk.
- What Laravel versions does PhpWeasyPrint support?
- PhpWeasyPrint works with Laravel 8.x, 9.x, and 10.x. It relies on Symfony Process (v6.2+), which is compatible with these Laravel versions. Test thoroughly if using older Laravel 7.x with custom configurations.
- How do I handle timeouts for PDF generation in production?
- The default 10-second timeout may need adjustment for complex PDFs. Use `$pdf->setTimeout(30)` to increase it or disable it entirely (`$pdf->disableTimeout()`) for queued jobs. Monitor process execution time in production to avoid hanging requests.
- Is PhpWeasyPrint a drop-in replacement for KnpLabs/snappy in Laravel?
- Yes, it implements the same `GeneratorInterface` as Snappy, so you can replace Snappy with PhpWeasyPrint in Laravel apps using packages like `barryvdh/laravel-dompdf` or custom PDF logic. Check the [differences section](https://github.com/pontedilana/php-weasyprint#differences-with-snappy) for WeasyPrint-specific limitations.
- How do I configure WeasyPrint options like CSS or attachments?
- Use `$pdf->setOption()` to pass WeasyPrint CLI arguments. For example, `$pdf->setOption('stylesheet', ['/path/to/style.css'])` or `$pdf->setOption('attachment', ['/path/to/image.png'])` to include stylesheets or attachments. Run `weasyprint -h` for a full list of supported options.
- Can I generate PDFs from remote URLs with PhpWeasyPrint?
- Yes. Use `$pdf->getOutput('https://example.com')` to fetch and render a URL directly. By default, it follows redirects, but you can disable this with `$pdf->setOption('no-http-redirects', true)` to mitigate SSRF risks.
- What are the infrastructure requirements for PhpWeasyPrint?
- You need Python 3.8+ and WeasyPrint (≥60.0) installed on your server. For Docker, include `python3-weasyprint` in your image. Avoid hardcoding the binary path; use environment variables (e.g., `WEASYPRINT_BINARY=/usr/local/bin/weasyprint`) or Laravel config files.
- How do I handle security risks like CSS/JS injection in dynamic HTML?
- Sanitize user-provided HTML/CSS before passing it to PhpWeasyPrint. Use Laravel’s `Purifier` package or a whitelist to strip unsafe tags. For metadata, avoid exposing sensitive data in PDF attachments or XMP tags.
- Are there alternatives to PhpWeasyPrint for PDF generation in Laravel?
- Yes. Consider `barryvdh/laravel-dompdf` (HTML-to-PDF with Dompdf), `spatie/laravel-pdf` (wrapper for Dompdf/Snappy), or `mike42/laravel-puppeteer` (headless Chrome). PhpWeasyPrint excels for CSS3-heavy PDFs but requires Python/WeasyPrint setup.