-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPdfReportWriter.php
More file actions
50 lines (41 loc) · 1.29 KB
/
Copy pathPdfReportWriter.php
File metadata and controls
50 lines (41 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
namespace App\Export;
use App\Entity\Validation;
use Knp\Snappy\Pdf;
use Twig\Environment;
class PdfReportWriter
{
public function __construct(
private readonly Pdf $snappy,
private readonly Environment $twig,
) {}
/**
* @param Validation $validation
* @return string Raw PDF binary content
*/
public function generate(Validation $validation): string
{
$entries = json_decode($validation->getResults());
$hasErrors = (bool) array_filter(
$entries,
static fn(array $e): bool => strtolower($e['level']) === 'error'
);
$order = ['error', 'warning', 'info'];
$grouped = [];
foreach ($entries as $entry) {
$grouped[$entry['level']][] = $entry;
}
$html = $this->twig->render('pdfModel.html.twig', [
'groupedEntries' => $grouped,
'hasErrors' => $hasErrors,
]);
return $this->snappy->getOutputFromHtml($html, [
'encoding' => 'UTF-8',
'enable-local-file-access' => true,
'margin-top' => '10mm',
'margin-bottom' => '10mm',
'margin-left' => '12mm',
'margin-right' => '12mm',
]);
}
}