dolibarr  9.0.0
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 <http://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  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  }
130  elseif (! empty($mysoc->logo) && is_readable($conf->mycompany->dir_output.'/logos/'.$mysoc->logo))
131  {
132  $logo=$conf->mycompany->dir_output.'/logos/'.$mysoc->logo;
133  }
134  }
135 
136  $xleft = 2;
137  $ytop = 2;
138 
139  // Top
140  if ($header!='')
141  {
142  $pdf->SetXY($_PosX+$xleft, $_PosY+1); // Only 1 mm and not ytop for top text
143  $pdf->Cell($this->_Width-2*$xleft, $this->_Line_Height, $outputlangs->convToOutputCharset($header),0,1,'C');
144  }
145 
146  $ytop += (empty($header)?0:(1+$this->_Line_Height));
147 
148  // Define widthtouse and heighttouse
149  $pageMargins = $pdf->getMargins();
150  $maxwidthtouse = round($this->_Width - 2*$xleft);
151  $maxheighttouse = round($this->_Height - 2*$ytop);
152  $maxheighttouse -= (empty($footer)?0:(1+$this->_Line_Height));
153  $defaultratio = ($maxwidthtouse/$maxheighttouse);
154  $widthtouse = $maxwidthtouse;
155  $heighttouse = $maxheighttouse;
156  $logoHeight = $heighttouse;
157  $logoWidth = $heighttouse;
158 
159  //var_dump($this->_Width.'x'.$this->_Height.' with border and scale '.$imgscale.' => max '.$maxwidthtouse.'x'.$maxheighttouse.' => We use '.$widthtouse.'x'.$heighttouse);exit;
160 
161  // Center
162  if ($textright=='') // Only a left part
163  {
164  // Output left area
165  if ($textleft == '%LOGO%' && $logo) $pdf->Image($logo, $_PosX+$xleft, $_PosY+$ytop, 0, $logoHeight);
166  else if ($code && !empty($encoding))
167  {
168  $this->writeBarcode($pdf, $code, $encoding, $is2d, $_PosX+$xleft, $_PosY+$ytop, $widthtouse, $heighttouse);
169  }
170  else
171  {
172  $pdf->SetXY($_PosX+$xleft, $_PosY+$ytop);
173  $pdf->MultiCell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft),0,'L');
174  }
175  }
176  else if ($textleft!='' && $textright!='') // left and right part
177  {
178  if (($textleft == '%LOGO%' || $textleft == '%PHOTO%' || $textleft == '%BARCODE%') && !strstr($textright, '%') ) // left part logo/barcode right part text
179  {
180  if ($textleft == '%LOGO%' && $logo) $pdf->Image($logo, $_PosX+$xleft, $_PosY+$ytop, $widthtouse/2, 0);
181  else if ($code && !empty($encoding))
182  {
183  $this->writeBarcode($pdf, $code, $encoding, $is2d, $_PosX+$xleft, $_PosY+$ytop, $widthtouse/2, $heighttouse);
184  }
185  $pdf->SetXY($_PosX+($widthtouse/2), $_PosY+$ytop);
186  $pdf->MultiCell($widthtouse/2, $this->_Line_Height, $outputlangs->convToOutputCharset($textright), 0, 'R');
187  }
188  else if (($textright == '%LOGO%' || $textright == '%PHOTO%' || $textright == '%BARCODE%') && !strstr($textleft, '%')) // right part logo/barcode left part text
189  {
190  if ($textright == '%LOGO%' && $logo) $pdf->Image($logo, $_PosX+($widthtouse/2), $_PosY+$ytop, $widthtouse/2, 0);
191  else if ($code && !empty($encoding))
192  {
193  $this->writeBarcode($pdf, $code, $encoding, $is2d, $_PosX+($widthtouse/2), $_PosY+$ytop, $widthtouse/2, $heighttouse);
194  }
195  $pdf->SetXY($_PosX+$xleft, $_PosY+$ytop);
196  $pdf->MultiCell($widthtouse/2, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft),0,'L');
197  }
198  else if ($textleft == '%LOGO%') // left part logo right part text/barcode
199  {
200  if ($logo) $pdf->Image($logo, $_PosX+$xleft, $_PosY+$ytop, 0, $logoHeight);
201  if ($code && !empty($encoding))
202  {
203  $this->writeBarcode($pdf, $code, $encoding, $is2d, $_PosX+$xleft+$logoWidth+1, $_PosY+$ytop, $widthtouse-$logoWidth-1, $heighttouse);
204  } else {
205  $pdf->SetXY($_PosX+$xleft+$logoWidth+1, $_PosY+$ytop);
206  $pdf->MultiCell($widthtouse-$logoWidth1-1, $this->_Line_Height, $outputlangs->convToOutputCharset($textright),0,'R');
207  }
208  }
209  else if ($textright == '%LOGO%') // right part logo left part text/barcode
210  {
211  if ($logo) $pdf->Image($logo, $_PosX+$xleft+$widthtouse-$logoWidth+1, $_PosY+$ytop, 0, $logoHeight);
212  if ($code && !empty($encoding))
213  {
214  $this->writeBarcode($pdf, $code, $encoding, $is2d, $_PosX+$xleft, $_PosY+$ytop, $widthtouse-$logoWidth-1, $heighttouse);
215  } else {
216  $pdf->SetXY($_PosX+$xleft, $_PosY+$ytop);
217  $pdf->MultiCell($widthtouse-$logoWidth-1, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft),0,'L');
218  }
219  }
220  else // text on halft left and text on half right
221  {
222  $pdf->SetXY($_PosX+$xleft, $_PosY+$ytop);
223  $pdf->MultiCell(round($this->_Width/2), $this->_Line_Height, $outputlangs->convToOutputCharset($textleft),0,'L');
224  $pdf->SetXY($_PosX+round($this->_Width/2), $_PosY+$ytop);
225  $pdf->MultiCell(round($this->_Width/2)-2, $this->_Line_Height, $outputlangs->convToOutputCharset($textright),0,'R');
226  }
227  }
228  else // Only a right part
229  {
230  // Output right area
231  if ($textright == '%LOGO%' && $logo) $pdf->Image($logo, $_PosX+$this->_Width-$widthtouse-$xleft, $_PosY+$ytop, 0, $logoHeight);
232  else if ($code && !empty($encoding))
233  {
234  $this->writeBarcode($pdf, $code, $encoding, $is2d, $_PosX+$this->_Width-$widthtouse-$xleft, $_PosY+$ytop, $widthtouse, $heighttouse);
235  }
236  else
237  {
238  $pdf->SetXY($_PosX+$xleft, $_PosY+$ytop);
239  $pdf->MultiCell($this->_Width-$xleft, $this->_Line_Height, $outputlangs->convToOutputCharset($textright),0,'R');
240  }
241  }
242 
243  // Bottom
244  if ($footer!='')
245  {
246  $pdf->SetXY($_PosX, $_PosY+$this->_Height-$this->_Line_Height-1);
247  $pdf->Cell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($footer),0,1,'C');
248  }
249  //print "$_PosY+$this->_Height-$this->_Line_Height-1<br>\n";
250 
251  $this->_COUNTY++;
252 
253  if ($this->_COUNTY == $this->_Y_Number) {
254  // Si on est en bas de page, on remonte le 'curseur' de position
255  $this->_COUNTX++;
256  $this->_COUNTY=0;
257  }
258 
259  if ($this->_COUNTX == $this->_X_Number) {
260  // Si on est en bout de page, alors on repart sur une nouvelle page
261  $this->_COUNTX=0;
262  $this->_COUNTY=0;
263  }
264  }
265 
266 
267 
268 
269  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
280  function write_file($arrayofrecords,$outputlangs,$srctemplatepath,$outputdir='',$filename='tmp_address_sheet.pdf')
281  {
282  // phpcs:enable
283  global $user,$conf,$langs,$mysoc,$_Avery_Labels;
284 
285  $this->code=$srctemplatepath;
286  $this->Tformat = $_Avery_Labels[$this->code];
287  if (empty($this->Tformat)) { dol_print_error('','ErrorBadTypeForCard'.$this->code); exit; }
288  $this->type = 'pdf';
289  // standard format or custom
290  if ($this->Tformat['paper-size']!='custom') {
291  $this->format = $this->Tformat['paper-size'];
292  } else {
293  //custom
294  $resolution= array($this->Tformat['custom_x'], $this->Tformat['custom_y']);
295  $this->format = $resolution;
296  }
297 
298  if (! is_object($outputlangs)) $outputlangs=$langs;
299  // For backward compatibility with FPDF, force output charset to ISO, because FPDF expect text to be encoded in ISO
300  if (! empty($conf->global->MAIN_USE_FPDF)) $outputlangs->charset_output='ISO-8859-1';
301 
302  // Load traductions files requiredby by page
303  $outputlangs->loadLangs(array("main", "dict", "companies", "admin"));
304 
305  $title=$outputlangs->transnoentities('Labels');
306  $keywords=$title." ".$outputlangs->convToOutputCharset($mysoc->name);
307 
308  $dir = (empty($outputdir)?$conf->adherent->dir_temp:$outputdir);
309  $file = $dir."/".$filename;
310 
311  if (! file_exists($dir))
312  {
313  if (dol_mkdir($dir) < 0)
314  {
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  {
324  $pdf->setPrintHeader(false);
325  $pdf->setPrintFooter(false);
326  }
327  $pdf->SetFont(pdf_getPDFFont($outputlangs));
328 
329  $pdf->SetTitle($title);
330  $pdf->SetSubject($title);
331  $pdf->SetCreator("Dolibarr ".DOL_VERSION);
332  $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
333  $pdf->SetKeyWords($keywords);
334  if (! empty($conf->global->MAIN_DISABLE_PDF_COMPRESSION)) $pdf->SetCompression(false);
335 
336  $pdf->SetMargins(0,0);
337  $pdf->SetAutoPageBreak(false);
338 
339  $this->_Metric_Doc = $this->Tformat['metric'];
340  // Permet de commencer l'impression de l'etiquette desiree dans le cas ou la page a deja servie
341  $posX=1;
342  $posY=1;
343  if ($posX > 0) $posX--; else $posX=0;
344  if ($posY > 0) $posY--; else $posY=0;
345  $this->_COUNTX = $posX;
346  $this->_COUNTY = $posY;
347  $this->_Set_Format($pdf, $this->Tformat);
348 
349 
350  $pdf->Open();
351  $pdf->AddPage();
352 
353 
354  // Add each record
355  foreach($arrayofrecords as $val)
356  {
357  // imprime le texte specifique sur la carte
358  $this->addSticker($pdf, $outputlangs, $val);
359  }
360 
361  //$pdf->SetXY(10, 295);
362  //$pdf->Cell($this->_Width, $this->_Line_Height, 'XXX',0,1,'C');
363 
364 
365  // Output to file
366  $pdf->Output($file,'F');
367 
368  if (! empty($conf->global->MAIN_UMASK))
369  @chmod($file, octdec($conf->global->MAIN_UMASK));
370 
371  // Output to http stream
372  clearstatcache();
373 
374  $attachment=true;
375  if (! empty($conf->global->MAIN_DISABLE_FORCE_SAVEAS)) $attachment=false;
376  $type=dol_mimetype($filename);
377 
378  //if ($encoding) header('Content-Encoding: '.$encoding);
379  if ($type) header('Content-Type: '.$type);
380  if ($attachment) header('Content-Disposition: attachment; filename="'.$filename.'"');
381  else header('Content-Disposition: inline; filename="'.$filename.'"');
382 
383  // Ajout directives pour resoudre bug IE
384  header('Cache-Control: Public, must-revalidate');
385  header('Pragma: public');
386 
387  readfile($file);
388 
389  $this->result = array('fullpath'=>$file);
390 
391  return 1;
392  }
393 }
pdf_getInstance($format='', $metric='mm', $pagetype='P')
Return a PDF instance object.
Definition: pdf.lib.php:82
addSticker(&$pdf, $outputlangs, $param)
Output a sticker on page at position _COUNTX, _COUNTY (_COUNTX and _COUNTY start from 0) ...
dol_mimetype($file, $default='application/octet-stream', $mode=0)
Return mime type of a file.
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
pdf_getPDFFont($outputlangs)
Return font name to use for PDF generation.
Definition: pdf.lib.php:201
Class to generate stick sheet with format Avery or other personalised.
type
Definition: viewcat.php:284
write_file($arrayofrecords, $outputlangs, $srctemplatepath, $outputdir='', $filename='tmp_address_sheet.pdf')
Function to build PDF on disk, then output on HTTP strem.
writeBarcode(&$pdf, $code, $encoding, $is2d, $x, $y, $w, $h)
write barcode to pdf
dol_mkdir($dir, $dataroot='', $newmask=null)
Creation of a directory (this can create recursive subdir)
_Set_Format(&$pdf, $format)
protected Set format
Class to generate stick sheet with format Avery or other personalised.