3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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.
32 * @copyright CiviCRM LLC (c) 2004-2014
37 require_once 'tcpdf/tcpdf.php';
40 * Class CRM_Utils_PDF_Label
42 class CRM_Utils_PDF_Label
extends TCPDF
{
44 // make these properties public due to
46 // Default label format values
48 // Current label format values
52 // Left margin of labels
54 // Top margin of labels
56 // Horizontal space between 2 labels
58 // Vertical space between 2 labels
60 // Number of labels horizontally
62 // Number of labels vertically
68 // Line Height of label - used in event code
69 public $lineHeight = 0;
70 // Space between text and left edge of label
72 // Space between text and top edge of label
74 // Character size (in points)
76 // Metric used for all PDF doc measurements
80 // 'B' bold, 'I' italic, 'BI' bold+italic
86 // Paper dimensions array (w, h)
87 public $paper_dimensions;
88 // Counter for positioning labels
90 // Counter for positioning labels
97 * Either the name of a Label Format in the Option Value table.
98 * or an array of Label Format values.
99 * @param string|\Unit $unit Unit of measure for the PDF document
102 public function __construct($format, $unit = 'mm') {
103 if (is_array($format)) {
109 $tFormat = CRM_Core_BAO_LabelFormat
::getByName($format);
112 $this->LabelSetFormat($tFormat, $unit);
113 parent
::__construct($this->orientation
, $this->metricDoc
, $this->paper_dimensions
);
114 $this->generatorMethod
= NULL;
115 $this->SetFont($this->fontName
, $this->fontStyle
);
116 $this->SetFontSize($this->charSize
);
117 $this->SetMargins(0, 0);
118 $this->SetAutoPageBreak(FALSE);
119 $this->setPrintHeader(FALSE);
120 $this->setPrintFooter(FALSE);
124 * @param $objectinstance
125 * @param string $methodname
127 public function SetGenerator($objectinstance, $methodname = 'generateLabel') {
128 $this->generatorMethod
= $methodname;
129 $this->generatorObject
= $objectinstance;
133 * @param string $name
134 * @param bool $convert
136 * @return float|int|mixed
138 public function getFormatValue($name, $convert = FALSE) {
139 if (isset($this->format
[$name])) {
140 $value = $this->format
[$name];
141 $metric = $this->format
['metric'];
144 $value = CRM_Utils_Array
::value($name, $this->defaults
);
145 $metric = $this->defaults
['metric'];
148 $value = CRM_Utils_PDF_Utils
::convertMetric($value, $metric, $this->metricDoc
);
154 * initialize label format settings
160 public function LabelSetFormat(&$format, $unit) {
161 $this->defaults
= CRM_Core_BAO_LabelFormat
::getDefaultValues();
162 $this->format
= &$format;
163 $this->formatName
= $this->getFormatValue('name');
164 $this->paperSize
= $this->getFormatValue('paper-size');
165 $this->orientation
= $this->getFormatValue('orientation');
166 $this->fontName
= $this->getFormatValue('font-name');
167 $this->charSize
= $this->getFormatValue('font-size');
168 $this->fontStyle
= $this->getFormatValue('font-style');
169 $this->xNumber
= $this->getFormatValue('NX');
170 $this->yNumber
= $this->getFormatValue('NY');
171 $this->metricDoc
= $unit;
172 $this->marginLeft
= $this->getFormatValue('lMargin', TRUE);
173 $this->marginTop
= $this->getFormatValue('tMargin', TRUE);
174 $this->xSpace
= $this->getFormatValue('SpaceX', TRUE);
175 $this->ySpace
= $this->getFormatValue('SpaceY', TRUE);
176 $this->width
= $this->getFormatValue('width', TRUE);
177 $this->height
= $this->getFormatValue('height', TRUE);
178 $this->paddingLeft
= $this->getFormatValue('lPadding', TRUE);
179 $this->paddingTop
= $this->getFormatValue('tPadding', TRUE);
180 $paperSize = CRM_Core_BAO_PaperSize
::getByName($this->paperSize
);
181 $w = CRM_Utils_PDF_Utils
::convertMetric($paperSize['width'], $paperSize['metric'], $this->metricDoc
);
182 $h = CRM_Utils_PDF_Utils
::convertMetric($paperSize['height'], $paperSize['metric'], $this->metricDoc
);
183 $this->paper_dimensions
= array($w, $h);
187 * Generate the pdf of one label (can be modified using SetGenerator)
192 public function generateLabel($text) {
206 'autopadding' => FALSE,
207 'maxh' => $this->height
,
210 CRM_Utils_Hook
::alterMailingLabelParams($args);
212 if ($args['ishtml'] == TRUE) {
213 $this->writeHTMLCell($args['w'], $args['h'],
214 $args['x'], $args['y'],
215 $args['txt'], $args['border'],
216 $args['ln'], $args['fill'],
217 $args['reseth'], $args['align'],
222 $this->multiCell($args['w'], $args['h'],
223 $args['txt'], $args['border'],
224 $args['align'], $args['fill'],
225 $args['ln'], $args['x'],
226 $args['y'], $args['reseth'],
227 $args['stretch'], $args['ishtml'],
228 $args['autopadding'], $args['maxh']
239 public function AddPdfLabel($texte) {
240 if ($this->countX
== $this->xNumber
) {
241 // Page full, we start a new one
247 $posX = $this->marginLeft +
($this->countX
* ($this->width +
$this->xSpace
));
248 $posY = $this->marginTop +
($this->countY
* ($this->height +
$this->ySpace
));
249 $this->SetXY($posX +
$this->paddingLeft
, $posY +
$this->paddingTop
);
250 if ($this->generatorMethod
) {
251 call_user_func_array(array($this->generatorObject
, $this->generatorMethod
), array($texte));
254 $this->generateLabel($texte);
258 if ($this->countY
== $this->yNumber
) {
259 // End of column reached, we start a new one
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 = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, '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];