1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Royopa\AlphaPDF; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Security\Core\User\AdvancedUserInterface; |
6
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
7
|
|
|
use Symfony\Component\Filesystem\Exception\FileNotFoundException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* WaterMark |
11
|
|
|
* |
12
|
|
|
*/ |
13
|
|
|
class WaterMark |
14
|
|
|
{ |
15
|
|
|
protected $pdf; |
16
|
|
|
protected $waterMarkBigText; |
17
|
|
|
protected $waterMarkSmallText; |
18
|
|
|
|
19
|
|
|
public function __construct($sourceFile, AdvancedUserInterface $user) |
20
|
|
|
{ |
21
|
|
|
$fs = new Filesystem(); |
22
|
|
|
|
23
|
|
|
if (! $fs->exists($sourceFile)) { |
24
|
|
|
throw new FileNotFoundException('File not found.'); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
$this->pdf = new AlphaPDF(); |
28
|
|
|
|
29
|
|
|
// set the source file and gets the number of pages |
30
|
|
|
$this->pdf->setNumPages($this->pdf->setSourceFile($sourceFile)); |
31
|
|
|
|
32
|
|
|
$this->waterMarkBigText = "C O N F I D E N C I A L"; |
33
|
|
|
|
34
|
|
|
$now = new \DateTime('now'); |
35
|
|
|
$this->waterMarkSmallText = 'Impresso por - ' . $user->getUsername() . ' em ' . $now->format('d/m/Y H:i'); |
36
|
|
|
|
37
|
|
|
if (method_exists($user, 'getName')) { |
38
|
|
|
$this->waterMarkSmallText = 'Impresso por - ' . $user->getUsername() . ' - ' . $user->getName() . ' em ' . $now->format('d/m/Y H:i'); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function getPdf() |
43
|
|
|
{ |
44
|
|
|
return $this->pdf; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function doWaterMark() |
48
|
|
|
{ |
49
|
|
|
for ($i = 1; $i <= $this->pdf->getNumPages(); $i++) { |
50
|
|
|
$this->pdf->addPage();//<- moved from outside loop |
51
|
|
|
|
52
|
|
|
$tplidx = $this->pdf->importPage($i); |
53
|
|
|
$this->pdf->useTemplate($tplidx, 10, 20, 200); |
54
|
|
|
|
55
|
|
|
//definir o tipo de texto |
56
|
|
|
$this->pdf->SetTextColor(204, 0, 0); |
57
|
|
|
// set alpha to semi-transparency |
58
|
|
|
$this->pdf->SetAlpha(0.5); |
59
|
|
|
|
60
|
|
|
// Big Text |
61
|
|
|
$this->pdf->SetFont('Arial', 'B', 60); |
62
|
|
|
$this->pdf->SetX(-1); |
63
|
|
|
$this->pdf->SetLeftMargin(-78); |
64
|
|
|
$this->_rotate(55); |
65
|
|
|
$this->pdf->Write(0, $this->waterMarkBigText); |
66
|
|
|
|
67
|
|
|
// Small Text |
68
|
|
|
$this->pdf->SetFont('Arial', 'B', 13); |
69
|
|
|
$this->pdf->SetX(225); |
70
|
|
|
$this->pdf->SetY(22); |
71
|
|
|
$this->pdf->SetLeftMargin(-35); |
72
|
|
|
$this->pdf->Write(0, $this->waterMarkSmallText); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $this->pdf; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param integer $angle |
80
|
|
|
*/ |
81
|
|
|
protected function _rotate($angle, $x=-1, $y=-1) |
82
|
|
|
{ |
83
|
|
|
if ($x==-1) { |
84
|
|
|
$x=$this->pdf->getX(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if ($y==-1) { |
88
|
|
|
$y=$this->pdf->getY(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if ($this->pdf->getAngle() != 0) { |
92
|
|
|
$this->pdf->_out('Q'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$this->pdf->setAngle($angle); |
96
|
|
|
|
97
|
|
|
if ($angle != 0) { |
98
|
|
|
$angle *= M_PI/180; |
99
|
|
|
$c = cos($angle); |
100
|
|
|
$s = sin($angle); |
101
|
|
|
$cx = $x * $this->pdf->getK(); |
102
|
|
|
$cy = ($this->pdf->getH() - $y) * $this->pdf->getK(); |
103
|
|
|
|
104
|
|
|
$this->pdf->_out(sprintf( |
105
|
|
|
'q %.5f %.5f %.5f %.5f %.2f %.2f cm 1 0 0 1 %.2f %.2f cm', |
106
|
|
|
$c,$s,-$s,$c,$cx,$cy,-$cx,-$cy) |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|