b16272acfd5bf2c0c50e6f3280d9d573104d004a
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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';
38 class CRM_Utils_PDF_Label
extends TCPDF
{
40 // make these properties public due to
42 // Default label format values
44 // Current label format values
48 // Left margin of labels
50 // Top margin of labels
52 // Horizontal space between 2 labels
54 // Vertical space between 2 labels
56 // Number of labels horizontally
58 // Number of labels vertically
64 // Line Height of label - used in event code
65 public $lineHeight = 0;
66 // Space between text and left edge of label
68 // Space between text and top edge of label
70 // Character size (in points)
72 // Metric used for all PDF doc measurements
76 // 'B' bold, 'I' italic, 'BI' bold+italic
82 // Paper dimensions array (w, h)
83 public $paper_dimensions;
84 // Counter for positioning labels
86 // Counter for positioning labels
92 * @param $format Either the name of a Label Format in the Option Value table
93 * or an array of Label Format values.
94 * @param $unit Unit of measure for the PDF document
98 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);
119 function SetGenerator($objectinstance, $methodname = 'generateLabel') {
120 $this->generatorMethod
= $methodname;
121 $this->generatorObject
= $objectinstance;
124 function getFormatValue($name, $convert = FALSE) {
125 if (isset($this->format
[$name])) {
126 $value = $this->format
[$name];
127 $metric = $this->format
['metric'];
130 $value = CRM_Utils_Array
::value($name, $this->defaults
);
131 $metric = $this->defaults
['metric'];
134 $value = CRM_Utils_PDF_Utils
::convertMetric($value, $metric, $this->metricDoc
);
140 * Function to initialize label format settings
142 function LabelSetFormat(&$format, $unit) {
143 $this->defaults
= CRM_Core_BAO_LabelFormat
::getDefaultValues();
144 $this->format
= &$format;
145 $this->formatName
= $this->getFormatValue('name');
146 $this->paperSize
= $this->getFormatValue('paper-size');
147 $this->orientation
= $this->getFormatValue('orientation');
148 $this->fontName
= $this->getFormatValue('font-name');
149 $this->charSize
= $this->getFormatValue('font-size');
150 $this->fontStyle
= $this->getFormatValue('font-style');
151 $this->xNumber
= $this->getFormatValue('NX');
152 $this->yNumber
= $this->getFormatValue('NY');
153 $this->metricDoc
= $unit;
154 $this->marginLeft
= $this->getFormatValue('lMargin', TRUE);
155 $this->marginTop
= $this->getFormatValue('tMargin', TRUE);
156 $this->xSpace
= $this->getFormatValue('SpaceX', TRUE);
157 $this->ySpace
= $this->getFormatValue('SpaceY', TRUE);
158 $this->width
= $this->getFormatValue('width', TRUE);
159 $this->height
= $this->getFormatValue('height', TRUE);
160 $this->paddingLeft
= $this->getFormatValue('lPadding', TRUE);
161 $this->paddingTop
= $this->getFormatValue('tPadding', TRUE);
162 $paperSize = CRM_Core_BAO_PaperSize
::getByName($this->paperSize
);
163 $w = CRM_Utils_PDF_Utils
::convertMetric($paperSize['width'], $paperSize['metric'], $this->metricDoc
);
164 $h = CRM_Utils_PDF_Utils
::convertMetric($paperSize['height'], $paperSize['metric'], $this->metricDoc
);
165 $this->paper_dimensions
= array($w, $h);
169 * function to Generate the pdf of one label (can be modified using SetGenerator)
171 function generateLabel($text) {
185 'autopadding' => FALSE,
186 'maxh' => $this->height
,
189 CRM_Utils_Hook
::alterMailingLabelParams($args);
191 if ($args['ishtml'] == TRUE) {
192 $this->writeHTMLCell($args['w'], $args['h'],
193 $args['x'], $args['y'],
194 $args['txt'], $args['border'],
195 $args['ln'], $args['fill'],
196 $args['reseth'], $args['align'],
201 $this->multiCell($args['w'], $args['h'],
202 $args['txt'], $args['border'],
203 $args['align'], $args['fill'],
204 $args['ln'], $args['x'],
205 $args['y'], $args['reseth'],
206 $args['stretch'], $args['ishtml'],
207 $args['autopadding'], $args['maxh']
213 * function to Print a label
215 function AddPdfLabel($texte) {
216 if ($this->countX
== $this->xNumber
) {
217 // Page full, we start a new one
223 $posX = $this->marginLeft +
($this->countX
* ($this->width +
$this->xSpace
));
224 $posY = $this->marginTop +
($this->countY
* ($this->height +
$this->ySpace
));
225 $this->SetXY($posX +
$this->paddingLeft
, $posY +
$this->paddingTop
);
226 if ($this->generatorMethod
) {
227 call_user_func_array(array($this->generatorObject
, $this->generatorMethod
), array($texte));
230 $this->generateLabel($texte);
234 if ($this->countY
== $this->yNumber
) {
235 // End of column reached, we start a new one
241 function getFontNames() {
242 // Define labels for TCPDF core fonts
244 'courier' => ts('Courier'),
245 'helvetica' => ts('Helvetica'),
246 'times' => ts('Times New Roman'),
247 'dejavusans' => ts('Deja Vu Sans (UTF-8)'),
251 // Check to see if we have any additional fonts to add. You can specify more fonts in
252 // civicrm.settings.php via: $config['CiviCRM Preferences']['additional_fonts']
254 $additionalFonts = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'additional_fonts');
255 if (is_array($additionalFonts)) {
256 $fontLabel = array_merge($fontLabel, $additionalFonts);
259 $tcpdfFonts = $this->fontlist
;
260 foreach ($tcpdfFonts as $fontName) {
261 if (array_key_exists($fontName, $fontLabel)) {
262 $list[$fontName] = $fontLabel[$fontName];