PDF.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2015 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
11 namespace wcmf\lib\pdf;
12 
13 if (!class_exists('FPDI')) {
14  throw new \wcmf\lib\config\ConfigurationException(
15  'wcmf\lib\pdf\PDF requires '.
16  'FPDI. If you are using composer, add setasign/fpdi '.
17  'as dependency to your project');
18 }
19 
20 /**
21  * PDF extends FPDF/FPDI.
22  *
23  * @note This class requires FPDI
24  *
25  * @author ingo herwig <ingo@wemove.com>
26  */
27 class PDF extends FPDI {
28 
29  var $_pageStarted = false;
30  var $_pageEnded = false;
31 
32  /**
33  * Overriden to set the template on the page
34  */
35  public function Header() {
36  parent::Header();
37  $this->useTemplate($this->tpl);
38  }
39 
40  /**
41  * Call this method when rendering a new page
42  */
43  public function startPage() {
44  $this->_pageStarted = true;
45  $this->_pageEnded = false;
46  }
47 
48  /**
49  * Call this method when rendering a page finished
50  */
51  public function endPage() {
52  $this->_pageEnded = true;
53  $this->_pageStarted = false;
54  }
55 
56  /**
57  * Determine if a new page started
58  * @return Boolean
59  */
60  public function isPageStarted() {
61  return $this->_pageStarted;
62  }
63 
64  /**
65  * Determine if a page finished
66  * @return Boolean
67  */
68  public function isPageEnded() {
69  return $this->_pageEnded;
70  }
71 
72  /**
73  * Move the render position down by given units
74  * @param $units The number of units to move
75  */
76  public function moveDown($units) {
77  $this->SetY($units+$this->GetY());
78  }
79 
80  /**
81  * Move the render position right by given units
82  * @param $units The number of units to move
83  */
84  public function moveRight($units) {
85  $this->SetX($units+$this->GetX());
86  }
87 
88  /**
89  * Computes the number of lines a MultiCell of width w will take
90  * instead of NbLines it correctly handles linebreaks
91  * @param $width The width
92  * @param $text The text
93  */
94  public function numberOfLines($width, $text) {
95  $nbLines = 0;
96  $lines = preg_split('/\n/', $text);
97  foreach ($lines as $line) {
98  $nbLines += $this->NbLines($width, $line);
99  }
100  return $nbLines;
101  }
102 
103  /**
104  * The following code is taken from FPDF Add-On 'Table with MultiCells'
105  * @see http://www.fpdf.de/downloads/addons/3/
106  */
107 
108  /**
109  * If the height h would cause an overflow, add a new page immediately
110  * @param $h The height
111  * @return Boolean whether a new page was inserted or not
112  */
113  public function CheckPageBreak($h) {
114  if($this->GetY()+$h>$this->PageBreakTrigger) {
115  $this->AddPage($this->CurOrientation);
116  return true;
117  }
118  return false;
119  }
120 
121  /**
122  * Computes the number of lines a MultiCell of width w will take
123  * @param $w The width
124  * @param $txt The text
125  */
126  public function NbLines($w, $txt) {
127  $cw=&$this->CurrentFont['cw'];
128  if($w==0) {
129  $w=$this->w-$this->rMargin-$this->x;
130  }
131  $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
132  $s=str_replace("\r",'',$txt);
133  $nb=strlen($s);
134  if($nb>0 && $s[$nb-1]=="\n") {
135  $nb--;
136  }
137  $sep=-1;
138  $i=0;
139  $j=0;
140  $l=0;
141  $nl=1;
142  while($i<$nb) {
143  $c=$s[$i];
144  if($c=="\n") {
145  $i++;
146  $sep=-1;
147  $j=$i;
148  $l=0;
149  $nl++;
150  continue;
151  }
152  if($c==' ') {
153  $sep=$i;
154  }
155  $l+=$cw[$c];
156  if($l>$wmax) {
157  if($sep==-1) {
158  if($i==$j) {
159  $i++;
160  }
161  }
162  else {
163  $i=$sep+1;
164  }
165  $sep=-1;
166  $j=$i;
167  $l=0;
168  $nl++;
169  }
170  else {
171  $i++;
172  }
173  }
174  return $nl;
175  }
176 }
177 ?>
numberOfLines($width, $text)
Computes the number of lines a MultiCell of width w will take instead of NbLines it correctly handles...
Definition: PDF.php:94
moveRight($units)
Move the render position right by given units.
Definition: PDF.php:84
isPageEnded()
Determine if a page finished.
Definition: PDF.php:68
PDF related interfaces and classes.
Definition: namespaces.php:37
CheckPageBreak($h)
The following code is taken from FPDF Add-On 'Table with MultiCells'.
Definition: PDF.php:113
startPage()
Call this method when rendering a new page.
Definition: PDF.php:43
Header()
Overriden to set the template on the page.
Definition: PDF.php:35
PDF extends FPDF/FPDI.
Definition: PDF.php:27
isPageStarted()
Determine if a new page started.
Definition: PDF.php:60
endPage()
Call this method when rendering a page finished.
Definition: PDF.php:51
NbLines($w, $txt)
Computes the number of lines a MultiCell of width w will take.
Definition: PDF.php:126
moveDown($units)
Move the render position down by given units.
Definition: PDF.php:76