Next major version 2 with PHP 8.1, Symfony 6, Tabler UI, 2FA ... (#2902)
This commit is contained in:
24
src/Pdf/HtmlToPdfConverter.php
Normal file
24
src/Pdf/HtmlToPdfConverter.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?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\Pdf;
|
||||
|
||||
interface HtmlToPdfConverter
|
||||
{
|
||||
/**
|
||||
* Returns the binary content of the PDF, which can be saved as file.
|
||||
* Throws an exception if conversion fails.
|
||||
*
|
||||
* @param string $html
|
||||
* @param array $options
|
||||
* @return string
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function convertToPdf(string $html, array $options = []): string;
|
||||
}
|
||||
102
src/Pdf/MPdfConverter.php
Normal file
102
src/Pdf/MPdfConverter.php
Normal file
@@ -0,0 +1,102 @@
|
||||
<?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\Pdf;
|
||||
|
||||
use App\Constants;
|
||||
use App\Utils\FileHelper;
|
||||
use Mpdf\Config\ConfigVariables;
|
||||
use Mpdf\Config\FontVariables;
|
||||
use Mpdf\Mpdf;
|
||||
use Mpdf\Output\Destination;
|
||||
|
||||
final class MPdfConverter implements HtmlToPdfConverter
|
||||
{
|
||||
public function __construct(private FileHelper $fileHelper, private string $cacheDirectory)
|
||||
{
|
||||
}
|
||||
|
||||
private function sanitizeOptions(array $options): array
|
||||
{
|
||||
$configs = new ConfigVariables();
|
||||
$fonts = new FontVariables();
|
||||
$allowed = [
|
||||
'mode', 'format', 'default_font_size', 'default_font', 'margin_left', 'margin_right', 'margin_top',
|
||||
'margin_bottom', 'margin_header', 'margin_footer', 'orientation',
|
||||
];
|
||||
|
||||
$filtered = array_filter($options, function ($key) use ($allowed, $configs, $fonts): bool {
|
||||
if (!\in_array($key, $allowed)) {
|
||||
if (!\array_key_exists($key, $configs->getDefaults())) {
|
||||
return \array_key_exists($key, $fonts->getDefaults());
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}, ARRAY_FILTER_USE_KEY);
|
||||
|
||||
if (\array_key_exists('tempDir', $filtered)) {
|
||||
unset($filtered['tempDir']);
|
||||
}
|
||||
|
||||
return $filtered;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $html
|
||||
* @param array $options
|
||||
* @return string
|
||||
* @throws \Mpdf\MpdfException
|
||||
*/
|
||||
public function convertToPdf(string $html, array $options = []): string
|
||||
{
|
||||
$sanitized = array_merge(
|
||||
$this->sanitizeOptions($options),
|
||||
['tempDir' => $this->cacheDirectory, 'exposeVersion' => false]
|
||||
);
|
||||
|
||||
$mpdf = new Mpdf($sanitized);
|
||||
$mpdf->creator = Constants::SOFTWARE;
|
||||
|
||||
if (\array_key_exists('fonts', $options)) {
|
||||
$mpdf->AddFontDirectory($this->fileHelper->getDataDirectory('fonts'));
|
||||
foreach ($options['fonts'] as $fontName => $fontConfig) {
|
||||
$mpdf->AddFont($fontName, $fontConfig);
|
||||
}
|
||||
}
|
||||
|
||||
// some OS'es do not follow the PHP default settings
|
||||
if ((int) \ini_get('pcre.backtrack_limit') < 1000000) {
|
||||
@ini_set('pcre.backtrack_limit', '1000000');
|
||||
}
|
||||
|
||||
// large amount of data take time
|
||||
@ini_set('max_execution_time', '120');
|
||||
|
||||
// reduce the size of content parts that are passed to MPDF, to prevent
|
||||
// https://mpdf.github.io/troubleshooting/known-issues.html#blank-pages-or-some-sections-missing
|
||||
$parts = explode('<pagebreak>', $html);
|
||||
for ($i = 0; $i < \count($parts); $i++) {
|
||||
if (stripos($parts[$i], '<!-- CONTENT_PART -->') !== false) {
|
||||
$subParts = explode('<!-- CONTENT_PART -->', $parts[$i]);
|
||||
foreach ($subParts as $subPart) {
|
||||
$mpdf->WriteHTML($subPart);
|
||||
}
|
||||
} else {
|
||||
$mpdf->WriteHTML($parts[$i]);
|
||||
}
|
||||
|
||||
if ($i < \count($parts) - 1) {
|
||||
$mpdf->WriteHTML('<pagebreak>');
|
||||
}
|
||||
}
|
||||
|
||||
return $mpdf->Output('', Destination::STRING_RETURN);
|
||||
}
|
||||
}
|
||||
38
src/Pdf/PdfContext.php
Normal file
38
src/Pdf/PdfContext.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?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\Pdf;
|
||||
|
||||
/**
|
||||
* A simple class that is available in PDF-Renderer context,
|
||||
* which can be used to define global renderer options.
|
||||
*/
|
||||
final class PdfContext
|
||||
{
|
||||
private array $options = [];
|
||||
|
||||
public function setOption(string $key, string|array|null|bool $value): void
|
||||
{
|
||||
$this->options[$key] = $value;
|
||||
}
|
||||
|
||||
public function getOptions(): array
|
||||
{
|
||||
return $this->options;
|
||||
}
|
||||
|
||||
public function getOption(string $key): array|string|null
|
||||
{
|
||||
if (\array_key_exists($key, $this->options)) {
|
||||
return $this->options[$key];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
43
src/Pdf/PdfRendererTrait.php
Normal file
43
src/Pdf/PdfRendererTrait.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?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\Pdf;
|
||||
|
||||
use App\Utils\FileHelper;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
|
||||
|
||||
trait PdfRendererTrait
|
||||
{
|
||||
private bool $inline = false;
|
||||
|
||||
public function setDispositionInline(bool $useInlineDisposition): void
|
||||
{
|
||||
$this->inline = $useInlineDisposition;
|
||||
}
|
||||
|
||||
protected function createPdfResponse(string $content, PdfContext $context): Response
|
||||
{
|
||||
$filename = $context->getOption('filename');
|
||||
if (empty($filename)) {
|
||||
throw new \Exception('Empty PDF filename given');
|
||||
}
|
||||
$filename = FileHelper::convertToAsciiFilename($filename);
|
||||
|
||||
$response = new Response($content);
|
||||
|
||||
$disposition = $this->inline ? ResponseHeaderBag::DISPOSITION_INLINE : ResponseHeaderBag::DISPOSITION_ATTACHMENT;
|
||||
$disposition = $response->headers->makeDisposition($disposition, $filename . '.pdf');
|
||||
|
||||
$response->headers->set('Content-Type', 'application/pdf');
|
||||
$response->headers->set('Content-Disposition', $disposition);
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user