dolibarr  7.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  *
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 
81  private function writeBarcode(&$pdf, $code, $encoding, $is2d, $x, $y, $w, $h)
82  {
83  if ($is2d) {
84  $pdf->write2DBarcode($code, $encoding, $x, $y, $w, $h, $this->_style2d, $this->_align2d);
85  } else {
86  $pdf->write1DBarcode($code, $encoding, $x, $y, $w, $h, $this->_xres, $this->_style1d);
87  }
88  }
89 
98  function addSticker(&$pdf,$outputlangs,$param)
99  {
100  global $mysoc,$conf;
101 
102  $textleft = $param['textleft'];
103  $header = $param['textheader'];
104  $footer = $param['textfooter'];
105  $textright = $param['textright'];
106  $code = $param['code'];
107  $encoding = $param['encoding'];
108  $is2d = $param['is2d'];
109 
110 
111 
112  // We are in a new page, then we must add a page
113  if (($this->_COUNTX ==0) && ($this->_COUNTY==0) and (!$this->_First==1)) {
114  $pdf->AddPage();
115  }
116  $this->_First=0;
117  $_PosX = $this->_Margin_Left+($this->_COUNTX*($this->_Width+$this->_X_Space));
118  $_PosY = $this->_Margin_Top+($this->_COUNTY*($this->_Height+$this->_Y_Space));
119 
120  // Define logo
121  $logo=$conf->mycompany->dir_output.'/logos/'.$mysoc->logo;
122  if (! is_readable($logo))
123  {
124  $logo='';
125  if (! empty($mysoc->logo_small) && is_readable($conf->mycompany->dir_output.'/logos/thumbs/'.$mysoc->logo_small))
126  {
127  $logo=$conf->mycompany->dir_output.'/logos/thumbs/'.$mysoc->logo_small;
128  }
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 = $heighttouse;
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  else if ($code && !empty($encoding))
166  {
167  $this->writeBarcode($pdf, $code, $encoding, $is2d, $_PosX+$xleft, $_PosY+$ytop, $widthtouse, $heighttouse);
168  }
169  else
170  {
171  $pdf->SetXY($_PosX+$xleft, $_PosY+$ytop);
172  $pdf->MultiCell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft),0,'L');
173  }
174  }
175  else if ($textleft!='' && $textright!='') // left and right part
176  {
177  if (($textleft == '%LOGO%' || $textleft == '%PHOTO%' || $textleft == '%BARCODE%') && !strstr($textright, '%') ) // left part logo/barcode right part text
178  {
179  if ($textleft == '%LOGO%' && $logo) $pdf->Image($logo, $_PosX+$xleft, $_PosY+$ytop, $widthtouse/2, 0);
180  else if ($code && !empty($encoding))
181  {
182  $this->writeBarcode($pdf, $code, $encoding, $is2d, $_PosX+$xleft, $_PosY+$ytop, $widthtouse/2, $heighttouse);
183  }
184  $pdf->SetXY($_PosX+($widthtouse/2), $_PosY+$ytop);
185  $pdf->MultiCell($widthtouse/2, $this->_Line_Height, $outputlangs->convToOutputCharset($textright), 0, 'R');
186  }
187  else if (($textright == '%LOGO%' || $textright == '%PHOTO%' || $textright == '%BARCODE%') && !strstr($textleft, '%')) // right part logo/barcode left part text
188  {
189  if ($textright == '%LOGO%' && $logo) $pdf->Image($logo, $_PosX+($widthtouse/2), $_PosY+$ytop, $widthtouse/2, 0);
190  else if ($code && !empty($encoding))
191  {
192  $this->writeBarcode($pdf, $code, $encoding, $is2d, $_PosX+($widthtouse/2), $_PosY+$ytop, $widthtouse/2, $heighttouse);
193  }
194  $pdf->SetXY($_PosX+$xleft, $_PosY+$ytop);
195  $pdf->MultiCell($widthtouse/2, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft),0,'L');
196  }
197  else if ($textleft == '%LOGO%') // left part logo right part text/barcode
198  {
199  if ($logo) $pdf->Image($logo, $_PosX+$xleft, $_PosY+$ytop, 0, $logoHeight);
200  if ($code && !empty($encoding))
201  {
202  $this->writeBarcode($pdf, $code, $encoding, $is2d, $_PosX+$xleft+$logoWidth+1, $_PosY+$ytop, $widthtouse-$logoWidth-1, $heighttouse);
203  } else {
204  $pdf->SetXY($_PosX+$xleft+$logoWidth+1, $_PosY+$ytop);
205  $pdf->MultiCell($widthtouse-$logoWidth1-1, $this->_Line_Height, $outputlangs->convToOutputCharset($textright),0,'R');
206  }
207  }
208  else if ($textright == '%LOGO%') // right part logo left part text/barcode
209  {
210  if ($logo) $pdf->Image($logo, $_PosX+$xleft+$widthtouse-$logoWidth+1, $_PosY+$ytop, 0, $logoHeight);
211  if ($code && !empty($encoding))
212  {
213  $this->writeBarcode($pdf, $code, $encoding, $is2d, $_PosX+$xleft, $_PosY+$ytop, $widthtouse-$logoWidth-1, $heighttouse);
214  } else {
215  $pdf->SetXY($_PosX+$xleft, $_PosY+$ytop);
216  $pdf->MultiCell($widthtouse-$logoWidth-1, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft),0,'L');
217  }
218  }
219  else // text on halft left and text on half right
220  {
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  }
227  else // Only a right part
228  {
229  // Output right area
230  if ($textright == '%LOGO%' && $logo) $pdf->Image($logo, $_PosX+$this->_Width-$widthtouse-$xleft, $_PosY+$ytop, 0, $logoHeight);
231  else if ($code && !empty($encoding))
232  {
233  $this->writeBarcode($pdf, $code, $encoding, $is2d, $_PosX+$this->_Width-$widthtouse-$xleft, $_PosY+$ytop, $widthtouse, $heighttouse);
234  }
235  else
236  {
237  $pdf->SetXY($_PosX+$xleft, $_PosY+$ytop);
238  $pdf->MultiCell($this->_Width-$xleft, $this->_Line_Height, $outputlangs->convToOutputCharset($textright),0,'R');
239  }
240  }
241 
242  // Bottom
243  if ($footer!='')
244  {
245  $pdf->SetXY($_PosX, $_PosY+$this->_Height-$this->_Line_Height-1);
246  $pdf->Cell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($footer),0,1,'C');
247  }
248  //print "$_PosY+$this->_Height-$this->_Line_Height-1<br>\n";
249 
250  $this->_COUNTY++;
251 
252  if ($this->_COUNTY == $this->_Y_Number) {
253  // Si on est en bas de page, on remonte le 'curseur' de position
254  $this->_COUNTX++;
255  $this->_COUNTY=0;
256  }
257 
258  if ($this->_COUNTX == $this->_X_Number) {
259  // Si on est en bout de page, alors on repart sur une nouvelle page
260  $this->_COUNTX=0;
261  $this->_COUNTY=0;
262  }
263  }
264 
265 
266 
267 
278  function write_file($arrayofrecords,$outputlangs,$srctemplatepath,$outputdir='',$filename='tmp_address_sheet.pdf')
279  {
280  global $user,$conf,$langs,$mysoc,$_Avery_Labels;
281 
282  $this->code=$srctemplatepath;
283  $this->Tformat = $_Avery_Labels[$this->code];
284  if (empty($this->Tformat)) { dol_print_error('','ErrorBadTypeForCard'.$this->code); exit; }
285  $this->type = 'pdf';
286  // standard format or custom
287  if ($this->Tformat['paper-size']!='custom') {
288  $this->format = $this->Tformat['paper-size'];
289  } else {
290  //custom
291  $resolution= array($this->Tformat['custom_x'], $this->Tformat['custom_y']);
292  $this->format = $resolution;
293  }
294 
295  if (! is_object($outputlangs)) $outputlangs=$langs;
296  // For backward compatibility with FPDF, force output charset to ISO, because FPDF expect text to be encoded in ISO
297  if (! empty($conf->global->MAIN_USE_FPDF)) $outputlangs->charset_output='ISO-8859-1';
298 
299  $outputlangs->load("main");
300  $outputlangs->load("dict");
301  $outputlangs->load("companies");
302  $outputlangs->load("admin");
303 
304  $title=$outputlangs->transnoentities('Labels');
305  $keywords=$title." ".$outputlangs->convToOutputCharset($mysoc->name);
306 
307  $dir = (empty($outputdir)?$conf->adherent->dir_temp:$outputdir);
308  $file = $dir."/".$filename;
309 
310  if (! file_exists($dir))
311  {
312  if (dol_mkdir($dir) < 0)
313  {
314  $this->error=$langs->trans("ErrorCanNotCreateDir",$dir);
315  return 0;
316  }
317  }
318 
319  $pdf=pdf_getInstance($this->format,$this->Tformat['metric'], $this->Tformat['orientation']);
320 
321  if (class_exists('TCPDF'))
322  {
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 (! empty($conf->global->MAIN_DISABLE_PDF_COMPRESSION)) $pdf->SetCompression(false);
334 
335  $pdf->SetMargins(0,0);
336  $pdf->SetAutoPageBreak(false);
337 
338  $this->_Metric_Doc = $this->Tformat['metric'];
339  // Permet de commencer l'impression de l'etiquette desiree dans le cas ou la page a deja servie
340  $posX=1;
341  $posY=1;
342  if ($posX > 0) $posX--; else $posX=0;
343  if ($posY > 0) $posY--; else $posY=0;
344  $this->_COUNTX = $posX;
345  $this->_COUNTY = $posY;
346  $this->_Set_Format($pdf, $this->Tformat);
347 
348 
349  $pdf->Open();
350  $pdf->AddPage();
351 
352 
353  // Add each record
354  foreach($arrayofrecords as $val)
355  {
356  // imprime le texte specifique sur la carte
357  $this->addSticker($pdf, $outputlangs, $val);
358  }
359 
360  //$pdf->SetXY(10, 295);
361  //$pdf->Cell($this->_Width, $this->_Line_Height, 'XXX',0,1,'C');
362 
363 
364  // Output to file
365  $pdf->Output($file,'F');
366 
367  if (! empty($conf->global->MAIN_UMASK))
368  @chmod($file, octdec($conf->global->MAIN_UMASK));
369 
370  // Output to http stream
371  clearstatcache();
372 
373  $attachment=true;
374  if (! empty($conf->global->MAIN_DISABLE_FORCE_SAVEAS)) $attachment=false;
375  $type=dol_mimetype($filename);
376 
377  //if ($encoding) header('Content-Encoding: '.$encoding);
378  if ($type) header('Content-Type: '.$type);
379  if ($attachment) header('Content-Disposition: attachment; filename="'.$filename.'"');
380  else header('Content-Disposition: inline; filename="'.$filename.'"');
381 
382  // Ajout directives pour resoudre bug IE
383  header('Cache-Control: Public, must-revalidate');
384  header('Pragma: public');
385 
386  readfile($file);
387 
388  $this->result = array('fullpath'=>$file);
389 
390  return 1;
391  }
392 }
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:233
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 strem.
if($_POST["cancel"]==$langs->trans("Cancel")&&!$id) if($action== 'setdatev'&&$user->rights->tax->charges->creer) if($action== 'add'&&$_POST["cancel"]<> $langs->trans("Cancel")) if($action== 'delete') $title
Actions.
Definition: card.php:183
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.
type
Definition: viewcat.php:283