FPDF and FPDI watermarks are repeated at an angle in the X-Y direction.
<p>Using setasign/FPDF and setasign/FPDI libraries, I need to add a watermark and custom footer overlay on any PDF document (each page needs to be watermarked). The original PDF file is stored on the server, and the process must occur when the file is requested (as they contain the date and time of the request). <br /><br />I successfully added the required footer including the requested date and time, thanks to the following code. The code also prints a single occurrence of the watermark diagonally across the page, but I would really like to have another behavior: repeating the string diagonally, like shown on this image (e.g. my Strings are generated dynamically). It doesn't matter whether the string starts or ends "outside" the page, as long as it repeats on multiple lines, and those lines must be equidistantly spaced. <br /><br />Do you have any clues on where to start? <br /><br />Working code as of today: </p><p><br /></p>
<pre class="brush:php;toolbar:false;"><?php
use setasignFpdiFpdi;
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 . ' - property of company';
addWatermark(120, 220, $watermarkText, 45, $pdf);
$pdf->SetXY(25, 25);
}
$pdf->Output();</pre>
<p><em>Watermark code from here: https://phppot.com/php/php-watermark-pdf/ and adapted.<br /><br />Basic composer.json file for testing: <br /></em></p><p><br />< /p>
<pre class="brush:php;toolbar:false;">{
"require": {
"setasign/fpdf": "1.8.*",
"setasign/fpdi": "^2.3"
}
}</pre>
<p>Thank you</p>