fix pdf export cache directory (#650)

This commit is contained in:
Kevin Papst
2019-03-22 14:37:25 +01:00
committed by GitHub
parent 50318a8fc5
commit cfc19dca25
6 changed files with 116 additions and 11 deletions

View File

@@ -59,6 +59,9 @@ services:
arguments: arguments:
$languageSettings: "%kimai.languages%" $languageSettings: "%kimai.languages%"
App\Utils\MPdfConverter:
arguments: ['%kernel.cache_dir%']
# ================================================================================ # ================================================================================
# DATABASE # DATABASE
# ================================================================================ # ================================================================================

View File

@@ -13,8 +13,7 @@ use App\Entity\Timesheet;
use App\Export\RendererInterface; use App\Export\RendererInterface;
use App\Repository\Query\TimesheetQuery; use App\Repository\Query\TimesheetQuery;
use App\Timesheet\UserDateTimeFactory; use App\Timesheet\UserDateTimeFactory;
use Mpdf\Mpdf; use App\Utils\HtmlToPdfConverter;
use Mpdf\Output\Destination;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag; use Symfony\Component\HttpFoundation\ResponseHeaderBag;
@@ -26,20 +25,24 @@ class PDFRenderer implements RendererInterface
* @var \Twig_Environment * @var \Twig_Environment
*/ */
protected $twig; protected $twig;
/** /**
* @var UserDateTimeFactory * @var UserDateTimeFactory
*/ */
protected $dateTime; protected $dateTime;
/**
* @var HtmlToPdfConverter
*/
protected $converter;
/** /**
* @param \Twig_Environment $twig * @param \Twig_Environment $twig
* @param UserDateTimeFactory $dateTime * @param UserDateTimeFactory $dateTime
*/ */
public function __construct(\Twig_Environment $twig, UserDateTimeFactory $dateTime) public function __construct(\Twig_Environment $twig, UserDateTimeFactory $dateTime, HtmlToPdfConverter $converter)
{ {
$this->twig = $twig; $this->twig = $twig;
$this->dateTime = $dateTime; $this->dateTime = $dateTime;
$this->converter = $converter;
} }
/** /**
@@ -60,11 +63,7 @@ class PDFRenderer implements RendererInterface
'summaries' => $this->calculateSummary($timesheets), 'summaries' => $this->calculateSummary($timesheets),
]); ]);
//return new Response($content); $content = $this->converter->convertToPdf($content);
$mpdf = new Mpdf();
$mpdf->WriteHTML($content);
$content = $mpdf->Output('test', Destination::STRING_RETURN);
$response = new Response($content); $response = new Response($content);

View File

@@ -0,0 +1,20 @@
<?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\Utils;
interface HtmlToPdfConverter
{
/**
* Returns the binary content of the PDF, which can be saved as file or send via Reponse.
* @param string $html
* @return mixed
*/
public function convertToPdf(string $html);
}

View File

@@ -0,0 +1,41 @@
<?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\Utils;
use App\Constants;
use Mpdf\Mpdf;
use Mpdf\Output\Destination;
class MPdfConverter implements HtmlToPdfConverter
{
/**
* @var string
*/
private $cacheDirectory;
public function __construct(string $cacheDirectory)
{
$this->cacheDirectory = $cacheDirectory;
}
/**
* @param string $html
* @return mixed|string
* @throws \Mpdf\MpdfException
*/
public function convertToPdf(string $html)
{
$mpdf = new Mpdf([['tempDir' => $this->cacheDirectory]]);
$mpdf->creator = Constants::SOFTWARE;
$mpdf->WriteHTML($html);
return $mpdf->Output('', Destination::STRING_RETURN);
}
}

View File

@@ -14,6 +14,8 @@ use App\Export\Renderer\PDFRenderer;
use App\Repository\UserRepository; use App\Repository\UserRepository;
use App\Security\CurrentUser; use App\Security\CurrentUser;
use App\Timesheet\UserDateTimeFactory; use App\Timesheet\UserDateTimeFactory;
use App\Utils\HtmlToPdfConverter;
use App\Utils\MPdfConverter;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
@@ -44,7 +46,8 @@ class PdfRendererTest extends AbstractRendererTest
{ {
$sut = new PDFRenderer( $sut = new PDFRenderer(
$this->getMockBuilder(\Twig_Environment::class)->disableOriginalConstructor()->getMock(), $this->getMockBuilder(\Twig_Environment::class)->disableOriginalConstructor()->getMock(),
$this->getDateTimeFactory() $this->getDateTimeFactory(),
$this->getMockBuilder(HtmlToPdfConverter::class)->getMock()
); );
$this->assertEquals('pdf', $sut->getId()); $this->assertEquals('pdf', $sut->getId());
@@ -58,6 +61,8 @@ class PdfRendererTest extends AbstractRendererTest
/** @var \Twig_Environment $twig */ /** @var \Twig_Environment $twig */
$twig = $kernel->getContainer()->get('twig'); $twig = $kernel->getContainer()->get('twig');
$stack = $kernel->getContainer()->get('request_stack'); $stack = $kernel->getContainer()->get('request_stack');
$cacheDir = $kernel->getContainer()->getParameter('kernel.cache_dir');
$converter = new MPdfConverter($cacheDir);
$request = new Request(); $request = new Request();
$request->setLocale('en'); $request->setLocale('en');
$stack->push($request); $stack->push($request);
@@ -65,7 +70,7 @@ class PdfRendererTest extends AbstractRendererTest
/** @var FilesystemLoader $loader */ /** @var FilesystemLoader $loader */
$loader = $twig->getLoader(); $loader = $twig->getLoader();
$sut = new PDFRenderer($twig, $this->getDateTimeFactory()); $sut = new PDFRenderer($twig, $this->getDateTimeFactory(), $converter);
$response = $this->render($sut); $response = $this->render($sut);

View File

@@ -0,0 +1,37 @@
<?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\Tests\Utils;
use App\Utils\MPdfConverter;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
/**
* @covers \App\Utils\MPdfConverter
*/
class MPdfConverterTest extends KernelTestCase
{
function unicode_hex($unicode_dec)
{
return (sprintf("%05s", strtoupper(dechex($unicode_dec))));
}
public function test()
{
$kernel = self::bootKernel();
$cacheDir = $kernel->getContainer()->getParameter('kernel.cache_dir');
$sut = new MPdfConverter($cacheDir);
$result = $sut->convertToPdf('<h1>Test</h1>');
// Yeah, thats not a real test, I know ;-)
$this->assertNotEmpty($result);
preg_match('/\/Creator \((.*)\)/', $result, $matches);
$this->assertCount(2, $matches);
}
}