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