Skip to content

Commit 1d097ac

Browse files
committed
Add transformations for pdf content, fix pdf template usage
1 parent 602ab9e commit 1d097ac

File tree

1 file changed

+112
-3
lines changed

1 file changed

+112
-3
lines changed

src/wcmf/lib/pdf/PDF.php

+112-3
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,27 @@ class PDF extends Fpdi {
4848

4949
private $pageStarted = false;
5050
private $pageEnded = false;
51+
private $usedTemplateId = null;
5152

5253
/**
5354
* Overridden to set the template on the page
54-
* TODO check if this is still necessary, if useTemplate is called in PDFTemplate::output() now
5555
*/
5656
public function Header() {
5757
parent::Header();
58-
if ($this->currentTemplateId) {
59-
$this->useTemplate($this->currentTemplateId);
58+
if ($this->usedTemplateId) {
59+
$this->useTemplate($this->usedTemplateId);
6060
}
6161
}
6262

63+
/**
64+
* Overridden to set the template id
65+
* @param $templateId
66+
*/
67+
public function useTemplate($tpl, $x=0, $y=0, $width=null, $height=null, $adjustPageSize=false) {
68+
parent::useTemplate($tpl);
69+
$this->usedTemplateId = $tpl;
70+
}
71+
6372
/**
6473
* Call this method when rendering a new page
6574
*/
@@ -148,6 +157,106 @@ public function UnsetClipping() {
148157
$this->_out('Q');
149158
}
150159

160+
/**
161+
* The following code is taken from FPDF Add-On 'Transformations'
162+
* @see http://fpdf.de/Addon-79-transformations.html
163+
*/
164+
165+
/**
166+
* Start a transformation (Scale, Translate, Rotate calls follow)
167+
*/
168+
public function StartTransform() {
169+
// save the current graphic state
170+
$this->_out('q');
171+
}
172+
173+
/**
174+
* Scale the following content
175+
* @param $s_x
176+
* @param $s_y
177+
* @param $x
178+
* @param $y
179+
*/
180+
public function Scale($s_x, $s_y, $x='', $y='') {
181+
if ($x === '') {
182+
$x = $this->x;
183+
}
184+
if ($y === '') {
185+
$y = $this->y;
186+
}
187+
if ($s_x == 0 || $s_y == 0) {
188+
$this->Error('Please use values unequal to zero for Scaling');
189+
}
190+
$y = ($this->h-$y)*$this->k;
191+
$x *= $this->k;
192+
// calculate elements of transformation matrix
193+
$s_x /= 100;
194+
$s_y /= 100;
195+
$tm[0] = $s_x;
196+
$tm[1] = 0;
197+
$tm[2] = 0;
198+
$tm[3] = $s_y;
199+
$tm[4] = $x*(1-$s_x);
200+
$tm[5] = $y*(1-$s_y);
201+
// scale the coordinate system
202+
$this->Transform($tm);
203+
}
204+
205+
/**
206+
* Move the following content
207+
* @param $t_x
208+
* @param $t_y
209+
*/
210+
public function Translate($t_x, $t_y) {
211+
// calculate elements of transformation matrix
212+
$tm[0]=1;
213+
$tm[1]=0;
214+
$tm[2]=0;
215+
$tm[3]=1;
216+
$tm[4]=$t_x*$this->k;
217+
$tm[5]=-$t_y*$this->k;
218+
// translate the coordinate system
219+
$this->Transform($tm);
220+
}
221+
222+
/**
223+
* Rotate the following content
224+
* @param $angle
225+
* @param $x
226+
* @param $y
227+
*/
228+
public function Rotate($angle, $x='', $y='') {
229+
if ($x === '') {
230+
$x = $this->x;
231+
}
232+
if ($y === '') {
233+
$y = $this->y;
234+
}
235+
$y = ($this->h-$y)*$this->k;
236+
$x *= $this->k;
237+
// calculate elements of transformation matrix
238+
$tm[0] = cos(deg2rad($angle));
239+
$tm[1] = sin(deg2rad($angle));
240+
$tm[2] = -$tm[1];
241+
$tm[3] = $tm[0];
242+
$tm[4] = $x+$tm[1]*$y-$tm[0]*$x;
243+
$tm[5] = $y-$tm[0]*$y-$tm[1]*$x;
244+
// rotate the coordinate system around ($x,$y)
245+
$this->Transform($tm);
246+
}
247+
248+
private function Transform($tm) {
249+
$this->_out(sprintf('%.3F %.3F %.3F %.3F %.3F %.3F cm', $tm[0], $tm[1], $tm[2], $tm[3], $tm[4], $tm[5]));
250+
}
251+
252+
/**
253+
* End a transformation
254+
*/
255+
function StopTransform() {
256+
// restore previous graphic state
257+
$this->_out('Q');
258+
}
259+
151260
/**
152261
* The following code is taken from FPDF Add-On 'Table with MultiCells'
153262
* @see http://fpdf.de/Addon-3-table-with-multicells.html

0 commit comments

Comments
 (0)