dolibarr 23.0.3
commonstickergenerator.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2003 Steve Dillon
3 * Copyright (C) 2003 Laurent Passebecq
4 * Copyright (C) 2001-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
5 * Copyright (C) 2002-2003 Jean-Louis Bergamo <jlb@j1b.org>
6 * Copyright (C) 2006-2013 Laurent Destailleur <eldy@users.sourceforge.net>
7 * Copyright (C) 2015 Francis Appels <francis.appels@yahoo.com>
8 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
9 * Copyright (C) 2025 Frédéric France <frederic.france@free.fr>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <https://www.gnu.org/licenses/>.
23 */
24
25/* Inspired by PDF_Label
26 * PDF_Label - PDF label editing
27 * @package PDF_Label
28 * @author Laurent PASSEBECQ <lpasseb@numericable.fr>
29 * @copyright 2003 Laurent PASSEBECQ
30 * available here : http://www.fpdf.org/fr/script/script29.php
31 */
32
33//-------------------------------------------------------------------
34// VERSIONS :
35// 1.0 : Initial release
36// 1.1 : + : Added unit in the constructor
37// + : Now Positions start @ (1,1).. then the first image @top-left of a page is (1,1)
38// + : Added in the description of a label :
39// font-size : default char size (can be changed by calling Set_Char_Size(xx);
40// paper-size : Size of the paper for this sheet (thanx to Al Canton)
41// metric : type of unit used in this description
42// You can define your label properties in inches by setting metric to 'in'
43// and printing in millimeter by setting unit to 'mm' in constructor.
44// Added some labels :
45// 5160, 5161, 5162, 5163,5164 : thanx to Al Canton : acanton@adams-blake.com
46// 8600 : thanx to Kunal Walia : kunal@u.washington.edu
47// + : Added 3mm to the position of labels to avoid errors
49
56require_once DOL_DOCUMENT_ROOT.'/core/lib/pdf.lib.php';
57require_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php';
58require_once DOL_DOCUMENT_ROOT.'/core/lib/format_cards.lib.php';
59require_once DOL_DOCUMENT_ROOT.'/core/class/commondocgenerator.class.php';
60
61
66{
70 public $db;
71
75 public $code;
76
77 // phpcs:disable PEAR.NamingConventions.ValidVariableName.PublicUnderscore
78 // protected
82 protected $_Avery_Name = '';
83
87 protected $_Avery_Code = '';
88
92 protected $_Margin_Left = 0;
93
97 protected $_Margin_Top = 0;
98
102 protected $_X_Space = 0;
103
107 protected $_Y_Space = 0;
108
112 protected $_X_Number = 0;
113
117 protected $_Y_Number = 0;
118
122 protected $_Width = 0;
123
127 protected $_Height = 0;
128
132 protected $_Char_Size = 10;
133
137 protected $_Line_Height = 10;
138
142 protected $_Metric = 'mm';
143
147 protected $_Metric_Doc = 'mm';
148
152 protected $_COUNTX = 1;
153
157 protected $_COUNTY = 1;
158
162 protected $_First = 1;
163
167 public $Tformat;
168
172 public $_Avery_Labels;
173 // phpcs:enable
174
180 public function __construct($db)
181 {
182 $this->db = $db;
183 }
184
185 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
196 abstract public function write_file($arrayofrecords, $outputlangs, $srctemplatepath, $outputdir = '', $filename = '');
197 // phpcs:enable
198
207 abstract public function addSticker(&$pdf, $outputlangs, $param);
208
209 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
218 public function Set_Char_Size(&$pdf, $pt)
219 {
220 // phpcs:enable
221 if ($pt > 3) {
222 $this->_Char_Size = $pt;
223 $this->_Line_Height = $this->_Get_Height_Chars($pt);
224 $pdf->SetFont('', '', $pt);
225 }
226 }
227
228 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
229 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
242 protected function _Pointille(&$pdf, $x1 = 0, $y1 = 0, $x2 = 210, $y2 = 297, $epaisseur = 1, $nbPointilles = 15)
243 {
244 // phpcs:enable
245 $pdf->SetLineWidth($epaisseur);
246 $length = abs($x1 - $x2);
247 $hauteur = abs($y1 - $y2);
248 if ($length > $hauteur) {
249 $Pointilles = ($length / $nbPointilles) / 2; // size of the dots
250 } else {
251 $Pointilles = ($hauteur / $nbPointilles) / 2;
252 }
253 for ($i = $x1; $i <= $x2; $i += $Pointilles + $Pointilles) {
254 for ($j = $i; $j <= ($i + $Pointilles); $j++) {
255 if ($j <= ($x2 - 1)) {
256 // @phan-suppress-next-line PhanPluginSuspiciousParamPosition
257 $pdf->Line($j, $y1, $j + 1, $y1); // we trace the top dot, point by point
258 // @phan-suppress-next-line PhanPluginSuspiciousParamPosition
259 $pdf->Line($j, $y2, $j + 1, $y2); // we trace the bottom dot, point by point
260 }
261 }
262 }
263 for ($i = $y1; $i <= $y2; $i += $Pointilles + $Pointilles) {
264 for ($j = $i; $j <= ($i + $Pointilles); $j++) {
265 if ($j <= ($y2 - 1)) {
266 // @phan-suppress-next-line PhanPluginSuspiciousParamPosition
267 $pdf->Line($x1, $j, $x1, $j + 1); // we trace the top dot, point by point
268 // @phan-suppress-next-line PhanPluginSuspiciousParamPosition
269 $pdf->Line($x2, $j, $x2, $j + 1); // we trace the bottom dot, point by point
270 }
271 }
272 }
273 }
274
275 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
276 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
291 protected function _Croix(&$pdf, $x1 = 0, $y1 = 0, $x2 = 210, $y2 = 297, $epaisseur = 1, $taille = 4)
292 {
293 // phpcs:enable
294 $pdf->SetDrawColor(192, 192, 192);
295
296 $pdf->SetLineWidth($epaisseur);
297 $lg = $taille / 2;
298 // top left cross
299 $pdf->Line($x1, $y1 - $lg, $x1, $y1 + $lg);
300 $pdf->Line($x1 - $lg, $y1, $x1 + $lg, $y1);
301 // bottom left cross
302 $pdf->Line($x1, $y2 - $lg, $x1, $y2 + $lg);
303 $pdf->Line($x1 - $lg, $y2, $x1 + $lg, $y2);
304 // top right cross
305 $pdf->Line($x2, $y1 - $lg, $x2, $y1 + $lg);
306 $pdf->Line($x2 - $lg, $y1, $x2 + $lg, $y1);
307 // bottom right cross
308 $pdf->Line($x2, $y2 - $lg, $x2, $y2 + $lg);
309 $pdf->Line($x2 - $lg, $y2, $x2 + $lg, $y2);
310
311 $pdf->SetDrawColor(0, 0, 0);
312 }
313
323 private function convertMetric($value, $src, $dest)
324 {
325 if ($src != $dest) {
326 $tab = array(
327 'in' => 39.37008,
328 'mm' => 1000
329 );
330 return $value * $tab[$dest] / $tab[$src];
331 }
332
333 return $value;
334 }
335
336 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
337 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
344 protected function _Get_Height_Chars($pt)
345 {
346 // phpcs:enable
347 // Array for link between height of characters and space between lines
348 $_Table_Hauteur_Chars = array(6 => 2, 7 => 2.5, 8 => 3, 9 => 3.5, 10 => 4, 11 => 6, 12 => 7, 13 => 8, 14 => 9, 15 => 10);
349 if (in_array($pt, array_keys($_Table_Hauteur_Chars))) {
350 return $_Table_Hauteur_Chars[$pt];
351 } else {
352 return 100; // There is a prob..
353 }
354 }
355
356 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
357 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
365 protected function _Set_Format(&$pdf, $format)
366 {
367 // phpcs:enable
368 $this->_Metric = $format['metric'];
369 $this->_Avery_Name = $format['name'];
370 $this->_Avery_Code = empty($format['code']) ? '' : $format['code'];
371 $this->_Margin_Left = $this->convertMetric($format['marginLeft'], $this->_Metric, $this->_Metric_Doc);
372 $this->_Margin_Top = $this->convertMetric($format['marginTop'], $this->_Metric, $this->_Metric_Doc);
373 $this->_X_Space = $this->convertMetric($format['SpaceX'], $this->_Metric, $this->_Metric_Doc);
374 $this->_Y_Space = $this->convertMetric($format['SpaceY'], $this->_Metric, $this->_Metric_Doc);
375 $this->_X_Number = $format['NX'];
376 $this->_Y_Number = $format['NY'];
377 $this->_Width = $this->convertMetric($format['width'], $this->_Metric, $this->_Metric_Doc);
378 $this->_Height = $this->convertMetric($format['height'], $this->_Metric, $this->_Metric_Doc);
379 $this->Set_Char_Size($pdf, $format['font-size']);
380 }
381}
Parent class for documents (PDF, ODT, ...) generators.
Class to generate stick sheet with format Avery or other personalised format.
_Get_Height_Chars($pt)
protected Give the height for a char size given.
_Croix(&$pdf, $x1=0, $y1=0, $x2=210, $y2=297, $epaisseur=1, $taille=4)
protected Function making a cross at the 4 corners of the labels
Set_Char_Size(&$pdf, $pt)
Method to modify the size of characters This will also modify the space between lines.
convertMetric($value, $src, $dest)
Convert units (in to mm, mm to in) $src and $dest must be 'in' or 'mm'.
_Set_Format(&$pdf, $format)
protected Set format
write_file($arrayofrecords, $outputlangs, $srctemplatepath, $outputdir='', $filename='')
Function to build PDF on disk, then output on HTTP stream.
_Pointille(&$pdf, $x1=0, $y1=0, $x2=210, $y2=297, $epaisseur=1, $nbPointilles=15)
protected Print dot line
addSticker(&$pdf, $outputlangs, $param)
Output a sticker on page at position _COUNTX, _COUNTY (_COUNTX and _COUNTY start from 0)