Files
Inventory/src/Service/PdfCompressorService.php
Matthieu d89c97f0a0 feat(documents) : filesystem storage, server-side pagination and PDF compression
- Add DocumentStorageService for file-based storage (replaces Base64 in DB)
- Add DocumentServeController with /file and /download endpoints
- Add DocumentUploadProcessor using FormData + filesystem storage
- Add DocumentNormalizer exposing fileUrl/downloadUrl on all responses
- Add DocumentFileCleanupListener for automatic file deletion
- Add MigrateDocumentsToFilesystemCommand (Base64 → files, memory-safe)
- Add ApiFilter (SearchFilter, ExistsFilter, OrderFilter) on Document entity
- Add PdfCompressorService + refactor CompressPdfCommand for batch processing
- Fix TypeMachine PUT: deserialize=false + validate=false to prevent
  UniqueEntity false positive and writableLink collection interference
- Update CHANGELOG for v1.8.0
- Update frontend submodule

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-03 15:18:55 +01:00

131 lines
3.5 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Service;
class PdfCompressorService
{
/**
* Compress an actual PDF file on disk. Returns metadata or null if no gain.
*
* @return null|array{size: int, originalSize: int, saved: int}
*/
public function compressFile(string $absolutePath): ?array
{
exec('which qpdf', $qpdfPath, $returnCode);
if (0 !== $returnCode) {
return null;
}
if (!file_exists($absolutePath)) {
return null;
}
$originalSize = filesize($absolutePath);
if (false === $originalSize) {
return null;
}
$tempOutput = tempnam(sys_get_temp_dir(), 'pdf_out_');
$command = sprintf(
'qpdf --linearize --object-streams=generate %s %s 2>&1',
escapeshellarg($absolutePath),
escapeshellarg($tempOutput)
);
exec($command, $cmdOutput, $returnCode);
if (0 !== $returnCode || !file_exists($tempOutput)) {
@unlink($tempOutput);
return null;
}
$compressedSize = filesize($tempOutput);
if (false === $compressedSize || $compressedSize >= $originalSize) {
@unlink($tempOutput);
return null;
}
if (!rename($tempOutput, $absolutePath)) {
@unlink($tempOutput);
return null;
}
return [
'size' => $compressedSize,
'originalSize' => $originalSize,
'saved' => $originalSize - $compressedSize,
];
}
public function compressBase64Pdf(string $base64Data): ?array
{
// Check if qpdf is available
exec('which qpdf', $qpdfPath, $returnCode);
if (0 !== $returnCode) {
return null;
}
// Remove data URI prefix if present
$originalBase64 = $base64Data;
if (str_contains($base64Data, ',')) {
$base64Data = explode(',', $base64Data, 2)[1];
}
$pdfContent = base64_decode($base64Data, true);
if (false === $pdfContent) {
return null;
}
$originalSize = strlen($pdfContent);
// Create temp files
$tempInput = tempnam(sys_get_temp_dir(), 'pdf_in_');
$tempOutput = tempnam(sys_get_temp_dir(), 'pdf_out_');
file_put_contents($tempInput, $pdfContent);
// Compress with qpdf (lossless)
$command = sprintf(
'qpdf --linearize --object-streams=generate %s %s 2>&1',
escapeshellarg($tempInput),
escapeshellarg($tempOutput)
);
exec($command, $cmdOutput, $returnCode);
if (0 !== $returnCode || !file_exists($tempOutput)) {
@unlink($tempInput);
@unlink($tempOutput);
return null;
}
$compressedContent = file_get_contents($tempOutput);
$compressedSize = strlen($compressedContent);
@unlink($tempInput);
@unlink($tempOutput);
// Only return compressed version if it's smaller
if ($compressedSize >= $originalSize) {
return null;
}
// Rebuild with data URI prefix
$newBase64 = 'data:application/pdf;base64,'.base64_encode($compressedContent);
return [
'path' => $newBase64,
'size' => $compressedSize,
'originalSize' => $originalSize,
'saved' => $originalSize - $compressedSize,
];
}
}