diff --git a/.env.dist b/.env.dist index b76e558a27..d25e1dbd1f 100644 --- a/.env.dist +++ b/.env.dist @@ -90,3 +90,6 @@ MYSQL_ALLOW_EMPTY_PASSWORD='yes' # This makes debugging with XDebug and PHPStorm easier PHP_IDE_CONFIG=serverName=gewis.nl + +# This is a tag for the course documents watermarking +WATERMARK_TAG=test123 diff --git a/config/autoload/local.development.php.dist b/config/autoload/local.development.php.dist index 4fb718a77b..8206c1cf2d 100644 --- a/config/autoload/local.development.php.dist +++ b/config/autoload/local.development.php.dist @@ -79,6 +79,13 @@ return [ 'supremum_api_key' => getenv('DOCKER_SUPREMUM_API_KEY'), ], + /** + * The config for watermarking + */ + 'watermark' => [ + 'tag' => getenv('WATERMARK_TAG'), + ], + /* * Path to folder in local filesystem available for browsing */ diff --git a/config/autoload/local.production.php.dist b/config/autoload/local.production.php.dist index 5e538f2398..13ee6e7c9e 100644 --- a/config/autoload/local.production.php.dist +++ b/config/autoload/local.production.php.dist @@ -79,6 +79,13 @@ return [ 'supremum_api_key' => getenv('DOCKER_SUPREMUM_API_KEY'), ], + /** + * The config for watermarking + */ + 'watermark' => [ + 'tag' => getenv('WATERMARK_TAG'), + ], + /* * Path to folder in local filesystem available for browsing */ diff --git a/module/Application/src/Module.php b/module/Application/src/Module.php index ae3f7ba757..a239ec9e36 100644 --- a/module/Application/src/Module.php +++ b/module/Application/src/Module.php @@ -168,8 +168,9 @@ public function getServiceConfig(): array /** @var AuthenticationService $authService */ $authService = $container->get('user_auth_user_service'); $remoteAddress = $container->get('user_remoteaddress'); + $watermarkConfig = $container->get('config')['watermark']; - return new WatermarkService($authService, $remoteAddress); + return new WatermarkService($authService, $remoteAddress, $watermarkConfig); }, 'application_get_languages' => static function () { return ['nl', 'en']; diff --git a/module/Application/src/Service/WatermarkService.php b/module/Application/src/Service/WatermarkService.php index 2973c7573f..a71271ca45 100644 --- a/module/Application/src/Service/WatermarkService.php +++ b/module/Application/src/Service/WatermarkService.php @@ -29,13 +29,16 @@ class WatermarkService private const FONT_SIZE_DIAGONAL = 32; private const FONT_SIZE_HORIZONTAL = 8; private const FONT = 'freesansb'; + private const TAG_FONT = 'times'; /** * @psalm-param TUserAuth $authService + * @psalm-param array{tag: string} $watermarkConfig */ public function __construct( private readonly AuthenticationService $authService, private readonly string $remoteAddress, + private readonly array $watermarkConfig, ) { } @@ -70,8 +73,8 @@ public function watermarkPdf( // Do the actual watermarking. $pdf->setFont(self::FONT, '', self::FONT_SIZE_DIAGONAL); $pdf->setTextColor(212, 0, 0); - // Set alpha to 50% for the diagonal watermark, the horizontal watermark will have a higher alpha. - $pdf->setAlpha(0.5); + // Set alpha to 35% for the diagonal watermark, the horizontal watermark will have a higher alpha. + $pdf->setAlpha(0.35); // Determine the position of the diagonal watermark, it should be (almost) centred on the page. $width = $pdf->getPageWidth(); @@ -144,7 +147,41 @@ public function watermarkPdf( ), ); - return $tempFlatFile; + // Construct final PDF. + $taggedPdf = new Fpdi(); + $taggedPdf->setTitle($fileName); + $pages = $taggedPdf->setSourceFile($tempFlatFile); + + $tag = $this->watermarkConfig['tag']; + + // We have to copy all pages to this new PDF. + for ($page = 1; $page <= $pages; $page++) { + $templateIndex = $taggedPdf->importPage($page); + $templateSpecs = $taggedPdf->getTemplateSize($templateIndex); + $taggedPdf->setPrintHeader(false); + $taggedPdf->setPrintFooter(false); + $taggedPdf->AddPage($templateSpecs['orientation']); + $taggedPdf->useTemplate($templateIndex, 0, 0, $templateSpecs['width'], $templateSpecs['height'], true); + + // Early exit if we are not working on the first page. + if (1 !== $page) { + continue; + } + + // Write the tag on the first page in the top left corner. + $taggedPdf->setFont(self::TAG_FONT, '', 6); + $taggedPdf->setTextColor(255, 255, 255); + + $taggedPdf->StartTransform(); + $taggedPdf->setXY(0, 0); + $taggedPdf->Write(0, $tag); + $taggedPdf->StopTransform(); + } + + $tempTaggedFile = $tempName . '-tagged.pdf'; + $taggedPdf->Output($tempTaggedFile, 'F'); + + return $tempTaggedFile; } /**