feat: pdf converter command

This commit is contained in:
Björn Fromme
2025-03-19 10:57:45 +01:00
parent 9aaf996e07
commit aae385d165
2 changed files with 72 additions and 2 deletions
+12 -2
View File
@@ -2,6 +2,7 @@
namespace App\Service\Pdf;
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Process\Process;
@@ -16,11 +17,16 @@ class Sanitizer
/**
* @throws \InvalidArgumentException
* @throws FileNotFoundException
*/
public function sanitizeFile(string $inputFilepath, ?string $targetFilepath = null, bool $backup = true): string
{
$filesystem = new Filesystem();
if (false === $filesystem->exists($inputFilepath)) {
throw new FileNotFoundException();
}
if (true === $backup) {
$parts = pathinfo($inputFilepath);
$backupFilename = sprintf('%s/%s.orig.%s', $parts['dirname'], $parts['filename'], $parts['extension']);
@@ -32,7 +38,8 @@ class Sanitizer
$pdfFilepath = null;
if (null === $targetFilepath) {
$targetFilepath = $inputFilepath.'.pdf';
$parts = pathinfo($inputFilepath);
$targetFilepath = sprintf('%s/%s.%s', $parts['dirname'], $parts['filename'], $parts['extension']);
}
if ('pdf' !== pathinfo($inputFilepath, PATHINFO_EXTENSION)) {
@@ -51,7 +58,10 @@ class Sanitizer
throw new \InvalidArgumentException('File could not be sanitized: '.$process->getErrorOutput());
}
$filesystem->remove($inputFilepath);
$inputFilepath = $pdfFilepath;
$targetFilepath.= '.pdf';
}
$command = [
@@ -61,7 +71,7 @@ class Sanitizer
'-dBATCH',
'-dPreserveAnnots=false',
'-sDEVICE=pdfwrite',
'-dCompatibilityLevel=1.4',
'-dCompatibilityLevel=1.5',
'-sOutputFile='.$outputFilepath,
'-f',
$inputFilepath,