Commit 06e41b38 by Mohammad Izzat Johari

Add Watermark

parent 09f65bff
...@@ -447,7 +447,7 @@ class Pkcs11Command ...@@ -447,7 +447,7 @@ class Pkcs11Command
throw new \Exception('Failed to decrypt PDF content.'); throw new \Exception('Failed to decrypt PDF content.');
} }
$fpdi = new Fpdi(); $fpdi = new MyPdf();
// Write decrypted bytes to a memory stream // Write decrypted bytes to a memory stream
$pdfStream = fopen('php://memory', 'r+'); $pdfStream = fopen('php://memory', 'r+');
...@@ -465,6 +465,24 @@ class Pkcs11Command ...@@ -465,6 +465,24 @@ class Pkcs11Command
$fpdi->AddPage($orientation, [$size['width'], $size['height']]); $fpdi->AddPage($orientation, [$size['width'], $size['height']]);
$fpdi->useTemplate($tplIdx); $fpdi->useTemplate($tplIdx);
// Set watermark style
$fpdi->SetFont('Helvetica', '', 10);
// $fpdi->SetTextColor(200, 200, 200); // light gray
$fpdi->SetTextColor(245, 245, 245);
// Create loop stripes across page
$watermarkText = auth()->user()->name . "|" . date("d/m/y") . "|" . date("h:i:s A");
$angle = 45; // diagonal
$spacing = 80; // distance between stripes
// Loop and place watermark repeatedly
for ($x = -50; $x < $size['width'] + 200; $x += $spacing) {
for ($y = 0; $y < $size['height'] + 200; $y += $spacing) {
$fpdi->Rotate($angle, $x, $y);
$fpdi->Text($x, $y, $watermarkText);
$fpdi->Rotate(0); // reset rotation
}
}
} }
// Output to string (S = string) // Output to string (S = string)
...@@ -491,3 +509,35 @@ class Pkcs11Command ...@@ -491,3 +509,35 @@ class Pkcs11Command
} }
} }
} }
class MyPdf extends Fpdi {
protected $angle = 0;
function Rotate($angle, $x = -1, $y = -1) {
if ($x == -1) $x = $this->x;
if ($y == -1) $y = $this->y;
if ($this->angle != 0) {
$this->_out('Q');
}
$this->angle = $angle;
if ($angle != 0) {
$angle *= M_PI/180;
$c = cos($angle);
$s = sin($angle);
$cx = $x * $this->k;
$cy = ($this->h - $y) * $this->k;
$this->_out(sprintf(
'q %.5f %.5f %.5f %.5f %.5f %.5f cm 1 0 0 1 %.5f %.5f cm',
$c, $s, -$s, $c, $cx, $cy, -$cx, -$cy
));
}
}
function _endpage() {
if ($this->angle != 0) {
$this->angle = 0;
$this->_out('Q');
}
parent::_endpage();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment