mostafaznv/pdf-optimizer
Optimize and compress PDFs in PHP or Laravel using Ghostscript. Fluent, chainable API with rich options, logging, and customization. Laravel-friendly inputs (paths, uploads, disks) plus queue support for async optimization.
Install Ghostscript (required):
# Ubuntu/Debian
sudo apt-get install ghostscript
# Alpine
sudo apk add ghostscript
# MacOS
brew install ghostscript
Install the package:
composer require mostafaznv/pdf-optimizer
Publish config (Laravel only):
php artisan vendor:publish --provider="Mostafaznv\PdfOptimizer\PdfOptimizerServiceProvider"
Optimize a PDF file from disk to disk:
use Mostafaznv\PdfOptimizer\PdfOptimizer;
$result = PdfOptimizer::init()
->settings(\Mostafaznv\PdfOptimizer\Enums\PdfSettings::SCREEN)
->optimize('path/to/input.pdf', 'path/to/output.pdf');
if ($result->status) {
echo "Optimized successfully!";
}
For Laravel, use the facade:
use Mostafaznv\PdfOptimizer\Laravel\Facade\PdfOptimizer;
$result = PdfOptimizer::fromDisk('local')
->open('input.pdf')
->toDisk('s3')
->optimize('output.pdf');
Input Handling:
PdfOptimizer::init()->optimize('input.pdf', 'output.pdf')PdfOptimizer::fromDisk('s3')->open('input.pdf')->toDisk('local')UploadedFile instance directly to open()Method Chaining:
PdfOptimizer::init()
->settings(PdfSettings::PRINT)
->colorImageResolution(72)
->grayImageResolution(150)
->optimize('input.pdf', 'output.pdf');
Queue Jobs (Laravel):
PdfOptimizer::fromDisk('local')
->open('large.pdf')
->toDisk('s3')
->queue('optimize', 'output.pdf');
beforeOptimize() callback for validation/logging.$result->originalSize vs $result->optimizedSize for metrics.| Scenario | Implementation Pattern |
|---|---|
| User uploads PDF | PdfOptimizer::fromRequest()->open('pdf')->optimize('optimized.pdf') |
| Generate optimized PDFs | Chain with Barryvdh\DomPDF or spatie/pdf-to-html before optimization. |
| Background processing | Use queue() method with optimize() for async operations. |
| Dynamic settings | Store optimization profiles in DB and apply via settings() method. |
Ghostscript Dependency:
Ghostscript not found → Verify installation (gs --version).PATH or configure custom path in .env:
PDF_OPTIMIZER_GHOSTSCRIPT_BINARY=/custom/path/to/gs
Permission Issues:
Permission denied when writing files.local, s3, etc.) have write permissions.Memory Limits:
memory_limit in php.ini or optimize in chunks.Queue Failures:
PdfOptimizer::init()->logger(new \Monolog\Logger('pdf_optimizer'));
PdfOptimizer::init()->logger()->debug();
PdfOptimizer::init()->exportScript() to preview the generated command.php artisan queue:work to debug failed jobs.config/pdf-optimizer.php) includes presets like SCREEN, PRINT, and EBOOK.addGhostscriptOption() for unsupported features:
->addGhostscriptOption('-dPDFSETTINGS=/prepress')
Custom Optimizers:
PdfOptimizer::init()->customOptimizer(function ($input, $output) {
// Custom logic (e.g., pre-process with Imagick)
});
Event Listeners:
PdfOptimizerOptimizing and PdfOptimizerOptimized events for hooks.Service Provider:
PdfOptimizerServiceProvider to add global defaults:PdfOptimizer::macro('customMacro', function () {
return $this->settings(PdfSettings::EBOOK)->colorImageResolution(96);
});
chunk() for large datasets.optimized_ prefix to avoid reprocessing.PdfOptimizerJob events.fallback_disk in config/filesystems.php for missing disks.queueOn('sqs') for distributed systems.PdfOptimizer in tests:
$this->partialMock(PdfOptimizer::class, ['optimize']);
How can I help you explore Laravel packages today?