3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 |
9 +--------------------------------------------------------------------+
13 * Class to print labels in Avery or custom formats
14 * functionality and smarts to the base PDF_Label.
17 * @copyright CiviCRM LLC https://civicrm.org/licensing
21 * Class CRM_Utils_PDF_Label
23 class CRM_Utils_PDF_Label
extends TCPDF
{
25 // make these properties public due to
28 * Default label format values
33 * Current label format values
43 * Left margin of labels
48 * Top margin of labels
53 * Horizontal space between 2 labels
58 * Vertical space between 2 labels
63 * Number of labels horizontally
68 * Number of labels vertically
83 * Line Height of label - used in event code
86 public $lineHeight = 0;
88 * Space between text and left edge of label
93 * Space between text and top edge of label
98 * Character size (in points)
103 * Metric used for all PDF doc measurements
113 * 'B' bold, 'I' italic, 'BI' bold+italic
128 * Paper dimensions array (w, h)
131 public $paper_dimensions;
133 * Counter for positioning labels
138 * Counter for positioning labels
147 * Either the name of a Label Format in the Option Value table.
148 * or an array of Label Format values.
149 * @param string|\Unit $unit Unit of measure for the PDF document
151 public function __construct($format, $unit = 'mm') {
152 if (is_array($format)) {
158 $tFormat = CRM_Core_BAO_LabelFormat
::getByName($format);
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);
173 * @param $objectinstance
174 * @param string $methodname
176 public function SetGenerator($objectinstance, $methodname = 'generateLabel') {
177 $this->generatorMethod
= $methodname;
178 $this->generatorObject
= $objectinstance;
182 * @param string $name
183 * @param bool $convert
185 * @return float|int|mixed
187 public function getFormatValue($name, $convert = FALSE) {
188 if (isset($this->format
[$name])) {
189 $value = $this->format
[$name];
190 $metric = $this->format
['metric'];
193 $value = CRM_Utils_Array
::value($name, $this->defaults
);
194 $metric = $this->defaults
['metric'];
197 $value = CRM_Utils_PDF_Utils
::convertMetric($value, $metric, $this->metricDoc
);
203 * initialize label format settings.
208 public function LabelSetFormat(&$format, $unit) {
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
);
231 $this->paper_dimensions
= [$w, $h];
235 * Generate the pdf of one label (can be modified using SetGenerator)
237 * @param string $text
239 public function generateLabel($text) {
240 // paddingLeft is used for both left & right padding so needs to be
241 // subtracted twice from width to get the width that is available for text
243 'w' => $this->width
- 2 * $this->paddingLeft
,
255 'autopadding' => FALSE,
256 'maxh' => $this->height
,
259 CRM_Utils_Hook
::alterMailingLabelParams($args);
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'],
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']
287 public function AddPdfLabel($texte) {
288 if ($this->countX
== $this->xNumber
) {
289 // Page full, we start a new one
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
) {
299 call_user_func_array([$this->generatorObject
, $this->generatorMethod
], [$texte]);
302 $this->generateLabel($texte);
306 if ($this->countY
== $this->yNumber
) {
307 // End of column reached, we start a new one
314 * Get the available font names.
318 public function getFontNames() {
319 // Define labels for TCPDF core fonts
321 'courier' => ts('Courier'),
322 'helvetica' => ts('Helvetica'),
323 'times' => ts('Times New Roman'),
324 'dejavusans' => ts('Deja Vu Sans (UTF-8)'),
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']
330 $additionalFonts = Civi
::settings()->get('additional_fonts');
331 if (is_array($additionalFonts)) {
332 $fontLabel = array_merge($fontLabel, $additionalFonts);
335 $tcpdfFonts = $this->fontlist
;
336 foreach ($tcpdfFonts as $fontName) {
337 if (array_key_exists($fontName, $fontLabel)) {
338 $list[$fontName] = $fontLabel[$fontName];