feat: convert uploaded contracts and invoices to pdf
This commit is contained in:
@@ -31,6 +31,7 @@ services:
|
||||
autowire: true
|
||||
autoconfigure: true
|
||||
bind:
|
||||
$tempDir: '%kernel.project_dir%/temp'
|
||||
$logger: '@monolog.logger.myep'
|
||||
$destinations: '%destinations%'
|
||||
|
||||
@@ -138,6 +139,11 @@ services:
|
||||
$orphanageManager: '@oneup_uploader.orphanage_manager'
|
||||
$projectDir: '%kernel.project_dir%'
|
||||
|
||||
App\Service\Pdf\Sanitizer:
|
||||
arguments:
|
||||
$binGs: '/usr/bin/gs'
|
||||
$binConvert: '/usr/bin/convert'
|
||||
|
||||
App\EventListener\UploadSessionListener:
|
||||
tags:
|
||||
- name: kernel.event_listener
|
||||
|
||||
@@ -126,7 +126,11 @@ class CheckController extends AbstractController
|
||||
;
|
||||
|
||||
if (Upload::TYPE_INVOICE === $document->getType()) {
|
||||
$this->invoiceApprover->render($document->getDisposition());
|
||||
try {
|
||||
$this->invoiceApprover->render($document->getDisposition());
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
$this->addFlash('error', 'Die Honorarnote ist nicht als PDF hochgeladen worden');
|
||||
}
|
||||
}
|
||||
|
||||
$this->addFlash('success', 'Das Dokument wurde bestätigt');
|
||||
|
||||
@@ -86,10 +86,11 @@ class DetailController extends AbstractController
|
||||
$disposition->addDocument($upload);
|
||||
$this->workflow->apply($disposition, 'upload_contract');
|
||||
|
||||
$this->entityManager->flush();
|
||||
|
||||
$this->uploadHandler->moveUploadSessionFilesFromOrphanage(Upload::TYPE_CONTRACT, $uploadSession);
|
||||
$this->uploadHandler->destroyUploadSession();
|
||||
$this->uploadHandler->ensurePdf($upload);
|
||||
|
||||
$this->entityManager->flush();
|
||||
|
||||
$this->addFlash('success', 'Der Honorarvertrag wurde hochgeladen');
|
||||
|
||||
@@ -115,10 +116,11 @@ class DetailController extends AbstractController
|
||||
$disposition->addDocument($upload);
|
||||
$this->workflow->apply($disposition, 'upload_invoice');
|
||||
|
||||
$this->entityManager->flush();
|
||||
|
||||
$this->uploadHandler->moveUploadSessionFilesFromOrphanage(Upload::TYPE_INVOICE, $uploadSession);
|
||||
$this->uploadHandler->destroyUploadSession();
|
||||
$this->uploadHandler->ensurePdf($upload);
|
||||
|
||||
$this->entityManager->flush();
|
||||
|
||||
$this->addFlash('success', 'Die Honornote wurde hochgeladen');
|
||||
|
||||
@@ -128,4 +130,4 @@ class DetailController extends AbstractController
|
||||
|
||||
$this->eventDispatcher->dispatch(new DocumentUploadedEvent($upload), DocumentUploadedEvent::NAME);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,12 +8,19 @@ use Symfony\Component\Filesystem\Filesystem;
|
||||
|
||||
class InvoiceApprover extends AbstractPdfRenderer
|
||||
{
|
||||
/**
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function render(Disposition $disposition): Pdf
|
||||
{
|
||||
$teamer = $disposition->getTeamer();
|
||||
$assignment = $disposition->getAssignment();
|
||||
$invoice = $disposition->getDocumentByType(Upload::TYPE_INVOICE);
|
||||
$originalPdfFilename = $invoice->getFilename();
|
||||
$originalFilename = $invoice->getFilename();
|
||||
|
||||
if ('application/pdf' !== $invoice->getMimeType()) {
|
||||
throw new \InvalidArgumentException('Expected PDF but got '.$invoice->getMimeType());
|
||||
}
|
||||
|
||||
$pdf = new Pdf();
|
||||
|
||||
@@ -21,7 +28,7 @@ class InvoiceApprover extends AbstractPdfRenderer
|
||||
$pdf->SetCreator($teamer->getFullName(), true);
|
||||
|
||||
// Load template
|
||||
$filepath = sprintf('%s/uploads/invoice/%s', $this->config['project_dir'], $originalPdfFilename);
|
||||
$filepath = sprintf('%s/uploads/invoice/%s', $this->config['project_dir'], $originalFilename);
|
||||
$pdf->appendPdf($filepath);
|
||||
$pdf->setPage(1);
|
||||
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service\Pdf;
|
||||
|
||||
use Symfony\Component\Filesystem\Filesystem;
|
||||
use Symfony\Component\Process\Process;
|
||||
|
||||
class Sanitizer
|
||||
{
|
||||
public function __construct(
|
||||
private readonly string $binGs,
|
||||
private readonly string $binConvert,
|
||||
private readonly string $tempDir,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function sanitizeFile(string $inputFilepath, ?string $targetFilepath = null, bool $backup = true): string
|
||||
{
|
||||
$filesystem = new Filesystem();
|
||||
|
||||
if (true === $backup) {
|
||||
$filesystem->copy($inputFilepath, $inputFilepath.'.bak');
|
||||
}
|
||||
|
||||
$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',
|
||||
'-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;
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ namespace App\Service\Upload;
|
||||
use App\Entity\Upload;
|
||||
use App\Model\UploadDto;
|
||||
use App\Model\UploadSessionDto;
|
||||
use App\Service\Pdf\Sanitizer;
|
||||
use Oneup\UploaderBundle\Uploader\Orphanage\OrphanageManager;
|
||||
use Symfony\Component\Filesystem\Exception\IOException;
|
||||
use Symfony\Component\Filesystem\Filesystem;
|
||||
@@ -17,6 +18,7 @@ class UploadHandler
|
||||
public function __construct(
|
||||
private readonly RequestStack $requestStack,
|
||||
private readonly OrphanageManager $orphanageManager,
|
||||
private readonly Sanitizer $sanitizer,
|
||||
private readonly string $projectDir
|
||||
) {
|
||||
}
|
||||
@@ -131,4 +133,17 @@ class UploadHandler
|
||||
{
|
||||
return sprintf('%s/uploads/temp/%s', $this->projectDir, $uploadSessionId);
|
||||
}
|
||||
|
||||
public function ensurePdf(Upload $upload): void
|
||||
{
|
||||
try {
|
||||
$filepath = $this->getUploadFilepath($upload);
|
||||
$sanitizedFilepath = $this->sanitizer->sanitizeFile($filepath);
|
||||
|
||||
if ($filepath !== $sanitizedFilepath) {
|
||||
$upload->setFilename(basename($sanitizedFilepath));
|
||||
}
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 702 KiB |
Binary file not shown.
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tests\Service\Pdf;
|
||||
|
||||
use App\Service\Pdf\Sanitizer;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Filesystem\Filesystem;
|
||||
|
||||
class SanitizerTest extends TestCase
|
||||
{
|
||||
public function testAcceptsImages(): void
|
||||
{
|
||||
$tempDir = realpath(__DIR__.'/../../../temp');
|
||||
$sourceFilepath = realpath(__DIR__.'/../../Resources/upload.jpg');
|
||||
$targetFilepath = $tempDir.'/test.pdf';
|
||||
|
||||
$sanitizer = new Sanitizer('/usr/bin/gs', '/usr/bin/convert', $tempDir);
|
||||
|
||||
$sanitizer->sanitizeFile($sourceFilepath, $targetFilepath);
|
||||
|
||||
$this->assertFileExists($targetFilepath);
|
||||
|
||||
(new Filesystem())->remove($targetFilepath);
|
||||
}
|
||||
|
||||
public function testAcceptsPdfs(): void
|
||||
{
|
||||
$tempDir = realpath(__DIR__.'/../../../temp');
|
||||
$sourceFilepath = realpath(__DIR__.'/../../Resources/upload.pdf');
|
||||
$targetFilepath = $tempDir.'/test.pdf';
|
||||
|
||||
$sanitizer = new Sanitizer('/usr/bin/gs', '/usr/bin/convert', $tempDir);
|
||||
|
||||
$sanitizer->sanitizeFile($sourceFilepath, $targetFilepath);
|
||||
|
||||
$this->assertFileExists($targetFilepath);
|
||||
|
||||
(new Filesystem())->remove($targetFilepath);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user