knplabs/knp-snappy
PHP wrapper for wkhtmltopdf/wkhtmltoimage to generate PDFs and images (thumbnails, snapshots) from URLs or HTML. Simple API, configurable binaries and options, with integrations available for Symfony and Laravel.
knp-snappy excels in dynamic PDF/thumbnail generation from URLs or HTML, aligning perfectly with Laravel’s need for server-side rendering (SSR), invoicing, reporting, or dynamic document generation. The wrapper around wkhtmltopdf provides a PHP-native abstraction for a complex CLI tool, reducing boilerplate.barryvdh/laravel-snappy) is a direct Laravel integration, offering Service Provider registration, Facade support, and Blade directives (e.g., @pdf, @image). This reduces friction for Laravel-specific workflows.SnappyJob) enables asynchronous processing, improving responsiveness.wkhtmltopdf (v0.12.x) or wkhtmltoimage (binary dependency). Not a PHP package, so requires OS-level installation (Linux/macOS/Windows).symfony/process (for CLI execution), included via Composer.h4cc/wkhtmltopdf-* (Composer-installed binaries for portability, but limited OS support).php artisan snappy:generate) for admin workflows.--enable-local-file-access in wkhtmltopdf enables RCE vulnerabilities if misconfigured. Must:
'enable-local-file-access' => false).| Risk Area | Assessment | Mitigation Strategy |
|---|---|---|
| Binary Dependency | wkhtmltopdf must be installed system-wide or via Composer (limited OS support). |
Use Docker or CI/CD scripts to ensure binary availability. |
| Performance | PDF generation is blocking and resource-intensive. | Offload to queues (Laravel Horizon) or serverless (AWS Lambda). |
| Security | RCE risk if --enable-local-file-access is enabled. |
Hardcode defaults, use allowlists, and audit inputs. |
| Compatibility | wkhtmltopdf v0.12.x may lack features in newer versions. | Pin version in composer.json and test upgrades. |
| Error Handling | Binary failures (e.g., missing fonts, JS errors) may crash silently. | Implement retry logic and fallback mechanisms (e.g., cached HTML snapshots). |
| Testing | Dynamic HTML/URLs make unit testing difficult. | Use mocked responses (e.g., HttpClient fakes) and integration tests in CI. |
wkhtmltopdf be installed system-wide, via Docker, or as a Composer dependency?config/snappy.php)?wkhtmltopdf fails (e.g., degraded mode, alternative renderer)?Knp\Snappy\Pdf as a bindable interface for dependency injection.barryvdh/laravel-snappy for @pdf Blade directives or custom facades.SnappyGenerated events for post-processing (e.g., analytics, notifications).storage/app/pdf/) or cloud storage (S3).GeneratePdfJob) with retries and timeouts.class GeneratePdfJob implements ShouldQueue {
use Dispatchable, InteractsWithQueue;
public function handle() {
$pdf = app(Pdf::class);
$pdf->generateFromHtml($this->html, $this->path);
}
}
Cache::remember) or HTML snapshots (e.g., file_get_contents + hash()).barryvdh/laravel-snappy and test basic PDF generation (e.g., from a URL).knp-snappy.| Component | Compatibility Notes |
|---|---|
| PHP | 8.1+ (Laravel 9+) or 8.2+ (Laravel 10+). |
| Laravel | Tested with Laravel 6+ (via barryvdh/laravel-snappy). |
| wkhtmltopdf | v0.12.x required (newer versions may break). |
| Operating Systems | Linux/macOS/Windows (but Composer binaries have limited OS support). |
| Databases | None (storage-agnostic). |
| Third-Party Services | Works with any URL (internal or external), but rate limits may apply. |
wkhtmltopdf (e.g., sudo apt-get install wkhtmltopdf on Ubuntu).barryvdh/laravel-snappy to composer.json.php artisan vendor:publish --tag=snappy.binary path and default options in config/snappy.php.@pdf in Blade.enable-local-file-access globally.How can I help you explore Laravel packages today?