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