Drupal#38 - allow creating absolute URLs from CLI in D8
[civicrm-core.git] / CRM / Utils / PDF / Label.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Class to print labels in Avery or custom formats
30 * functionality and smarts to the base PDF_Label.
31 *
32 * @package CRM
33 * @copyright CiviCRM LLC (c) 2004-2019
34 */
35
36 /**
37 * Class CRM_Utils_PDF_Label
38 */
39 class CRM_Utils_PDF_Label extends TCPDF {
40
41 // make these properties public due to
42 // CRM-5880
43 /**
44 * Default label format values
45 * @var array
46 */
47 public $defaults;
48 /**
49 * Current label format values
50 * @var array
51 */
52 public $format;
53 /**
54 * Name of format
55 * @var string
56 */
57 public $formatName;
58 /**
59 * Left margin of labels
60 * @var float
61 */
62 public $marginLeft;
63 /**
64 * Top margin of labels
65 * @var float
66 */
67 public $marginTop;
68 /**
69 * Horizontal space between 2 labels
70 * @var float
71 */
72 public $xSpace;
73 /**
74 * Vertical space between 2 labels
75 * @var float
76 */
77 public $ySpace;
78 /**
79 * Number of labels horizontally
80 * @var float
81 */
82 public $xNumber;
83 /**
84 * Number of labels vertically
85 * @var float
86 */
87 public $yNumber;
88 /**
89 * Width of label
90 * @var float
91 */
92 public $width;
93 /**
94 * Height of label
95 * @var float
96 */
97 public $height;
98 /**
99 * Line Height of label - used in event code
100 * @var float
101 */
102 public $lineHeight = 0;
103 /**
104 * Space between text and left edge of label
105 * @var float
106 */
107 public $paddingLeft;
108 /**
109 * Space between text and top edge of label
110 * @var float
111 */
112 public $paddingTop;
113 /**
114 * Character size (in points)
115 * @var float
116 */
117 public $charSize;
118 /**
119 * Metric used for all PDF doc measurements
120 * @var string
121 */
122 public $metricDoc;
123 /**
124 * Name of the font
125 * @var string
126 */
127 public $fontName;
128 /**
129 * 'B' bold, 'I' italic, 'BI' bold+italic
130 * @var string
131 */
132 public $fontStyle;
133 /**
134 * Paper size name
135 * @var string
136 */
137 public $paperSize;
138 /**
139 * Paper orientation
140 * @var string
141 */
142 public $orientation;
143 /**
144 * Paper dimensions array (w, h)
145 * @var array
146 */
147 public $paper_dimensions;
148 /**
149 * Counter for positioning labels
150 * @var float
151 */
152 public $countX = 0;
153 /**
154 * Counter for positioning labels
155 * @var float
156 */
157 public $countY = 0;
158
159 /**
160 * Constructor.
161 *
162 * @param $format
163 * Either the name of a Label Format in the Option Value table.
164 * or an array of Label Format values.
165 * @param string|\Unit $unit Unit of measure for the PDF document
166 */
167 public function __construct($format, $unit = 'mm') {
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
188 /**
189 * @param $objectinstance
190 * @param string $methodname
191 */
192 public function SetGenerator($objectinstance, $methodname = 'generateLabel') {
193 $this->generatorMethod = $methodname;
194 $this->generatorObject = $objectinstance;
195 }
196
197 /**
198 * @param string $name
199 * @param bool $convert
200 *
201 * @return float|int|mixed
202 */
203 public function getFormatValue($name, $convert = FALSE) {
204 if (isset($this->format[$name])) {
205 $value = $this->format[$name];
206 $metric = $this->format['metric'];
207 }
208 else {
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
218 /**
219 * initialize label format settings.
220 *
221 * @param $format
222 * @param $unit
223 */
224 public function LabelSetFormat(&$format, $unit) {
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);
247 $this->paper_dimensions = [$w, $h];
248 }
249
250 /**
251 * Generate the pdf of one label (can be modified using SetGenerator)
252 *
253 * @param string $text
254 */
255 public function generateLabel($text) {
256 // paddingLeft is used for both left & right padding so needs to be
257 // subtracted twice from width to get the width that is available for text
258 $args = [
259 'w' => $this->width - 2 * $this->paddingLeft,
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,
273 ];
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
298 /**
299 * Print a label.
300 *
301 * @param $texte
302 */
303 public function AddPdfLabel($texte) {
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
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) {
315 call_user_func_array([$this->generatorObject, $this->generatorMethod], [$texte]);
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 }
327 }
328
329 /**
330 * Get the available font names.
331 *
332 * @return array
333 */
334 public function getFontNames() {
335 // Define labels for TCPDF core fonts
336 $fontLabel = [
337 'courier' => ts('Courier'),
338 'helvetica' => ts('Helvetica'),
339 'times' => ts('Times New Roman'),
340 'dejavusans' => ts('Deja Vu Sans (UTF-8)'),
341 ];
342
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
346 $additionalFonts = Civi::settings()->get('additional_fonts');
347 if (is_array($additionalFonts)) {
348 $fontLabel = array_merge($fontLabel, $additionalFonts);
349 }
350
351 $tcpdfFonts = $this->fontlist;
352 foreach ($tcpdfFonts as $fontName) {
353 if (array_key_exists($fontName, $fontLabel)) {
354 $list[$fontName] = $fontLabel[$fontName];
355 }
356 }
357
358 return $list;
359 }
360
361 }