* deactivate deprecated auto-mapping * use native month-picker with fallback on old browser * bump dependencies to new major versions * replace deprecated ARRAY column type with JSON * named arguments * nullable arguments * native lazy ghosts * update routes to use php config * disable PHP < 8.4 * use static RTL configuration * new qr code package * clear out env file * recipe updates * fix deprecations * preset new env variable if not existing * bump maria db version examples * remove deprecated api-token support * fix validator deprecations * remove timesheet category column * fix broken validator autoconfiguration * stabilize tests * fix migration syntax compatibility * fix doctrine deprecation * fix datetime format * remove constructor dependency * only include visible preferences in invoice templates * fix test container dependency * removed ProjectConstraint multi-constraint logic * allow to disable plugins in certain environments * refactor TimesheetConstraint to non-service * activate new interface methods * prevent update issues with .env * improved customer fixture * removed $user->isExportDecimal()
113 lines
2.9 KiB
PHP
113 lines
2.9 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of the Kimai time-tracking app.
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace App\Export\Base;
|
|
|
|
use App\Entity\ExportableItem;
|
|
use App\Export\ExportFilename;
|
|
use App\Export\ExportRendererInterface;
|
|
use App\Pdf\HtmlToPdfConverter;
|
|
use App\Pdf\PdfContext;
|
|
use App\Pdf\PdfRendererTrait;
|
|
use App\Project\ProjectStatisticService;
|
|
use App\Repository\Query\TimesheetQuery;
|
|
use App\Twig\SecurityPolicy\ExportPolicy;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Twig\Environment;
|
|
use Twig\Extension\SandboxExtension;
|
|
|
|
final class PDFRenderer implements DispositionInlineInterface, ExportRendererInterface
|
|
{
|
|
use RendererTrait;
|
|
use PDFRendererTrait;
|
|
|
|
private array $pdfOptions = [];
|
|
|
|
public function __construct(
|
|
private readonly Environment $twig,
|
|
private readonly HtmlToPdfConverter $converter,
|
|
private readonly ProjectStatisticService $projectStatisticService,
|
|
private string $id,
|
|
private string $title,
|
|
private string $template,
|
|
)
|
|
{
|
|
}
|
|
|
|
public function isInternal(): false
|
|
{
|
|
return false;
|
|
}
|
|
|
|
public function getType(): string
|
|
{
|
|
return 'pdf';
|
|
}
|
|
|
|
public function getTitle(): string
|
|
{
|
|
return $this->title;
|
|
}
|
|
|
|
protected function getTemplate(): string
|
|
{
|
|
return $this->template;
|
|
}
|
|
|
|
public function getPdfOptions(): array
|
|
{
|
|
return $this->pdfOptions;
|
|
}
|
|
|
|
public function setPdfOption(string $key, string $value): void
|
|
{
|
|
$this->pdfOptions[$key] = $value;
|
|
}
|
|
|
|
/**
|
|
* @param ExportableItem[] $exportItems
|
|
* @throws \Twig\Error\LoaderError
|
|
* @throws \Twig\Error\RuntimeError
|
|
* @throws \Twig\Error\SyntaxError
|
|
*/
|
|
public function render(array $exportItems, TimesheetQuery $query): Response
|
|
{
|
|
$filename = new ExportFilename($query);
|
|
$context = new PdfContext();
|
|
$context->setOption('filename', $filename->getFilename());
|
|
|
|
$summary = $this->calculateSummary($exportItems);
|
|
|
|
// enable basic security measures
|
|
$sandbox = new SandboxExtension(new ExportPolicy());
|
|
$sandbox->enableSandbox();
|
|
$this->twig->addExtension($sandbox);
|
|
|
|
$content = $this->twig->render($this->getTemplate(), [
|
|
'entries' => $exportItems,
|
|
'query' => $query,
|
|
'summaries' => $summary,
|
|
'budgets' => $this->calculateProjectBudget($exportItems, $query, $this->projectStatisticService),
|
|
'decimal' => false,
|
|
'pdfContext' => $context
|
|
]);
|
|
|
|
$pdfOptions = array_merge($context->getOptions(), $this->getPdfOptions());
|
|
|
|
$content = $this->converter->convertToPdf($content, $pdfOptions);
|
|
|
|
return $this->createPdfResponse($content, $context);
|
|
}
|
|
|
|
public function getId(): string
|
|
{
|
|
return $this->id;
|
|
}
|
|
}
|