0

Working with the setasign/FPDF & setasign/FPDI libraries, I have to add a watermark and a custom footer overlay to any PDF document on the go (each page has to be watermarked). Original PDF files are stored on the server, and the process has to be done upon file requests (as they include the request datetime)

I managed to add the desired footer including the date and time of the request, thanks to the folowing code. This code also prints a single occurence of the watermark, diagonally on the page, however, I would really like another behavior : repeat the string diagonally, as it can be seen on this image (for the example, my string is dynamically generated). It does not matter if the string starts or ends "outside" the page, as long as it repeats itself on several lines, which have to be eaqually spaced.

Do you have any leads on where to start that ?

Working code as of today:

<?php

use setasign\Fpdi\Fpdi;

require_once('vendor/autoload.php');

class Watermarked_PDF extends Fpdi
{
    function Footer()
    {
        $this->SetY(-10);
        $this->SetFont('Arial', false, 8);
        $this->SetTextColor(28, 28, 28);
        $this->Cell(0, 15, 'File requested on : ' . date('r'), 0, 0, 'C');
    }
}

function addWatermark($x, $y, $watermarkText, $angle, $pdf)
{
    $angle = $angle * M_PI / 180;
    $c = cos($angle);
    $s = sin($angle);
    $cx = $x * 1;
    $cy = (300 - $y) * 1;
    $pdf->_out(sprintf('q %.5F %.5F %.5F %.5F %.2F %.2F cm 1 0 0 1 %.2F %.2F cm', $c, $s, -$s, $c, $cx, $cy, -$cx, -$cy));
    $pdf->Text($x, $y, $watermarkText);
    $pdf->_out('Q');
}

$pdf = new Watermarked_PDF();
$file_Path = 'documents/';
$file_Name = '13825_2023-07-04';
$pages_count = $pdf->setSourceFile($file_Path . $file_Name . '.pdf');
for ($i = 1; $i <= $pages_count; $i++) {
    $pdf->AddPage();
    $tplIdx = $pdf->importPage($i);
    $pdf->useTemplate($tplIdx, 0, 0);
    $pdf->SetFont('Arial', 'B', 15);
    $pdf->SetTextColor(175, 175, 175);
    $watermarkText = 'file #' . $file_Name . ' - propery of company';
    addWatermark(120, 220, $watermarkText, 45, $pdf);
    $pdf->SetXY(25, 25);
}
$pdf->Output();

Code for the watermark coming from here : https://phppot.com/php/php-watermark-pdf/ and adapted.

Basic composer.json file for tests:

{
    "require": {
        "setasign/fpdf": "1.8.*",
        "setasign/fpdi": "^2.3"
    }
}

Thanks for the help!

jijihbt
  • 35
  • 6

1 Answers1

0

I actually found a workaround, but quite an ugly one:

for ($i = 1; $i <= $pages_count; $i++) {
    $pdf->AddPage();
    $tplIdx = $pdf->importPage($i);
    $pdf->useTemplate($tplIdx, 0, 0);
    $pdf->SetFont('Arial', 'B', 15);
    $pdf->SetTextColor(175, 175, 175);
    $watermarkText = 'file #' . $file_Name . ' - propery of company';
    addWatermark(0, 0, $watermarkText, 45, $pdf);
    addWatermark(0, 50, $watermarkText, 45, $pdf);
    addWatermark(0, 100, $watermarkText, 45, $pdf);
    addWatermark(0, 150, $watermarkText, 45, $pdf);
    addWatermark(0, 200, $watermarkText, 45, $pdf);
    addWatermark(0, 250, $watermarkText, 45, $pdf);
    addWatermark(0, 300, $watermarkText, 45, $pdf);
    addWatermark(0, 350, $watermarkText, 45, $pdf);
    addWatermark(0, 400, $watermarkText, 45, $pdf);
    addWatermark(0, 450, $watermarkText, 45, $pdf);
    $pdf->SetXY(25, 25);
}

That updated loop, even if not optimized, does the job. I will now work on that way and adapt it for my purposes,

jijihbt
  • 35
  • 6