- How do I install knp-snappy in a Laravel project?
- Run `composer require knplabs/knp-snappy` for the core library, then install `barryvdh/laravel-snappy` for Laravel-specific integrations like Blade directives and Service Providers. Ensure wkhtmltopdf (v0.12.x) is installed system-wide or via Docker.
- Which Laravel versions does knp-snappy support?
- The package works with Laravel 5.5+ and 6.x/7.x/8.x/9.x via barryvdh/laravel-snappy. Check the [Laravel Snappy Bundle](https://github.com/barryvdh/laravel-snappy) for version-specific docs. Core knp-snappy supports PHP 7.4+.
- Can I generate PDFs asynchronously in Laravel?
- Yes. Use Laravel’s queue system with `barryvdh/laravel-snappy` to offload PDF generation to background jobs (e.g., `SnappyJob`). Pair with Laravel Horizon or database queues for scalability. Avoid blocking HTTP requests.
- How do I configure wkhtmltopdf binary paths across environments?
- Set the binary path in `config/snappy.php` (via `barryvdh/laravel-snappy`) or dynamically via `Pdf::setBinary()`. Use environment variables (e.g., `.env`) for flexibility. For Docker, mount the binary or use `h4cc/wkhtmltopdf-amd64` as a Composer dependency.
- What security risks does wkhtmltopdf pose, and how do I mitigate them?
- The `--enable-local-file-access` flag is a critical RCE risk. Disable it by default in your `snappy.php` config. Sanitize all user-provided HTML/URLs (e.g., with HTML Purifier) and use sandboxing (AppArmor/SELinux). For untrusted content, consider WeasyPrint or Puppeteer.
- How do I handle failures if wkhtmltopdf crashes or times out?
- Implement retry logic in your job or middleware. Use Laravel’s `try-catch` blocks to log failures via Monolog. For critical apps, add fallback mechanisms like cached HTML snapshots or alternative renderers (e.g., Dompdf for simple cases).
- Can I generate thumbnails or snapshots from URLs in Laravel Blade?
- Yes. With `barryvdh/laravel-snappy`, use Blade directives like `@image('https://example.com', 'thumbnail.jpg')` or `@pdf('https://example.com', 'invoice.pdf')`. Customize dimensions/options via the `snappy` config array.
- What are the performance implications of wkhtmltopdf in production?
- PDF generation is CPU/IO-intensive. Offload to queues to avoid timeouts. Monitor resource usage (e.g., memory limits) and set timeouts in `snappy.php`. For high-volume apps, consider pre-rendering static content or using serverless functions.
- Are there alternatives to wkhtmltopdf for Laravel PDF generation?
- Yes. For simpler PDFs, use Dompdf or Barryvdh’s Laravel Dompdf. For advanced CSS/JS rendering, consider Puppeteer (via `spatie/pdf-to-image`) or WeasyPrint. wkhtmltopdf excels for dynamic webkit-based outputs but requires binary management.
- How do I test knp-snappy in Laravel CI/CD pipelines?
- Mock wkhtmltopdf responses using `symfony/process` stubs or Docker containers with pre-installed binaries. Test Blade directives via `collect()` or `obStart()`. Use Laravel’s `HttpClient` to mock external URLs. Validate outputs with assertions (e.g., file existence, PDF metadata).