33namespace Drupal \os2forms_attachment ;
44
55use Drupal \Core \Entity \EntityInterface ;
6+ use Drupal \Core \File \FileExists ;
7+ use Drupal \Core \File \FileSystemInterface ;
8+ use Drupal \Core \StringTranslation \TranslationInterface ;
9+ use Drupal \entity_print \Event \PreSendPrintEvent ;
10+ use Drupal \entity_print \Event \PrintEvents ;
611use Drupal \entity_print \Plugin \PrintEngineInterface ;
712use Drupal \entity_print \PrintBuilder ;
13+ use Drupal \entity_print \Renderer \RendererFactoryInterface ;
14+ use Symfony \Component \EventDispatcher \EventDispatcherInterface ;
815
916/**
1017 * The OS2Forms attachment print builder service.
1118 */
1219class Os2formsAttachmentPrintBuilder extends PrintBuilder {
1320
21+ /**
22+ * {@inheritdoc}
23+ */
24+ public function __construct (RendererFactoryInterface $ renderer_factory , EventDispatcherInterface $ event_dispatcher , TranslationInterface $ string_translation , protected readonly FileSystemInterface $ file_system ) {
25+ parent ::__construct ($ renderer_factory , $ event_dispatcher , $ string_translation );
26+ }
27+
1428 /**
1529 * {@inheritdoc}
1630 */
@@ -27,10 +41,56 @@ public function printHtml(EntityInterface $entity, $use_default_css = TRUE, $opt
2741 return $ renderer ->generateHtml ([$ entity ], $ render , $ use_default_css , $ optimize_css );
2842 }
2943
44+ /**
45+ * Modified version of the original savePrintable() function.
46+ *
47+ * The only difference is modified call to prepareRenderer with digitalPost
48+ * flag TRUE.
49+ *
50+ * @see PrintBuilder::savePrintable()
51+ *
52+ * @return string
53+ * FALSE or the URI to the file. E.g. public://my-file.pdf.
54+ */
55+ public function savePrintableDigitalSignature (array $ entities , PrintEngineInterface $ print_engine , $ scheme = 'public ' , $ filename = FALSE , $ use_default_css = TRUE ) {
56+ $ renderer = $ this ->prepareRenderer ($ entities , $ print_engine , $ use_default_css , TRUE );
57+
58+ // Allow other modules to alter the generated Print object.
59+ $ this ->dispatcher ->dispatch (new PreSendPrintEvent ($ print_engine , $ entities ), PrintEvents::PRE_SEND );
60+
61+ // If we didn't have a URI passed in the generate one.
62+ if (!$ filename ) {
63+ $ filename = $ renderer ->getFilename ($ entities ) . '. ' . $ print_engine ->getExportType ()->getFileExtension ();
64+ }
65+
66+ $ uri = "$ scheme:// $ filename " ;
67+
68+ // Save the file.
69+ return $ this ->file_system ->saveData ($ print_engine ->getBlob (), $ uri , FileExists::Replace);
70+ }
71+
3072 /**
3173 * {@inheritdoc}
3274 */
33- protected function prepareRenderer (array $ entities , PrintEngineInterface $ print_engine , $ use_default_css ) {
75+
76+ /**
77+ * Override prepareRenderer() the print engine with the passed entities.
78+ *
79+ * @param array $entities
80+ * An array of entities.
81+ * @param \Drupal\entity_print\Plugin\PrintEngineInterface $print_engine
82+ * The print engine.
83+ * @param bool $use_default_css
84+ * TRUE if we want the default CSS included.
85+ * @param bool $digitalSignature
86+ * If the digital signature message needs to be added.
87+ *
88+ * @return \Drupal\entity_print\Renderer\RendererInterface
89+ * A print renderer.
90+ *
91+ * @see PrintBuilder::prepareRenderer
92+ */
93+ protected function prepareRenderer (array $ entities , PrintEngineInterface $ print_engine , $ use_default_css , $ digitalSignature = FALSE ) {
3494 if (empty ($ entities )) {
3595 throw new \InvalidArgumentException ('You must pass at least 1 entity ' );
3696 }
@@ -50,6 +110,9 @@ protected function prepareRenderer(array $entities, PrintEngineInterface $print_
50110 // structure. That margin is automatically added in PDF and PDF only.
51111 $ generatedHtml = (string ) $ renderer ->generateHtml ($ entities , $ render , $ use_default_css , TRUE );
52112 $ generatedHtml .= "<style>fieldset legend {margin-left: -12px;}</style> " ;
113+ if ($ digitalSignature ) {
114+ $ generatedHtml .= $ this ->t ('You can validate the signature on this PDF file via validering.nemlog-in.dk. ' );
115+ }
53116
54117 $ print_engine ->addPage ($ generatedHtml );
55118
0 commit comments