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