mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-08-02 09:14:48 +02:00
Added a basic text to PDF renderer.
This commit is contained in:
parent
a8a92b9c5d
commit
dee4094d8b
10 changed files with 546 additions and 5 deletions
59
src/Controller/LabelController.php
Normal file
59
src/Controller/LabelController.php
Normal file
|
@ -0,0 +1,59 @@
|
|||
<?php
|
||||
/**
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published
|
||||
* by the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
|
||||
use App\Entity\LabelSystem\LabelProfile;
|
||||
use App\Entity\Parts\Part;
|
||||
use App\Helpers\LabelResponse;
|
||||
use App\Services\LabelSystem\LabelGenerator;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
|
||||
/**
|
||||
* @Route("/label")
|
||||
* @package App\Controller
|
||||
*/
|
||||
class LabelController extends AbstractController
|
||||
{
|
||||
protected $labelGenerator;
|
||||
|
||||
public function __construct(LabelGenerator $labelGenerator)
|
||||
{
|
||||
$this->labelGenerator = $labelGenerator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Route("/{profile}/{part}/view")
|
||||
*/
|
||||
public function view(LabelProfile $profile, Part $part)
|
||||
{
|
||||
$label = $this->labelGenerator->generateLabel($profile->getOptions(), $part);
|
||||
|
||||
$response = new LabelResponse($label);
|
||||
$response->setContentDisposition(ResponseHeaderBag::DISPOSITION_INLINE, 'label.pdf');
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
110
src/Helpers/LabelResponse.php
Normal file
110
src/Helpers/LabelResponse.php
Normal file
|
@ -0,0 +1,110 @@
|
|||
<?php
|
||||
/**
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published
|
||||
* by the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace App\Helpers;
|
||||
|
||||
|
||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||
use Symfony\Component\HttpFoundation\File\File;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class LabelResponse extends Response
|
||||
{
|
||||
public function __construct($content = '', int $status = 200, array $headers = [])
|
||||
{
|
||||
parent::__construct($content, $status, $headers);
|
||||
}
|
||||
|
||||
public function setContent($content)
|
||||
{
|
||||
parent::setContent($content);
|
||||
|
||||
$this->setAutoEtag();
|
||||
$this->setAutoLastModified();
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function prepare(Request $request)
|
||||
{
|
||||
parent::prepare($request);
|
||||
|
||||
$this->headers->set('Content-Type','application/pdf');
|
||||
|
||||
|
||||
if ('HTTP/1.0' !== $request->server->get('SERVER_PROTOCOL')) {
|
||||
$this->setProtocolVersion('1.1');
|
||||
}
|
||||
|
||||
$this->ensureIEOverSSLCompatibility($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Automatically sets the Last-Modified header according the file modification date.
|
||||
*/
|
||||
public function setAutoLastModified()
|
||||
{
|
||||
$this->setLastModified(new \DateTime());
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Automatically sets the ETag header according to the checksum of the file.
|
||||
*/
|
||||
public function setAutoEtag()
|
||||
{
|
||||
$this->setEtag(base64_encode(hash('sha256', $this->content, true)));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the Content-Disposition header with the given filename.
|
||||
*
|
||||
* @param string $disposition ResponseHeaderBag::DISPOSITION_INLINE or ResponseHeaderBag::DISPOSITION_ATTACHMENT
|
||||
* @param string $filename Optionally use this UTF-8 encoded filename instead of the real name of the file
|
||||
* @param string $filenameFallback A fallback filename, containing only ASCII characters. Defaults to an automatically encoded filename
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setContentDisposition($disposition, $filename, $filenameFallback = '')
|
||||
{
|
||||
if ('' === $filenameFallback && (!preg_match('/^[\x20-\x7e]*$/', $filename) || false !== strpos($filename, '%'))) {
|
||||
$encoding = mb_detect_encoding($filename, null, true) ?: '8bit';
|
||||
|
||||
for ($i = 0, $filenameLength = mb_strlen($filename, $encoding); $i < $filenameLength; ++$i) {
|
||||
$char = mb_substr($filename, $i, 1, $encoding);
|
||||
|
||||
if ('%' === $char || \ord($char) < 32 || \ord($char) > 126) {
|
||||
$filenameFallback .= '_';
|
||||
} else {
|
||||
$filenameFallback .= $char;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$dispositionHeader = $this->headers->makeDisposition($disposition, $filename, $filenameFallback);
|
||||
$this->headers->set('Content-Disposition', $dispositionHeader);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
81
src/Services/LabelSystem/LabelGenerator.php
Normal file
81
src/Services/LabelSystem/LabelGenerator.php
Normal file
|
@ -0,0 +1,81 @@
|
|||
<?php
|
||||
/**
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published
|
||||
* by the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace App\Services\LabelSystem;
|
||||
|
||||
|
||||
use App\Entity\Contracts\NamedElementInterface;
|
||||
use App\Entity\LabelSystem\LabelOptions;
|
||||
use App\Entity\Parts\Part;
|
||||
use App\Services\ElementTypeNameGenerator;
|
||||
use Dompdf\Dompdf;
|
||||
use Twig\Environment;
|
||||
|
||||
class LabelGenerator
|
||||
{
|
||||
protected const CLASS_SUPPORT_MAPPING = [
|
||||
'part' => Part::class,
|
||||
];
|
||||
|
||||
public const MM_TO_POINTS_FACTOR = 2.83465;
|
||||
|
||||
protected $labelHTMLGenerator;
|
||||
|
||||
public function __construct(LabelHTMLGenerator $labelHTMLGenerator)
|
||||
{
|
||||
$this->labelHTMLGenerator = $labelHTMLGenerator;
|
||||
}
|
||||
|
||||
public function generateLabel(LabelOptions $options, object $element): string
|
||||
{
|
||||
if (!$this->supports($options, $element)) {
|
||||
throw new \InvalidArgumentException('The given options are not compatible with the given element!');
|
||||
}
|
||||
|
||||
$dompdf = new Dompdf();
|
||||
$dompdf->loadHtml($this->labelHTMLGenerator->getLabelHTML($options, $element));
|
||||
|
||||
$dompdf->setPaper($this->mmToPointsArray($options->getWidth(), $options->getHeight()));
|
||||
|
||||
$dompdf->render();
|
||||
return $dompdf->output();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the given LabelOptions can be used with $element.
|
||||
* @param LabelOptions $options
|
||||
* @param object $element
|
||||
* @return bool
|
||||
*/
|
||||
public function supports(LabelOptions $options, object $element)
|
||||
{
|
||||
$supported_type = $options->getSupportedElement();
|
||||
if (!isset(static::CLASS_SUPPORT_MAPPING[$supported_type])) {
|
||||
throw new \InvalidArgumentException('Supported type name of the Label options not known!');
|
||||
}
|
||||
|
||||
return is_a($element, static::CLASS_SUPPORT_MAPPING[$supported_type]);
|
||||
}
|
||||
|
||||
public function mmToPointsArray(float $width, float $height): array
|
||||
{
|
||||
return [0, 0, $width * self::MM_TO_POINTS_FACTOR, $height * self::MM_TO_POINTS_FACTOR];
|
||||
}
|
||||
}
|
55
src/Services/LabelSystem/LabelHTMLGenerator.php
Normal file
55
src/Services/LabelSystem/LabelHTMLGenerator.php
Normal file
|
@ -0,0 +1,55 @@
|
|||
<?php
|
||||
/**
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published
|
||||
* by the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace App\Services\LabelSystem;
|
||||
|
||||
use App\Entity\Contracts\NamedElementInterface;
|
||||
use App\Entity\LabelSystem\LabelOptions;
|
||||
use App\Services\ElementTypeNameGenerator;
|
||||
use Twig\Environment;
|
||||
|
||||
class LabelHTMLGenerator
|
||||
{
|
||||
protected $twig;
|
||||
protected $elementTypeNameGenerator;
|
||||
|
||||
public function __construct(ElementTypeNameGenerator $elementTypeNameGenerator, Environment $twig)
|
||||
{
|
||||
$this->twig = $twig;
|
||||
$this->elementTypeNameGenerator = $elementTypeNameGenerator;
|
||||
}
|
||||
|
||||
public function getLabelHTML(LabelOptions $options, object $element): string
|
||||
{
|
||||
return $this->twig->render('labels/base_label.html.twig', [
|
||||
'meta_title' => $this->getPDFTitle($options, $element),
|
||||
'lines' => $options->getLines(),
|
||||
]);
|
||||
}
|
||||
|
||||
protected function getPDFTitle(LabelOptions $options, object $element)
|
||||
{
|
||||
if ($element instanceof NamedElementInterface) {
|
||||
return $this->elementTypeNameGenerator->getTypeNameCombination($element, false);
|
||||
}
|
||||
|
||||
return 'Part-DB label';
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue