From dbfebc367285eb117fc14e0cc2d79f7507d4e534 Mon Sep 17 00:00:00 2001 From: Jort van Driel <10116429+JortvD@users.noreply.github.com> Date: Tue, 9 Jan 2024 16:27:35 +0000 Subject: [PATCH 1/3] Implement watermark tag --- .env.dist | 3 +++ config/autoload/local.development.php.dist | 7 +++++ config/autoload/local.production.php.dist | 7 +++++ module/Application/src/Module.php | 3 ++- .../src/Service/WatermarkService.php | 27 ++++++++++++++++++- 5 files changed, 45 insertions(+), 2 deletions(-) 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..c92d5c757d 100644 --- a/module/Application/src/Service/WatermarkService.php +++ b/module/Application/src/Service/WatermarkService.php @@ -29,6 +29,7 @@ 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 @@ -36,6 +37,7 @@ class WatermarkService public function __construct( private readonly AuthenticationService $authService, private readonly string $remoteAddress, + private readonly array $watermarkConfig, ) { } @@ -144,7 +146,30 @@ public function watermarkPdf( ), ); - return $tempFlatFile; + $pdf = new Fpdi(); + $pdf->setTitle($fileName); + $pages = $pdf->setSourceFile($tempFlatFile); + + $tag = $this->watermarkConfig['tag']; + + // Write the tag on the first page page a tag at the top left corner with the value of local variable $tag + $pdf->setPrintHeader(false); + $pdf->setPrintFooter(false); + $templateIndex = $pdf->importPage(1); + $templateSpecs = $pdf->getTemplateSize($templateIndex); + $pdf->AddPage($templateSpecs['orientation']); + $pdf->useTemplate($templateIndex, 0, 0, $templateSpecs['width'], $templateSpecs['height'], true); + $pdf->setFont(self::TAG_FONT, '', 6); + + // Set text color to the same color as the image at 0 0 + $pdf->setTextColor(255, 255, 255); + $pdf->setXY(0, 0); + $pdf->Write(0, $tag); + + $tempTaggedFile = $tempName . '-tagged.pdf'; + $pdf->Output($tempTaggedFile, 'F'); + + return $tempTaggedFile; } /** From 99ccc9389c211d4f98919ce59a720a9a11dee1d0 Mon Sep 17 00:00:00 2001 From: Jort van Driel <10116429+JortvD@users.noreply.github.com> Date: Tue, 9 Jan 2024 17:11:02 +0000 Subject: [PATCH 2/3] Add last pages and changed the watermark alpha --- .../src/Service/WatermarkService.php | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/module/Application/src/Service/WatermarkService.php b/module/Application/src/Service/WatermarkService.php index c92d5c757d..18f0975cca 100644 --- a/module/Application/src/Service/WatermarkService.php +++ b/module/Application/src/Service/WatermarkService.php @@ -33,6 +33,7 @@ class WatermarkService /** * @psalm-param TUserAuth $authService + * @psalm-param array{tag: string} $watermarkConfig */ public function __construct( private readonly AuthenticationService $authService, @@ -72,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(); @@ -160,12 +161,22 @@ public function watermarkPdf( $pdf->AddPage($templateSpecs['orientation']); $pdf->useTemplate($templateIndex, 0, 0, $templateSpecs['width'], $templateSpecs['height'], true); $pdf->setFont(self::TAG_FONT, '', 6); - + // Set text color to the same color as the image at 0 0 $pdf->setTextColor(255, 255, 255); $pdf->setXY(0, 0); $pdf->Write(0, $tag); + // add the rest of the pages + for ($page = 2; $page <= $pages; $page++) { + $templateIndex = $pdf->importPage($page); + $templateSpecs = $pdf->getTemplateSize($templateIndex); + $pdf->setPrintHeader(false); + $pdf->setPrintFooter(false); + $pdf->AddPage($templateSpecs['orientation']); + $pdf->useTemplate($templateIndex, 0, 0, $templateSpecs['width'], $templateSpecs['height'], true); + } + $tempTaggedFile = $tempName . '-tagged.pdf'; $pdf->Output($tempTaggedFile, 'F'); From 12188fcbc2e812c250a20d51a368312f4fd59b6e Mon Sep 17 00:00:00 2001 From: Tom Udding Date: Thu, 1 Feb 2024 17:58:37 +0100 Subject: [PATCH 3/3] Simplify tagged PDF generation code Nothing changes in terms of the underlying functionality, only some duplicate code is removed. --- .../src/Service/WatermarkService.php | 53 ++++++++++--------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/module/Application/src/Service/WatermarkService.php b/module/Application/src/Service/WatermarkService.php index 18f0975cca..a71271ca45 100644 --- a/module/Application/src/Service/WatermarkService.php +++ b/module/Application/src/Service/WatermarkService.php @@ -147,38 +147,39 @@ public function watermarkPdf( ), ); - $pdf = new Fpdi(); - $pdf->setTitle($fileName); - $pages = $pdf->setSourceFile($tempFlatFile); + // Construct final PDF. + $taggedPdf = new Fpdi(); + $taggedPdf->setTitle($fileName); + $pages = $taggedPdf->setSourceFile($tempFlatFile); $tag = $this->watermarkConfig['tag']; - // Write the tag on the first page page a tag at the top left corner with the value of local variable $tag - $pdf->setPrintHeader(false); - $pdf->setPrintFooter(false); - $templateIndex = $pdf->importPage(1); - $templateSpecs = $pdf->getTemplateSize($templateIndex); - $pdf->AddPage($templateSpecs['orientation']); - $pdf->useTemplate($templateIndex, 0, 0, $templateSpecs['width'], $templateSpecs['height'], true); - $pdf->setFont(self::TAG_FONT, '', 6); - - // Set text color to the same color as the image at 0 0 - $pdf->setTextColor(255, 255, 255); - $pdf->setXY(0, 0); - $pdf->Write(0, $tag); - - // add the rest of the pages - for ($page = 2; $page <= $pages; $page++) { - $templateIndex = $pdf->importPage($page); - $templateSpecs = $pdf->getTemplateSize($templateIndex); - $pdf->setPrintHeader(false); - $pdf->setPrintFooter(false); - $pdf->AddPage($templateSpecs['orientation']); - $pdf->useTemplate($templateIndex, 0, 0, $templateSpecs['width'], $templateSpecs['height'], true); + // 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'; - $pdf->Output($tempTaggedFile, 'F'); + $taggedPdf->Output($tempTaggedFile, 'F'); return $tempTaggedFile; }