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