- How do I install spipu/html2pdf in a Laravel project?
- Run `composer require spipu/html2pdf` in your project root. Ensure PHP 7.2+ and extensions `gd` and `mbstring` are enabled. No additional Laravel-specific setup is required beyond Composer installation.
- Can I use this package to generate PDFs from Laravel Blade templates?
- Yes. Pass your Blade-generated HTML to the `Html2Pdf` class, ensuring the HTML is optimized for PDF rendering (e.g., avoid complex CSS or WYSIWYG-generated markup). Use `@include('pdf.template')` with dynamic data.
- Does spipu/html2pdf support Laravel Queues for async PDF generation?
- Absolutely. Wrap PDF generation in a Laravel job (e.g., `GeneratePdfJob`) and dispatch it via the queue system. This is critical for performance, especially for user-triggered actions like invoice downloads.
- What Laravel versions does spipu/html2pdf support?
- The package itself requires PHP 7.2–8.4, but it integrates seamlessly with all modern Laravel versions (6.x–11.x). No Laravel-specific dependencies exist, so version compatibility is broad.
- How do I handle dynamic data (e.g., user-specific invoices) in PDF templates?
- Use Laravel’s service container to bind `Html2Pdf` and pass dynamic data via Blade variables (e.g., `$invoice->amount`). Store templates in `resources/views/pdf/` and include them with `@include('pdf.invoice', ['data' => $invoice])`.
- Are there performance concerns with spipu/html2pdf in production?
- Yes, PDF generation can be resource-intensive. Mitigate this by using Laravel Queues + Redis for async processing, caching pre-generated templates, or offloading to a microservice. Test under load with tools like Laravel Dusk.
- What if Ghostscript or libpng dependencies fail in production?
- Use Docker (e.g., Laradock) to isolate dependencies. Avoid conflicts with `dompdf` by ensuring only one HTML-to-PDF package is installed. For system installs, verify Ghostscript and `libpng` are properly configured in your production environment.
- Can I use spipu/html2pdf for batch processing (e.g., nightly reports)?
- Yes, but design for scalability. Queue batch jobs with `GeneratePdfJob::dispatch($data)` and process them asynchronously. Monitor progress via Laravel Horizon. For large volumes, consider a dedicated microservice.
- How do I test PDF generation in Laravel?
- Unit-test with Mockery to mock the `Html2Pdf` class. For end-to-end tests, use Laravel Dusk to verify PDF output or tools like `pdf-diff` to compare generated files against expected templates. Test edge cases like Unicode or complex tables.
- What are the alternatives to spipu/html2pdf for Laravel?
- Consider `dompdf` (lighter, no Ghostscript), `wkhtmltopdf` (browser-based, higher quality), or `barryvdh/laravel-dompdf` (Laravel wrapper for dompdf). Choose based on dependency constraints, performance needs, and output quality requirements.