Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
50bfb460 | 29 | * Class to print labels in Avery or custom formats |
6a488035 TO |
30 | * functionality and smarts to the base PDF_Label. |
31 | * | |
50bfb460 | 32 | * @package CRM |
6b83d5bd | 33 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
34 | */ |
35 | ||
5bc392e6 EM |
36 | /** |
37 | * Class CRM_Utils_PDF_Label | |
38 | */ | |
6a488035 TO |
39 | class CRM_Utils_PDF_Label extends TCPDF { |
40 | ||
41 | // make these properties public due to | |
42 | // CRM-5880 | |
6714d8d2 SL |
43 | /** |
44 | * Default label format values | |
45 | * @var array | |
46 | */ | |
6a488035 | 47 | public $defaults; |
6714d8d2 SL |
48 | /** |
49 | * Current label format values | |
50 | * @var array | |
51 | */ | |
6a488035 | 52 | public $format; |
6714d8d2 SL |
53 | /** |
54 | * Name of format | |
55 | * @var string | |
56 | */ | |
6a488035 | 57 | public $formatName; |
6714d8d2 SL |
58 | /** |
59 | * Left margin of labels | |
60 | * @var float | |
61 | */ | |
6a488035 | 62 | public $marginLeft; |
6714d8d2 SL |
63 | /** |
64 | * Top margin of labels | |
65 | * @var float | |
66 | */ | |
6a488035 | 67 | public $marginTop; |
6714d8d2 SL |
68 | /** |
69 | * Horizontal space between 2 labels | |
70 | * @var float | |
71 | */ | |
6a488035 | 72 | public $xSpace; |
6714d8d2 SL |
73 | /** |
74 | * Vertical space between 2 labels | |
75 | * @var float | |
76 | */ | |
6a488035 | 77 | public $ySpace; |
6714d8d2 SL |
78 | /** |
79 | * Number of labels horizontally | |
80 | * @var float | |
81 | */ | |
6a488035 | 82 | public $xNumber; |
6714d8d2 SL |
83 | /** |
84 | * Number of labels vertically | |
85 | * @var float | |
86 | */ | |
6a488035 | 87 | public $yNumber; |
6714d8d2 SL |
88 | /** |
89 | * Width of label | |
90 | * @var float | |
91 | */ | |
6a488035 | 92 | public $width; |
6714d8d2 SL |
93 | /** |
94 | * Height of label | |
95 | * @var float | |
96 | */ | |
6a488035 | 97 | public $height; |
6714d8d2 SL |
98 | /** |
99 | * Line Height of label - used in event code | |
100 | * @var float | |
101 | */ | |
a5611c8e | 102 | public $lineHeight = 0; |
6714d8d2 SL |
103 | /** |
104 | * Space between text and left edge of label | |
105 | * @var float | |
106 | */ | |
6a488035 | 107 | public $paddingLeft; |
6714d8d2 SL |
108 | /** |
109 | * Space between text and top edge of label | |
110 | * @var float | |
111 | */ | |
6a488035 | 112 | public $paddingTop; |
6714d8d2 SL |
113 | /** |
114 | * Character size (in points) | |
115 | * @var float | |
116 | */ | |
6a488035 | 117 | public $charSize; |
6714d8d2 SL |
118 | /** |
119 | * Metric used for all PDF doc measurements | |
120 | * @var string | |
121 | */ | |
6a488035 | 122 | public $metricDoc; |
6714d8d2 SL |
123 | /** |
124 | * Name of the font | |
125 | * @var string | |
126 | */ | |
6a488035 | 127 | public $fontName; |
6714d8d2 SL |
128 | /** |
129 | * 'B' bold, 'I' italic, 'BI' bold+italic | |
130 | * @var string | |
131 | */ | |
6a488035 | 132 | public $fontStyle; |
6714d8d2 SL |
133 | /** |
134 | * Paper size name | |
135 | * @var string | |
136 | */ | |
6a488035 | 137 | public $paperSize; |
6714d8d2 SL |
138 | /** |
139 | * Paper orientation | |
140 | * @var string | |
141 | */ | |
6a488035 | 142 | public $orientation; |
6714d8d2 SL |
143 | /** |
144 | * Paper dimensions array (w, h) | |
145 | * @var array | |
146 | */ | |
6a488035 | 147 | public $paper_dimensions; |
6714d8d2 SL |
148 | /** |
149 | * Counter for positioning labels | |
150 | * @var float | |
151 | */ | |
6a488035 | 152 | public $countX = 0; |
6714d8d2 SL |
153 | /** |
154 | * Counter for positioning labels | |
155 | * @var float | |
156 | */ | |
6a488035 TO |
157 | public $countY = 0; |
158 | ||
159 | /** | |
fe482240 | 160 | * Constructor. |
6a488035 | 161 | * |
77855840 TO |
162 | * @param $format |
163 | * Either the name of a Label Format in the Option Value table. | |
6a488035 | 164 | * or an array of Label Format values. |
77b97be7 | 165 | * @param string|\Unit $unit Unit of measure for the PDF document |
6a488035 | 166 | */ |
00be9182 | 167 | public function __construct($format, $unit = 'mm') { |
6a488035 TO |
168 | if (is_array($format)) { |
169 | // Custom format | |
170 | $tFormat = $format; | |
171 | } | |
172 | else { | |
173 | // Saved format | |
174 | $tFormat = CRM_Core_BAO_LabelFormat::getByName($format); | |
175 | } | |
176 | ||
177 | $this->LabelSetFormat($tFormat, $unit); | |
178 | parent::__construct($this->orientation, $this->metricDoc, $this->paper_dimensions); | |
179 | $this->generatorMethod = NULL; | |
180 | $this->SetFont($this->fontName, $this->fontStyle); | |
181 | $this->SetFontSize($this->charSize); | |
182 | $this->SetMargins(0, 0); | |
183 | $this->SetAutoPageBreak(FALSE); | |
184 | $this->setPrintHeader(FALSE); | |
185 | $this->setPrintFooter(FALSE); | |
186 | } | |
187 | ||
5bc392e6 EM |
188 | /** |
189 | * @param $objectinstance | |
190 | * @param string $methodname | |
191 | */ | |
00be9182 | 192 | public function SetGenerator($objectinstance, $methodname = 'generateLabel') { |
6a488035 TO |
193 | $this->generatorMethod = $methodname; |
194 | $this->generatorObject = $objectinstance; | |
195 | } | |
196 | ||
5bc392e6 | 197 | /** |
100fef9d | 198 | * @param string $name |
5bc392e6 EM |
199 | * @param bool $convert |
200 | * | |
201 | * @return float|int|mixed | |
202 | */ | |
00be9182 | 203 | public function getFormatValue($name, $convert = FALSE) { |
6a488035 TO |
204 | if (isset($this->format[$name])) { |
205 | $value = $this->format[$name]; | |
206 | $metric = $this->format['metric']; | |
207 | } | |
a5611c8e | 208 | else { |
6a488035 TO |
209 | $value = CRM_Utils_Array::value($name, $this->defaults); |
210 | $metric = $this->defaults['metric']; | |
211 | } | |
212 | if ($convert) { | |
213 | $value = CRM_Utils_PDF_Utils::convertMetric($value, $metric, $this->metricDoc); | |
214 | } | |
215 | return $value; | |
216 | } | |
217 | ||
5bc392e6 | 218 | /** |
fe482240 | 219 | * initialize label format settings. |
d424ffde | 220 | * |
5bc392e6 EM |
221 | * @param $format |
222 | * @param $unit | |
223 | */ | |
00be9182 | 224 | public function LabelSetFormat(&$format, $unit) { |
6a488035 TO |
225 | $this->defaults = CRM_Core_BAO_LabelFormat::getDefaultValues(); |
226 | $this->format = &$format; | |
227 | $this->formatName = $this->getFormatValue('name'); | |
228 | $this->paperSize = $this->getFormatValue('paper-size'); | |
229 | $this->orientation = $this->getFormatValue('orientation'); | |
230 | $this->fontName = $this->getFormatValue('font-name'); | |
231 | $this->charSize = $this->getFormatValue('font-size'); | |
232 | $this->fontStyle = $this->getFormatValue('font-style'); | |
233 | $this->xNumber = $this->getFormatValue('NX'); | |
234 | $this->yNumber = $this->getFormatValue('NY'); | |
235 | $this->metricDoc = $unit; | |
236 | $this->marginLeft = $this->getFormatValue('lMargin', TRUE); | |
237 | $this->marginTop = $this->getFormatValue('tMargin', TRUE); | |
238 | $this->xSpace = $this->getFormatValue('SpaceX', TRUE); | |
239 | $this->ySpace = $this->getFormatValue('SpaceY', TRUE); | |
240 | $this->width = $this->getFormatValue('width', TRUE); | |
241 | $this->height = $this->getFormatValue('height', TRUE); | |
242 | $this->paddingLeft = $this->getFormatValue('lPadding', TRUE); | |
243 | $this->paddingTop = $this->getFormatValue('tPadding', TRUE); | |
244 | $paperSize = CRM_Core_BAO_PaperSize::getByName($this->paperSize); | |
245 | $w = CRM_Utils_PDF_Utils::convertMetric($paperSize['width'], $paperSize['metric'], $this->metricDoc); | |
246 | $h = CRM_Utils_PDF_Utils::convertMetric($paperSize['height'], $paperSize['metric'], $this->metricDoc); | |
be2fb01f | 247 | $this->paper_dimensions = [$w, $h]; |
6a488035 TO |
248 | } |
249 | ||
5bc392e6 | 250 | /** |
d424ffde CW |
251 | * Generate the pdf of one label (can be modified using SetGenerator) |
252 | * | |
253 | * @param string $text | |
5bc392e6 | 254 | */ |
00be9182 | 255 | public function generateLabel($text) { |
d5d5c66e | 256 | // paddingLeft is used for both left & right padding so needs to be |
86462e6e | 257 | // subtracted twice from width to get the width that is available for text |
be2fb01f | 258 | $args = [ |
6084a669 | 259 | 'w' => $this->width - 2 * $this->paddingLeft, |
6a488035 TO |
260 | 'h' => 0, |
261 | 'txt' => $text, | |
262 | 'border' => 0, | |
263 | 'align' => 'L', | |
264 | 'fill' => 0, | |
265 | 'ln' => 0, | |
266 | 'x' => '', | |
267 | 'y' => '', | |
268 | 'reseth' => TRUE, | |
269 | 'stretch' => 0, | |
270 | 'ishtml' => FALSE, | |
271 | 'autopadding' => FALSE, | |
272 | 'maxh' => $this->height, | |
be2fb01f | 273 | ]; |
6a488035 TO |
274 | |
275 | CRM_Utils_Hook::alterMailingLabelParams($args); | |
276 | ||
277 | if ($args['ishtml'] == TRUE) { | |
278 | $this->writeHTMLCell($args['w'], $args['h'], | |
279 | $args['x'], $args['y'], | |
280 | $args['txt'], $args['border'], | |
281 | $args['ln'], $args['fill'], | |
282 | $args['reseth'], $args['align'], | |
283 | $args['autopadding'] | |
284 | ); | |
285 | } | |
286 | else { | |
287 | $this->multiCell($args['w'], $args['h'], | |
288 | $args['txt'], $args['border'], | |
289 | $args['align'], $args['fill'], | |
290 | $args['ln'], $args['x'], | |
291 | $args['y'], $args['reseth'], | |
292 | $args['stretch'], $args['ishtml'], | |
293 | $args['autopadding'], $args['maxh'] | |
294 | ); | |
295 | } | |
296 | } | |
297 | ||
5bc392e6 | 298 | /** |
fe482240 | 299 | * Print a label. |
d424ffde | 300 | * |
5bc392e6 EM |
301 | * @param $texte |
302 | */ | |
00be9182 | 303 | public function AddPdfLabel($texte) { |
0318fed3 DD |
304 | if ($this->countX == $this->xNumber) { |
305 | // Page full, we start a new one | |
306 | $this->AddPage(); | |
307 | $this->countX = 0; | |
308 | $this->countY = 0; | |
309 | } | |
310 | ||
6a488035 TO |
311 | $posX = $this->marginLeft + ($this->countX * ($this->width + $this->xSpace)); |
312 | $posY = $this->marginTop + ($this->countY * ($this->height + $this->ySpace)); | |
313 | $this->SetXY($posX + $this->paddingLeft, $posY + $this->paddingTop); | |
314 | if ($this->generatorMethod) { | |
be2fb01f | 315 | call_user_func_array([$this->generatorObject, $this->generatorMethod], [$texte]); |
6a488035 TO |
316 | } |
317 | else { | |
318 | $this->generateLabel($texte); | |
319 | } | |
320 | $this->countY++; | |
321 | ||
322 | if ($this->countY == $this->yNumber) { | |
323 | // End of column reached, we start a new one | |
324 | $this->countX++; | |
325 | $this->countY = 0; | |
326 | } | |
6a488035 TO |
327 | } |
328 | ||
f2ac86d1 | 329 | /** |
330 | * Get the available font names. | |
331 | * | |
332 | * @return array | |
333 | */ | |
00be9182 | 334 | public function getFontNames() { |
6a488035 | 335 | // Define labels for TCPDF core fonts |
be2fb01f | 336 | $fontLabel = [ |
6a488035 TO |
337 | 'courier' => ts('Courier'), |
338 | 'helvetica' => ts('Helvetica'), | |
339 | 'times' => ts('Times New Roman'), | |
340 | 'dejavusans' => ts('Deja Vu Sans (UTF-8)'), | |
be2fb01f | 341 | ]; |
d28132a1 | 342 | |
d28132a1 DL |
343 | // Check to see if we have any additional fonts to add. You can specify more fonts in |
344 | // civicrm.settings.php via: $config['CiviCRM Preferences']['additional_fonts'] | |
345 | // CRM-13307 | |
aaffa79f | 346 | $additionalFonts = Civi::settings()->get('additional_fonts'); |
d28132a1 DL |
347 | if (is_array($additionalFonts)) { |
348 | $fontLabel = array_merge($fontLabel, $additionalFonts); | |
349 | } | |
350 | ||
6a488035 TO |
351 | $tcpdfFonts = $this->fontlist; |
352 | foreach ($tcpdfFonts as $fontName) { | |
353 | if (array_key_exists($fontName, $fontLabel)) { | |
354 | $list[$fontName] = $fontLabel[$fontName]; | |
355 | } | |
356 | } | |
d28132a1 | 357 | |
6a488035 TO |
358 | return $list; |
359 | } | |
96025800 | 360 | |
6a488035 | 361 | } |