- How do I install wkhtmltopdf-amd64 in a Laravel project?
- Run `composer require h4cc/wkhtmltopdf-amd64` to install the package. The binary will be available at `vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf` or symlinked to `vendor/bin/wkhtmltopdf`. Ensure your Laravel app runs on Linux amd64, as this package only supports that architecture.
- Can I use this package with Laravel 9 or PHP 8.x?
- The package supports PHP 5.6+, but no active maintenance post-2018 means compatibility with PHP 8.x or Laravel 9 isn’t guaranteed. Test thoroughly in a Docker container using `ubuntu:20.04` or `debian:bullseye` to avoid issues with deprecated functions or binary dependencies.
- How do I generate PDFs from Blade templates in Laravel?
- Use Laravel’s `Process` facade or `shell_exec` to call the binary. For example, pass a Blade-rendered HTML string to `wkhtmltopdf` via a temporary file or pipe. Wrap this logic in a service class for reusability, like `PdfGeneratorService`, and inject it into controllers.
- What’s the best way to handle long-running PDF generation in Laravel?
- Offload PDF generation to Laravel Queues using a job class (e.g., `GeneratePdfJob`). This prevents timeouts and improves performance. Use Redis or database queues for async processing, and store the resulting PDFs in Laravel’s `Storage` facade (e.g., S3 or local filesystem).
- Does this package work in Docker or serverless environments?
- This package requires Linux amd64, so Docker is the best option for containerized deployments. Use a base image like `ubuntu:20.04` and install the binary via Composer. Serverless environments (e.g., AWS Lambda) are not ideal due to binary constraints, though you could use ECS or a custom runtime.
- Are there alternatives if I need ARM support or lighter PDF generation?
- For ARM support, consider `barryvdh/laravel-dompdf` (pure PHP, slower but lightweight) or `spatie/pdf-to-text` for text extraction. For headless Chrome, use Puppeteer via Node.js (e.g., with Laravel Octane or Horizon). If CSS accuracy isn’t critical, `dompdf` is a simpler alternative.
- How do I handle errors or crashes in wkhtmltopdf?
- Implement retry logic in your job or service class using Laravel’s `retryAfter` or a custom retry mechanism. Log failures with Laravel’s logging system and set up alerts (e.g., Slack or email) for repeated crashes. Fallback to a simpler PDF generator like `dompdf` if wkhtmltopdf consistently fails.
- Can I update the wkhtmltopdf binary without breaking my app?
- Pin the version in your `composer.json` to avoid unexpected updates. For critical environments, maintain a private patched version or use a wrapper script to manage binary updates. Test updates in a staging environment first, especially if you rely on specific wkhtmltopdf features.
- How do I ensure PDFs are generated consistently across environments?
- Containerize your app with Docker to standardize the environment. Use a base image with the exact binary version (e.g., `FROM ubuntu:20.04` + `composer require h4cc/wkhtmltopdf-amd64:0.12.4`). Validate PDF output (fonts, tables, CSS) in CI/CD pipelines by comparing against a golden master.
- What are the performance implications of using wkhtmltopdf in production?
- PDF generation can be resource-intensive, especially for large HTML payloads. Monitor memory/CPU usage and optimize by using async queues. For high-volume apps, benchmark wkhtmltopdf against alternatives like Puppeteer or consider splitting PDF generation into microservices with dedicated resources.