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