copy($inputFilepath, $backupFilename); } $tempFilename = sha1($inputFilepath); $outputFilepath = $this->tempDir.'/'.$tempFilename; $pdfFilepath = null; if (null === $targetFilepath) { $targetFilepath = $inputFilepath.'.pdf'; } if ('pdf' !== pathinfo($inputFilepath, PATHINFO_EXTENSION)) { $pdfFilepath = $outputFilepath.'.pdf'; $command = [ $this->binConvert, $inputFilepath, $pdfFilepath, ]; $process = new Process($command); $process->run(); if (false === $process->isSuccessful()) { throw new \InvalidArgumentException('File could not be sanitized: '.$process->getErrorOutput()); } $inputFilepath = $pdfFilepath; } $command = [ $this->binGs, '-dNOPAUSE', '-dQUIET', '-dBATCH', '-dPreserveAnnots=false', '-sDEVICE=pdfwrite', '-dCompatibilityLevel=1.4', '-sOutputFile='.$outputFilepath, '-f', $inputFilepath, ]; $process = new Process($command); $process->run(); if (false === $process->isSuccessful()) { throw new \InvalidArgumentException('File could not be sanitized: '.$process->getErrorOutput()); } $filesystem->rename($outputFilepath, $targetFilepath, true); if (null !== $pdfFilepath) { $filesystem->remove($pdfFilepath); } return $targetFilepath; } }