|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics) |
|
6
|
|
|
* |
|
7
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
8
|
|
|
* it under the terms of the GNU Affero General Public License as published |
|
9
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
|
10
|
|
|
* (at your option) any later version. |
|
11
|
|
|
* |
|
12
|
|
|
* This program is distributed in the hope that it will be useful, |
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
* GNU Affero General Public License for more details. |
|
16
|
|
|
* |
|
17
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
18
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace App\Services\LabelSystem; |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
use App\Entity\Contracts\NamedElementInterface; |
|
25
|
|
|
use App\Entity\LabelSystem\LabelOptions; |
|
26
|
|
|
use App\Entity\Parts\Part; |
|
27
|
|
|
use App\Entity\Parts\PartLot; |
|
28
|
|
|
use App\Entity\Parts\Storelocation; |
|
29
|
|
|
use App\Services\ElementTypeNameGenerator; |
|
30
|
|
|
use Dompdf\Dompdf; |
|
31
|
|
|
use Twig\Environment; |
|
32
|
|
|
|
|
33
|
|
|
final class LabelGenerator |
|
34
|
|
|
{ |
|
35
|
|
|
public const CLASS_SUPPORT_MAPPING = [ |
|
36
|
|
|
'part' => Part::class, |
|
37
|
|
|
'part_lot' => PartLot::class, |
|
38
|
|
|
'storelocation' => Storelocation::class, |
|
39
|
|
|
]; |
|
40
|
|
|
|
|
41
|
|
|
public const MM_TO_POINTS_FACTOR = 2.83465; |
|
42
|
|
|
|
|
43
|
|
|
private $labelHTMLGenerator; |
|
44
|
|
|
|
|
45
|
|
|
public function __construct(LabelHTMLGenerator $labelHTMLGenerator) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->labelHTMLGenerator = $labelHTMLGenerator; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param LabelOptions $options |
|
52
|
|
|
* @param object|object[] $elements An element or an array of elements for which labels should be generated |
|
53
|
|
|
* @return string |
|
54
|
|
|
*/ |
|
55
|
|
|
public function generateLabel(LabelOptions $options, $elements): string |
|
56
|
|
|
{ |
|
57
|
|
|
if (!is_array($elements) && !is_object($elements)) { |
|
58
|
|
|
throw new \InvalidArgumentException('$element must be an object or an array of objects!'); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
if (!is_array($elements)) { |
|
62
|
|
|
$elements = [$elements]; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
foreach ($elements as $element) { |
|
66
|
|
|
if (!$this->supports($options, $element)) { |
|
67
|
|
|
throw new \InvalidArgumentException('The given options are not compatible with the given element!'); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
$dompdf = new Dompdf(); |
|
73
|
|
|
$dompdf->setPaper($this->mmToPointsArray($options->getWidth(), $options->getHeight())); |
|
74
|
|
|
$dompdf->loadHtml($this->labelHTMLGenerator->getLabelHTML($options, $elements)); |
|
75
|
|
|
$dompdf->render(); |
|
76
|
|
|
return $dompdf->output(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Check if the given LabelOptions can be used with $element. |
|
81
|
|
|
* @param LabelOptions $options |
|
82
|
|
|
* @param object $element |
|
83
|
|
|
* @return bool |
|
84
|
|
|
*/ |
|
85
|
|
|
public function supports(LabelOptions $options, object $element) |
|
86
|
|
|
{ |
|
87
|
|
|
$supported_type = $options->getSupportedElement(); |
|
88
|
|
|
if (!isset(static::CLASS_SUPPORT_MAPPING[$supported_type])) { |
|
89
|
|
|
throw new \InvalidArgumentException('Supported type name of the Label options not known!'); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return is_a($element, static::CLASS_SUPPORT_MAPPING[$supported_type]); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Converts width and height given in mm to an size array, that can be used by DOMPDF for page size |
|
97
|
|
|
* @param float $width The width of the paper |
|
98
|
|
|
* @param float $height The height of the paper |
|
99
|
|
|
* @return float[] |
|
100
|
|
|
*/ |
|
101
|
|
|
public function mmToPointsArray(float $width, float $height): array |
|
102
|
|
|
{ |
|
103
|
|
|
return [0.0, 0.0, $width * self::MM_TO_POINTS_FACTOR, $height * self::MM_TO_POINTS_FACTOR]; |
|
104
|
|
|
} |
|
105
|
|
|
} |