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
101 public function __construct($format, $unit = 'mm') {
102 if (is_array($format)) {
108 $tFormat = CRM_Core_BAO_LabelFormat
::getByName($format);
111 $this->LabelSetFormat($tFormat, $unit);
112 parent
::__construct($this->orientation
, $this->metricDoc
, $this->paper_dimensions
);
113 $this->generatorMethod
= NULL;
114 $this->SetFont($this->fontName
, $this->fontStyle
);
115 $this->SetFontSize($this->charSize
);
116 $this->SetMargins(0, 0);
117 $this->SetAutoPageBreak(FALSE);
118 $this->setPrintHeader(FALSE);
119 $this->setPrintFooter(FALSE);
123 * @param $objectinstance
124 * @param string $methodname
126 public function SetGenerator($objectinstance, $methodname = 'generateLabel') {
127 $this->generatorMethod
= $methodname;
128 $this->generatorObject
= $objectinstance;
132 * @param string $name
133 * @param bool $convert
135 * @return float|int|mixed
137 public function getFormatValue($name, $convert = FALSE) {
138 if (isset($this->format
[$name])) {
139 $value = $this->format
[$name];
140 $metric = $this->format
['metric'];
143 $value = CRM_Utils_Array
::value($name, $this->defaults
);
144 $metric = $this->defaults
['metric'];
147 $value = CRM_Utils_PDF_Utils
::convertMetric($value, $metric, $this->metricDoc
);
153 * initialize label format settings
158 public function LabelSetFormat(&$format, $unit) {
159 $this->defaults
= CRM_Core_BAO_LabelFormat
::getDefaultValues();
160 $this->format
= &$format;
161 $this->formatName
= $this->getFormatValue('name');
162 $this->paperSize
= $this->getFormatValue('paper-size');
163 $this->orientation
= $this->getFormatValue('orientation');
164 $this->fontName
= $this->getFormatValue('font-name');
165 $this->charSize
= $this->getFormatValue('font-size');
166 $this->fontStyle
= $this->getFormatValue('font-style');
167 $this->xNumber
= $this->getFormatValue('NX');
168 $this->yNumber
= $this->getFormatValue('NY');
169 $this->metricDoc
= $unit;
170 $this->marginLeft
= $this->getFormatValue('lMargin', TRUE);
171 $this->marginTop
= $this->getFormatValue('tMargin', TRUE);
172 $this->xSpace
= $this->getFormatValue('SpaceX', TRUE);
173 $this->ySpace
= $this->getFormatValue('SpaceY', TRUE);
174 $this->width
= $this->getFormatValue('width', TRUE);
175 $this->height
= $this->getFormatValue('height', TRUE);
176 $this->paddingLeft
= $this->getFormatValue('lPadding', TRUE);
177 $this->paddingTop
= $this->getFormatValue('tPadding', TRUE);
178 $paperSize = CRM_Core_BAO_PaperSize
::getByName($this->paperSize
);
179 $w = CRM_Utils_PDF_Utils
::convertMetric($paperSize['width'], $paperSize['metric'], $this->metricDoc
);
180 $h = CRM_Utils_PDF_Utils
::convertMetric($paperSize['height'], $paperSize['metric'], $this->metricDoc
);
181 $this->paper_dimensions
= array($w, $h);
185 * Generate the pdf of one label (can be modified using SetGenerator)
187 * @param string $text
189 public function generateLabel($text) {
203 'autopadding' => FALSE,
204 'maxh' => $this->height
,
207 CRM_Utils_Hook
::alterMailingLabelParams($args);
209 if ($args['ishtml'] == TRUE) {
210 $this->writeHTMLCell($args['w'], $args['h'],
211 $args['x'], $args['y'],
212 $args['txt'], $args['border'],
213 $args['ln'], $args['fill'],
214 $args['reseth'], $args['align'],
219 $this->multiCell($args['w'], $args['h'],
220 $args['txt'], $args['border'],
221 $args['align'], $args['fill'],
222 $args['ln'], $args['x'],
223 $args['y'], $args['reseth'],
224 $args['stretch'], $args['ishtml'],
225 $args['autopadding'], $args['maxh']
235 public function AddPdfLabel($texte) {
236 if ($this->countX
== $this->xNumber
) {
237 // Page full, we start a new one
243 $posX = $this->marginLeft +
($this->countX
* ($this->width +
$this->xSpace
));
244 $posY = $this->marginTop +
($this->countY
* ($this->height +
$this->ySpace
));
245 $this->SetXY($posX +
$this->paddingLeft
, $posY +
$this->paddingTop
);
246 if ($this->generatorMethod
) {
247 call_user_func_array(array($this->generatorObject
, $this->generatorMethod
), array($texte));
250 $this->generateLabel($texte);
254 if ($this->countY
== $this->yNumber
) {
255 // End of column reached, we start a new one
261 public function getFontNames() {
262 // Define labels for TCPDF core fonts
264 'courier' => ts('Courier'),
265 'helvetica' => ts('Helvetica'),
266 'times' => ts('Times New Roman'),
267 'dejavusans' => ts('Deja Vu Sans (UTF-8)'),
270 // Check to see if we have any additional fonts to add. You can specify more fonts in
271 // civicrm.settings.php via: $config['CiviCRM Preferences']['additional_fonts']
273 $additionalFonts = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'additional_fonts');
274 if (is_array($additionalFonts)) {
275 $fontLabel = array_merge($fontLabel, $additionalFonts);
278 $tcpdfFonts = $this->fontlist
;
279 foreach ($tcpdfFonts as $fontName) {
280 if (array_key_exists($fontName, $fontLabel)) {
281 $list[$fontName] = $fontLabel[$fontName];