mostafaznv/pdf-optimizer
Optimize and compress PDFs in PHP or Laravel using Ghostscript. Fluent API with extensive option support, logging, and customization. Works with file paths, UploadedFile, and storage disks, with optional queue support for async optimization.
Install Ghostscript first—this is required but not bundled (you must handle licensing). On Ubuntu: sudo apt install ghostscript. Then install the package: composer require mostafaznv/pdf-optimizer. For Laravel, run php artisan vendor:publish --provider="Mostafaznv\PdfOptimizer\PdfOptimizerServiceProvider". Your first use case: compress an uploaded PDF before saving it—e.g., after a user submits a report.
Use fluent method chaining for clarity and control: chain settings (e.g., PdfSettings::PREPRESS), resolutions, and strategies (e.g., ColorConversionStrategy::DEVICE_INDEPENDENT_COLOR). Leverage Laravel-specific helpers: PdfOptimizer::fromDisk('local')->open($file)->toDisk('s3')->optimize(). For background processing, dispatch jobs using ->queue() ( Laravel’s queue workers handle the async execution). Combine with filesystem events (e.g., Storage::after() or model observers) to auto-optimize on upload. Use the logger() method or custom PSR-3 logger to track file sizes before/after and handle errors gracefully.
Ghostscript must be correctly installed and accessible via CLI—verify with which gs or whereis ghostscript. If optimization fails, check exit codes and logs; default timeouts can be too short for large files—tune via timeout(120). Windows support is untested—avoid production deployments there unless thoroughly validated. Beware Ghostscript’s AGPL licensing: if you distribute your app, you may need a commercial license or comply with AGPL. In Laravel, the config file (config/pdf-optimizer.php) lets you define defaults and binary paths—use env vars (GS_BINARY_PATH) for deployment-specific overrides. For debugging, temporarily switch to PdfSettings::PREPRESS for high quality and check intermediate logs to spot resolution/filters causing bloat.
How can I help you explore Laravel packages today?