Skip to content

Commit

Permalink
Merge pull request #1779 from JortvD/feature/tag-course-document
Browse files Browse the repository at this point in the history
Implement watermark tag
  • Loading branch information
tomudding authored Feb 1, 2024
2 parents c3ad38b + 12188fc commit 9a4d146
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions config/autoload/local.development.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
7 changes: 7 additions & 0 deletions config/autoload/local.production.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
3 changes: 2 additions & 1 deletion module/Application/src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,9 @@ public function getServiceConfig(): array
/** @var AuthenticationService<UserSession, UserAdapter> $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'];
Expand Down
43 changes: 40 additions & 3 deletions module/Application/src/Service/WatermarkService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {
}

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit 9a4d146

Please sign in to comment.