forked from OS2Forms/os2forms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOs2formsAttachmentPrintBuilder.php
More file actions
122 lines (103 loc) · 4.28 KB
/
Os2formsAttachmentPrintBuilder.php
File metadata and controls
122 lines (103 loc) · 4.28 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
namespace Drupal\os2forms_attachment;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\File\FileExists;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\entity_print\Event\PreSendPrintEvent;
use Drupal\entity_print\Event\PrintEvents;
use Drupal\entity_print\Plugin\PrintEngineInterface;
use Drupal\entity_print\PrintBuilder;
use Drupal\entity_print\Renderer\RendererFactoryInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/**
* The OS2Forms attachment print builder service.
*/
class Os2formsAttachmentPrintBuilder extends PrintBuilder {
/**
* {@inheritdoc}
*/
public function __construct(RendererFactoryInterface $renderer_factory, EventDispatcherInterface $event_dispatcher, TranslationInterface $string_translation, protected readonly FileSystemInterface $file_system) {
parent::__construct($renderer_factory, $event_dispatcher, $string_translation);
}
/**
* {@inheritdoc}
*/
public function printHtml(EntityInterface $entity, $use_default_css = TRUE, $optimize_css = TRUE) {
$renderer = $this->rendererFactory->create([$entity]);
$content[] = $renderer->render([$entity]);
$render = [
'#theme' => 'entity_print__' . $entity->getEntityTypeId() . '__' . $entity->bundle(),
'#title' => $entity->label(),
'#content' => $content,
'#attached' => [],
];
return $renderer->generateHtml([$entity], $render, $use_default_css, $optimize_css);
}
/**
* Modified version of the original savePrintable() function.
*
* The only difference is modified call to prepareRenderer with digitalPost
* flag TRUE.
*
* @see PrintBuilder::savePrintable()
*
* @return string
* FALSE or the URI to the file. E.g. public://my-file.pdf.
*/
public function savePrintableDigitalSignature(array $entities, PrintEngineInterface $print_engine, $scheme = 'public', $filename = FALSE, $use_default_css = TRUE) {
$renderer = $this->prepareRenderer($entities, $print_engine, $use_default_css, TRUE);
// Allow other modules to alter the generated Print object.
$this->dispatcher->dispatch(new PreSendPrintEvent($print_engine, $entities), PrintEvents::PRE_SEND);
// If we didn't have a URI passed in the generate one.
if (!$filename) {
$filename = $renderer->getFilename($entities) . '.' . $print_engine->getExportType()->getFileExtension();
}
$uri = "$scheme://$filename";
// Save the file.
return $this->file_system->saveData($print_engine->getBlob(), $uri, FileExists::Replace);
}
/**
* {@inheritdoc}
*/
/**
* Override prepareRenderer() the print engine with the passed entities.
*
* @param array $entities
* An array of entities.
* @param \Drupal\entity_print\Plugin\PrintEngineInterface $print_engine
* The print engine.
* @param bool $use_default_css
* TRUE if we want the default CSS included.
* @param bool $digitalSignature
* If the digital signature message needs to be added.
*
* @return \Drupal\entity_print\Renderer\RendererInterface
* A print renderer.
*
* @see PrintBuilder::prepareRenderer
*/
protected function prepareRenderer(array $entities, PrintEngineInterface $print_engine, $use_default_css, $digitalSignature = FALSE) {
if (empty($entities)) {
throw new \InvalidArgumentException('You must pass at least 1 entity');
}
$renderer = $this->rendererFactory->create($entities);
$content = $renderer->render($entities);
$first_entity = reset($entities);
$render = [
'#theme' => 'entity_print__' . $first_entity->getEntityTypeId() . '__' . $first_entity->bundle(),
'#title' => $first_entity->label(),
'#content' => $content,
'#attached' => [],
];
// Adding hardcoded negative margin to avoid margins in <fieldset> <legend>
// structure. That margin is automatically added in PDF and PDF only.
$generatedHtml = (string) $renderer->generateHtml($entities, $render, $use_default_css, TRUE);
$generatedHtml .= "<style>fieldset legend {margin-left: -12px;}</style>";
if ($digitalSignature) {
$generatedHtml .= $this->t('You can validate the signature on this PDF file via validering.nemlog-in.dk.');
}
$print_engine->addPage($generatedHtml);
return $renderer;
}
}