dolibarr 22.0.5
pdf_tcpdflabel.class.php
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) 2024-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
31require_once DOL_DOCUMENT_ROOT.'/core/class/commonstickergenerator.class.php';
32
37{
38 // define 1d barcode style
42 private $_style1d = array(
43 'position' => '',
44 'align' => 'C',
45 'stretch' => false,
46 'fitwidth' => true,
47 'cellfitalign' => '',
48 'border' => false,
49 'hpadding' => 'auto',
50 'vpadding' => 'auto',
51 'fgcolor' => array(0, 0, 0),
52 'bgcolor' => false,
53 'text' => true,
54 'font' => 'helvetica',
55 'fontsize' => 8,
56 'stretchtext' => 4
57 );
58
59 // set style for 2d barcode
63 private $_style2d = array(
64 'border' => false,
65 'vpadding' => 'auto',
66 'hpadding' => 'auto',
67 'fgcolor' => array(0, 0, 0),
68 'bgcolor' => false,
69 'module_width' => 1, // width of a single module in points
70 'module_height' => 1 // height of a single module in points
71 );
72
76 private $_align2d = 'N';
77
81 private $_xres = 0.4;
82
96 private function writeBarcode(&$pdf, $code, $encoding, $is2d, $x, $y, $w, $h)
97 {
98 if ($is2d) {
99 $pdf->write2DBarcode($code, $encoding, $x, $y, $w, $h, $this->_style2d, $this->_align2d);
100 } else {
101 $pdf->write1DBarcode($code, $encoding, $x, $y, $w, $h, $this->_xres, $this->_style1d);
102 }
103 }
104
113 public function addSticker(&$pdf, $outputlangs, $param)
114 {
115 global $mysoc, $conf;
116
117 $textleft = $param['textleft'];
118 $header = $param['textheader'];
119 $footer = $param['textfooter'];
120 $textright = $param['textright'];
121 $code = $param['code'];
122 $encoding = $param['encoding'];
123 $is2d = $param['is2d'];
124
125
126
127 // We are in a new page, then we must add a page
128 if (($this->_COUNTX == 0) && ($this->_COUNTY == 0) and (!$this->_First == 1)) {
129 $pdf->AddPage();
130 }
131 $this->_First = 0;
132 $_PosX = $this->_Margin_Left + ($this->_COUNTX * ($this->_Width + $this->_X_Space));
133 $_PosY = $this->_Margin_Top + ($this->_COUNTY * ($this->_Height + $this->_Y_Space));
134
135 // Define logo
136 $logo = $conf->mycompany->dir_output.'/logos/'.$mysoc->logo;
137 if (!is_readable($logo)) {
138 $logo = '';
139 if (!empty($mysoc->logo_small) && is_readable($conf->mycompany->dir_output.'/logos/thumbs/'.$mysoc->logo_small)) {
140 $logo = $conf->mycompany->dir_output.'/logos/thumbs/'.$mysoc->logo_small;
141 } elseif (!empty($mysoc->logo) && is_readable($conf->mycompany->dir_output.'/logos/'.$mysoc->logo)) {
142 $logo = $conf->mycompany->dir_output.'/logos/'.$mysoc->logo;
143 }
144 }
145
146 $xleft = 2;
147 $ytop = 2;
148
149 // Top
150 if ($header != '') {
151 $pdf->SetXY($_PosX + $xleft, $_PosY + 1); // Only 1 mm and not ytop for top text
152 $pdf->Cell(2 * strlen($header), $this->_Line_Height, $outputlangs->convToOutputCharset($header), 0, 1, 'C');
153 }
154
155 $ytop += (empty($header) ? 0 : (1 + $this->_Line_Height));
156
157 // Define widthtouse and heighttouse
158 $pageMargins = $pdf->getMargins();
159 $maxwidthtouse = round($this->_Width - 2 * $xleft);
160 $maxheighttouse = round($this->_Height - 2 * $ytop);
161 $maxheighttouse -= (empty($footer) ? 0 : (1 + $this->_Line_Height));
162 $defaultratio = ($maxwidthtouse / $maxheighttouse);
163 $widthtouse = $maxwidthtouse;
164 $heighttouse = $maxheighttouse;
165 $logoHeight = $heighttouse;
166 $logoWidth = $widthtouse;
167
168 //var_dump($this->_Width.'x'.$this->_Height.' with border and scale '.$imgscale.' => max '.$maxwidthtouse.'x'.$maxheighttouse.' => We use '.$widthtouse.'x'.$heighttouse);exit;
169
170 // Center
171 if ($textright == '') { // Only a left part
172 // Output left area
173 if ($textleft == '%LOGO%' && $logo) {
174 $pdf->Image($logo, $_PosX + $xleft, $_PosY + $ytop, 0, $logoHeight);
175 } elseif ($code && !empty($encoding)) {
176 $this->writeBarcode($pdf, $code, $encoding, $is2d, $_PosX + $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
177 } else {
178 $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
179 $pdf->MultiCell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft), 0, 'L');
180 }
181 } elseif ($textleft != '' && $textright != '') { // left and right part
182 $logoHeight = $heighttouse / 2;
183 $logoWidth = $widthtouse / 2;
184 if (($textleft == '%LOGO%' || $textleft == '%PHOTO%' || $textleft == '%BARCODE%') && !strstr($textright, '%')) { // left part logo/barcode right part text
185 if ($textleft == '%LOGO%' && $logo) {
186 $pdf->Image($logo, $_PosX + $xleft, $_PosY + $ytop, $logoWidth, 0);
187 } elseif ($code && !empty($encoding)) {
188 $this->writeBarcode($pdf, $code, $encoding, $is2d, $_PosX + $xleft, $_PosY + $ytop, $widthtouse / 2, $heighttouse);
189 }
190 $pdf->SetXY($_PosX + ($widthtouse / 2), $_PosY + $ytop);
191 $pdf->MultiCell($widthtouse / 2, $this->_Line_Height, $outputlangs->convToOutputCharset($textright), 0, 'R');
192 } elseif (($textright == '%LOGO%' || $textright == '%PHOTO%' || $textright == '%BARCODE%') && !strstr($textleft, '%')) { // right part logo/barcode left part text
193 if ($textright == '%LOGO%' && $logo) {
194 $pdf->Image($logo, $_PosX + ($widthtouse / 2), $_PosY + $ytop, $logoWidth, 0);
195 } elseif ($code && !empty($encoding)) {
196 $this->writeBarcode($pdf, $code, $encoding, $is2d, $_PosX + ($widthtouse / 2), $_PosY + $ytop, $widthtouse / 2, $heighttouse);
197 }
198 $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
199 $pdf->MultiCell($widthtouse / 2, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft), 0, 'L');
200 } elseif ($textleft == '%LOGO%') { // left part logo right part text/barcode
201 if ($logo) {
202 $pdf->Image($logo, $_PosX + $xleft, $_PosY + $ytop, 0, $logoHeight);
203 }
204 if ($code && !empty($encoding)) {
205 $this->writeBarcode($pdf, $code, $encoding, $is2d, $_PosX + $xleft + $logoWidth + 1, $_PosY + $ytop, $widthtouse - $logoWidth - 1, $heighttouse);
206 } else {
207 $pdf->SetXY($_PosX + $xleft + $logoWidth + 1, $_PosY + $ytop);
208 $pdf->MultiCell($widthtouse - $logoWidth - 1, $this->_Line_Height, $outputlangs->convToOutputCharset($textright), 0, 'R');
209 }
210 } elseif ($textright == '%LOGO%') { // right part logo left part text/barcode
211 if ($logo) {
212 $pdf->Image($logo, $_PosX + $xleft + $widthtouse - $logoWidth + 1, $_PosY + $ytop, 0, $logoHeight);
213 }
214 if ($code && !empty($encoding)) {
215 $this->writeBarcode($pdf, $code, $encoding, $is2d, $_PosX + $xleft, $_PosY + $ytop, $widthtouse - $logoWidth - 1, $heighttouse);
216 } else {
217 $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
218 $pdf->MultiCell($widthtouse - $logoWidth - 1, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft), 0, 'L');
219 }
220 } else { // text on halft left and text on half right
221 $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
222 $pdf->MultiCell(round($this->_Width / 2), $this->_Line_Height, $outputlangs->convToOutputCharset($textleft), 0, 'L');
223 $pdf->SetXY($_PosX + round($this->_Width / 2), $_PosY + $ytop);
224 $pdf->MultiCell(round($this->_Width / 2) - 2, $this->_Line_Height, $outputlangs->convToOutputCharset($textright), 0, 'R');
225 }
226 } else { // Only a right part
227 // Output right area
228 if ($textright == '%LOGO%' && $logo) {
229 $pdf->Image($logo, $_PosX + $this->_Width - $widthtouse - $xleft, $_PosY + $ytop, 0, $logoHeight);
230 } elseif ($code && !empty($encoding)) {
231 $this->writeBarcode($pdf, $code, $encoding, $is2d, $_PosX + $this->_Width - $widthtouse - $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
232 } else {
233 $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
234 $pdf->MultiCell($this->_Width - $xleft, $this->_Line_Height, $outputlangs->convToOutputCharset($textright), 0, 'R');
235 }
236 }
237
238 // Bottom
239 if ($footer != '') {
240 $pdf->SetXY($_PosX, $_PosY + $this->_Height - $this->_Line_Height - 1);
241 $pdf->Cell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($footer), 0, 1, 'C');
242 }
243 //print "$_PosY+$this->_Height-$this->_Line_Height-1<br>\n";
244
245 $this->_COUNTY++;
246
247 if ($this->_COUNTY == $this->_Y_Number) {
248 // Si on est en bas de page, on remonte le 'curseur' de position
249 $this->_COUNTX++;
250 $this->_COUNTY = 0;
251 }
252
253 if ($this->_COUNTX == $this->_X_Number) {
254 // Si on est en bout de page, alors on repart sur une nouvelle page
255 $this->_COUNTX = 0;
256 $this->_COUNTY = 0;
257 }
258 }
259
260
261
262
263 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
274 public function write_file($arrayofrecords, $outputlangs, $srctemplatepath, $outputdir = '', $filename = 'tmp_address_sheet.pdf')
275 {
276 // phpcs:enable
277 global $user, $conf, $langs, $mysoc, $_Avery_Labels;
278
279 $this->code = $srctemplatepath;
280 $this->Tformat = $_Avery_Labels[$this->code];
281 if (empty($this->Tformat)) {
282 dol_print_error(null, 'ErrorBadTypeForCard'.$this->code);
283 exit;
284 }
285 $this->type = 'pdf';
286 // standard format or custom
287 $paper_size = $this->Tformat['paper-size'];
288 if (!is_string($paper_size) || $paper_size != 'custom') {
289 $this->format = $paper_size;
290 } else {
291 //custom
292 $resolution = array($this->Tformat['custom_x'], $this->Tformat['custom_y']);
293 $this->format = $resolution;
294 }
295
296 if (!is_object($outputlangs)) {
297 $outputlangs = $langs;
298 }
299 // For backward compatibility with FPDF, force output charset to ISO, because FPDF expect text to be encoded in ISO
300 if (getDolGlobalString('MAIN_USE_FPDF')) {
301 $outputlangs->charset_output = 'ISO-8859-1';
302 }
303
304 // Load traductions files required by page
305 $outputlangs->loadLangs(array("main", "dict", "companies", "admin"));
306
307 $title = $outputlangs->transnoentities('Labels');
308 $keywords = $title." ".$outputlangs->convToOutputCharset($mysoc->name);
309
310 $dir = (empty($outputdir) ? $conf->adherent->dir_temp : $outputdir);
311 $file = $dir."/".$filename;
312
313 if (!file_exists($dir)) {
314 if (dol_mkdir($dir) < 0) {
315 $this->error = $langs->trans("ErrorCanNotCreateDir", $dir);
316 return 0;
317 }
318 }
319
320 $pdf = pdf_getInstance($this->format, $this->Tformat['metric'], $this->Tformat['orientation']);
321
322 if (class_exists('TCPDF')) {
323 $pdf->setPrintHeader(false);
324 $pdf->setPrintFooter(false);
325 }
326 $pdf->SetFont(pdf_getPDFFont($outputlangs));
327
328 $pdf->SetTitle($title);
329 $pdf->SetSubject($title);
330 $pdf->SetCreator("Dolibarr ".DOL_VERSION);
331 $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
332 $pdf->SetKeyWords($keywords);
333 if (getDolGlobalString('MAIN_DISABLE_PDF_COMPRESSION')) {
334 $pdf->SetCompression(false);
335 }
336
337 $pdf->SetMargins(0, 0);
338 $pdf->setAutoPageBreak(false);
339
340 $this->_Metric_Doc = $this->Tformat['metric'];
341 // Permet de commencer l'impression de l'etiquette desiree dans le cas ou la page a deja service
342 $posX = 1;
343 $posY = 1;
344 if ($posX > 0) {
345 $posX--;
346 } else {
347 $posX = 0;
348 }
349 if ($posY > 0) {
350 $posY--;
351 } else {
352 $posY = 0;
353 }
354 $this->_COUNTX = $posX;
355 $this->_COUNTY = $posY;
356 $this->_Set_Format($pdf, $this->Tformat);
357
358
359 $pdf->Open();
360 $pdf->AddPage();
361
362
363 // Add each record
364 foreach ($arrayofrecords as $val) {
365 // imprime le texte specifique sur la carte
366 $this->addSticker($pdf, $outputlangs, $val);
367 }
368
369 //$pdf->SetXY(10, 295);
370 //$pdf->Cell($this->_Width, $this->_Line_Height, 'XXX',0,1,'C');
371
372
373 // Output to file
374 $pdf->Output($file, 'F');
375
376 dolChmod($file);
377
378 $this->result = array('fullpath' => $file);
379
380 return 1;
381 }
382}
Class to generate stick sheet with format Avery or other personalised.
_Set_Format(&$pdf, $format)
protected Set format
Class to generate stick sheet with format Avery or other personalised.
write_file($arrayofrecords, $outputlangs, $srctemplatepath, $outputdir='', $filename='tmp_address_sheet.pdf')
Function to build PDF on disk, then output on HTTP stream.
writeBarcode(&$pdf, $code, $encoding, $is2d, $x, $y, $w, $h)
write barcode to pdf
addSticker(&$pdf, $outputlangs, $param)
Output a sticker on page at position _COUNTX, _COUNTY (_COUNTX and _COUNTY start from 0)
dolChmod($filepath, $newmask='')
Change mod of a file.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
pdf_getPDFFont($outputlangs)
Return font name to use for PDF generation.
Definition pdf.lib.php:266
pdf_getInstance($format='', $metric='mm', $pagetype='P')
Return a PDF instance object.
Definition pdf.lib.php:129
if(preg_match('/(crypted|dolcrypt):/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition repair.php:158