3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
37 * Class CRM_Utils_PDF_Label
39 class CRM_Utils_PDF_Label
extends TCPDF
{
41 // make these properties public due to
43 // Default label format values
45 // Current label format values
49 // Left margin of labels
51 // Top margin of labels
53 // Horizontal space between 2 labels
55 // Vertical space between 2 labels
57 // Number of labels horizontally
59 // Number of labels vertically
65 // Line Height of label - used in event code
66 public $lineHeight = 0;
67 // Space between text and left edge of label
69 // Space between text and top edge of label
71 // Character size (in points)
73 // Metric used for all PDF doc measurements
77 // 'B' bold, 'I' italic, 'BI' bold+italic
83 // Paper dimensions array (w, h)
84 public $paper_dimensions;
85 // Counter for positioning labels
87 // Counter for positioning labels
94 * Either the name of a Label Format in the Option Value table.
95 * or an array of Label Format values.
96 * @param string|\Unit $unit Unit of measure for the PDF document
98 public function __construct($format, $unit = 'mm') {
99 if (is_array($format)) {
105 $tFormat = CRM_Core_BAO_LabelFormat
::getByName($format);
108 $this->LabelSetFormat($tFormat, $unit);
109 parent
::__construct($this->orientation
, $this->metricDoc
, $this->paper_dimensions
);
110 $this->generatorMethod
= NULL;
111 $this->SetFont($this->fontName
, $this->fontStyle
);
112 $this->SetFontSize($this->charSize
);
113 $this->SetMargins(0, 0);
114 $this->SetAutoPageBreak(FALSE);
115 $this->setPrintHeader(FALSE);
116 $this->setPrintFooter(FALSE);
120 * @param $objectinstance
121 * @param string $methodname
123 public function SetGenerator($objectinstance, $methodname = 'generateLabel') {
124 $this->generatorMethod
= $methodname;
125 $this->generatorObject
= $objectinstance;
129 * @param string $name
130 * @param bool $convert
132 * @return float|int|mixed
134 public function getFormatValue($name, $convert = FALSE) {
135 if (isset($this->format
[$name])) {
136 $value = $this->format
[$name];
137 $metric = $this->format
['metric'];
140 $value = CRM_Utils_Array
::value($name, $this->defaults
);
141 $metric = $this->defaults
['metric'];
144 $value = CRM_Utils_PDF_Utils
::convertMetric($value, $metric, $this->metricDoc
);
150 * initialize label format settings.
155 public function LabelSetFormat(&$format, $unit) {
156 $this->defaults
= CRM_Core_BAO_LabelFormat
::getDefaultValues();
157 $this->format
= &$format;
158 $this->formatName
= $this->getFormatValue('name');
159 $this->paperSize
= $this->getFormatValue('paper-size');
160 $this->orientation
= $this->getFormatValue('orientation');
161 $this->fontName
= $this->getFormatValue('font-name');
162 $this->charSize
= $this->getFormatValue('font-size');
163 $this->fontStyle
= $this->getFormatValue('font-style');
164 $this->xNumber
= $this->getFormatValue('NX');
165 $this->yNumber
= $this->getFormatValue('NY');
166 $this->metricDoc
= $unit;
167 $this->marginLeft
= $this->getFormatValue('lMargin', TRUE);
168 $this->marginTop
= $this->getFormatValue('tMargin', TRUE);
169 $this->xSpace
= $this->getFormatValue('SpaceX', TRUE);
170 $this->ySpace
= $this->getFormatValue('SpaceY', TRUE);
171 $this->width
= $this->getFormatValue('width', TRUE);
172 $this->height
= $this->getFormatValue('height', TRUE);
173 $this->paddingLeft
= $this->getFormatValue('lPadding', TRUE);
174 $this->paddingTop
= $this->getFormatValue('tPadding', TRUE);
175 $paperSize = CRM_Core_BAO_PaperSize
::getByName($this->paperSize
);
176 $w = CRM_Utils_PDF_Utils
::convertMetric($paperSize['width'], $paperSize['metric'], $this->metricDoc
);
177 $h = CRM_Utils_PDF_Utils
::convertMetric($paperSize['height'], $paperSize['metric'], $this->metricDoc
);
178 $this->paper_dimensions
= array($w, $h);
182 * Generate the pdf of one label (can be modified using SetGenerator)
184 * @param string $text
186 public function generateLabel($text) {
187 // paddingLeft is used for both left & right padding so needs to be
188 // subtracted twice from width to get the width that is available for text
190 'w' => $this->width
- 2 * $this->paddingLeft
,
202 'autopadding' => FALSE,
203 'maxh' => $this->height
,
206 CRM_Utils_Hook
::alterMailingLabelParams($args);
208 if ($args['ishtml'] == TRUE) {
209 $this->writeHTMLCell($args['w'], $args['h'],
210 $args['x'], $args['y'],
211 $args['txt'], $args['border'],
212 $args['ln'], $args['fill'],
213 $args['reseth'], $args['align'],
218 $this->multiCell($args['w'], $args['h'],
219 $args['txt'], $args['border'],
220 $args['align'], $args['fill'],
221 $args['ln'], $args['x'],
222 $args['y'], $args['reseth'],
223 $args['stretch'], $args['ishtml'],
224 $args['autopadding'], $args['maxh']
234 public function AddPdfLabel($texte) {
235 if ($this->countX
== $this->xNumber
) {
236 // Page full, we start a new one
242 $posX = $this->marginLeft +
($this->countX
* ($this->width +
$this->xSpace
));
243 $posY = $this->marginTop +
($this->countY
* ($this->height +
$this->ySpace
));
244 $this->SetXY($posX +
$this->paddingLeft
, $posY +
$this->paddingTop
);
245 if ($this->generatorMethod
) {
246 call_user_func_array(array($this->generatorObject
, $this->generatorMethod
), array($texte));
249 $this->generateLabel($texte);
253 if ($this->countY
== $this->yNumber
) {
254 // End of column reached, we start a new one
261 * Get the available font names.
265 public function getFontNames() {
266 // Define labels for TCPDF core fonts
268 'courier' => ts('Courier'),
269 'helvetica' => ts('Helvetica'),
270 'times' => ts('Times New Roman'),
271 'dejavusans' => ts('Deja Vu Sans (UTF-8)'),
274 // Check to see if we have any additional fonts to add. You can specify more fonts in
275 // civicrm.settings.php via: $config['CiviCRM Preferences']['additional_fonts']
277 $additionalFonts = Civi
::settings()->get('additional_fonts');
278 if (is_array($additionalFonts)) {
279 $fontLabel = array_merge($fontLabel, $additionalFonts);
282 $tcpdfFonts = $this->fontlist
;
283 foreach ($tcpdfFonts as $fontName) {
284 if (array_key_exists($fontName, $fontLabel)) {
285 $list[$fontName] = $fontLabel[$fontName];