33namespace Drupal \os2forms_attachment ;
44
55use Drupal \Core \Entity \EntityInterface ;
6+ use Drupal \Core \File \FileExists ;
7+ use Drupal \entity_print \Event \PreSendPrintEvent ;
8+ use Drupal \entity_print \Event \PrintEvents ;
69use Drupal \entity_print \Plugin \PrintEngineInterface ;
710use Drupal \entity_print \PrintBuilder ;
811
@@ -27,10 +30,56 @@ public function printHtml(EntityInterface $entity, $use_default_css = TRUE, $opt
2730 return $ renderer ->generateHtml ([$ entity ], $ render , $ use_default_css , $ optimize_css );
2831 }
2932
33+ /**
34+ * Modified version of the original savePrintable() function.
35+ *
36+ * The only difference is modified call to prepareRenderer with digitalPost flag
37+ * TRUE.
38+ *
39+ * @see PrintBuilder::savePrintable()
40+ *
41+ * @return string
42+ * FALSE or the URI to the file. E.g. public://my-file.pdf.
43+ */
44+ public function savePrintableDigitalSignature (array $ entities , PrintEngineInterface $ print_engine , $ scheme = 'public ' , $ filename = FALSE , $ use_default_css = TRUE ) {
45+ $ renderer = $ this ->prepareRenderer ($ entities , $ print_engine , $ use_default_css , TRUE );
46+
47+ // Allow other modules to alter the generated Print object.
48+ $ this ->dispatcher ->dispatch (new PreSendPrintEvent ($ print_engine , $ entities ), PrintEvents::PRE_SEND );
49+
50+ // If we didn't have a URI passed in the generate one.
51+ if (!$ filename ) {
52+ $ filename = $ renderer ->getFilename ($ entities ) . '. ' . $ print_engine ->getExportType ()->getFileExtension ();
53+ }
54+
55+ $ uri = "$ scheme:// $ filename " ;
56+
57+ // Save the file.
58+ return \Drupal::service ('file_system ' )->saveData ($ print_engine ->getBlob (), $ uri , FileExists::Replace);
59+ }
60+
3061 /**
3162 * {@inheritdoc}
3263 */
33- protected function prepareRenderer (array $ entities , PrintEngineInterface $ print_engine , $ use_default_css ) {
64+
65+ /**
66+ * Override prepareRenderer() the print engine with the passed entities.
67+ *
68+ * @see PrintBuilder::prepareRenderer
69+ *
70+ * @param array $entities
71+ * An array of entities.
72+ * @param \Drupal\entity_print\Plugin\PrintEngineInterface $print_engine
73+ * The print engine.
74+ * @param bool $use_default_css
75+ * TRUE if we want the default CSS included.
76+ * @param bool $digitalSignature
77+ * If the digital signature message needs to be added.
78+ *
79+ * @return \Drupal\entity_print\Renderer\RendererInterface
80+ * A print renderer.
81+ */
82+ protected function prepareRenderer (array $ entities , PrintEngineInterface $ print_engine , $ use_default_css , $ digitalSignature = false ) {
3483 if (empty ($ entities )) {
3584 throw new \InvalidArgumentException ('You must pass at least 1 entity ' );
3685 }
@@ -50,6 +99,9 @@ protected function prepareRenderer(array $entities, PrintEngineInterface $print_
5099 // structure. That margin is automatically added in PDF and PDF only.
51100 $ generatedHtml = (string ) $ renderer ->generateHtml ($ entities , $ render , $ use_default_css , TRUE );
52101 $ generatedHtml .= "<style>fieldset legend {margin-left: -12px;}</style> " ;
102+ if ($ digitalSignature ) {
103+ $ generatedHtml .= $ this ->t ('You can validate the signature on this PDF file via validering.nemlog-in.dk. ' );
104+ }
53105
54106 $ print_engine ->addPage ($ generatedHtml );
55107
0 commit comments