3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * Class to print labels in Avery or custom formats
30 * functionality and smarts to the base PDF_Label.
33 * @copyright CiviCRM LLC (c) 2004-2019
37 * Class CRM_Utils_PDF_Label
39 class CRM_Utils_PDF_Label
extends TCPDF
{
41 // make these properties public due to
44 * Default label format values
49 * Current label format values
59 * Left margin of labels
64 * Top margin of labels
69 * Horizontal space between 2 labels
74 * Vertical space between 2 labels
79 * Number of labels horizontally
84 * Number of labels vertically
99 * Line Height of label - used in event code
102 public $lineHeight = 0;
104 * Space between text and left edge of label
109 * Space between text and top edge of label
114 * Character size (in points)
119 * Metric used for all PDF doc measurements
129 * 'B' bold, 'I' italic, 'BI' bold+italic
144 * Paper dimensions array (w, h)
147 public $paper_dimensions;
149 * Counter for positioning labels
154 * Counter for positioning labels
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
167 public function __construct($format, $unit = 'mm') {
168 if (is_array($format)) {
174 $tFormat = CRM_Core_BAO_LabelFormat
::getByName($format);
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);
189 * @param $objectinstance
190 * @param string $methodname
192 public function SetGenerator($objectinstance, $methodname = 'generateLabel') {
193 $this->generatorMethod
= $methodname;
194 $this->generatorObject
= $objectinstance;
198 * @param string $name
199 * @param bool $convert
201 * @return float|int|mixed
203 public function getFormatValue($name, $convert = FALSE) {
204 if (isset($this->format
[$name])) {
205 $value = $this->format
[$name];
206 $metric = $this->format
['metric'];
209 $value = CRM_Utils_Array
::value($name, $this->defaults
);
210 $metric = $this->defaults
['metric'];
213 $value = CRM_Utils_PDF_Utils
::convertMetric($value, $metric, $this->metricDoc
);
219 * initialize label format settings.
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];
251 * Generate the pdf of one label (can be modified using SetGenerator)
253 * @param string $text
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
259 'w' => $this->width
- 2 * $this->paddingLeft
,
271 'autopadding' => FALSE,
272 'maxh' => $this->height
,
275 CRM_Utils_Hook
::alterMailingLabelParams($args);
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'],
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']
303 public function AddPdfLabel($texte) {
304 if ($this->countX
== $this->xNumber
) {
305 // Page full, we start a new one
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]);
318 $this->generateLabel($texte);
322 if ($this->countY
== $this->yNumber
) {
323 // End of column reached, we start a new one
330 * Get the available font names.
334 public function getFontNames() {
335 // Define labels for TCPDF core fonts
337 'courier' => ts('Courier'),
338 'helvetica' => ts('Helvetica'),
339 'times' => ts('Times New Roman'),
340 'dejavusans' => ts('Deja Vu Sans (UTF-8)'),
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']
346 $additionalFonts = Civi
::settings()->get('additional_fonts');
347 if (is_array($additionalFonts)) {
348 $fontLabel = array_merge($fontLabel, $additionalFonts);
351 $tcpdfFonts = $this->fontlist
;
352 foreach ($tcpdfFonts as $fontName) {
353 if (array_key_exists($fontName, $fontLabel)) {
354 $list[$fontName] = $fontLabel[$fontName];