dolibarr  9.0.0
pdf_standardlabel.class.php
Go to the documentation of this file.
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 {
44  function addSticker(&$pdf,$outputlangs,$param)
45  {
46  // use this method in future refactoring
47  }
48 
49  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
64  function Add_PDF_label(&$pdf,$textleft,$header,$footer,$outputlangs,$textright='',$photo='')
65  {
66  // phpcs:enable
67  global $mysoc, $conf, $langs;
68  global $forceimgscalewidth, $forceimgscaleheight;
69 
70  $imgscalewidth=(empty($forceimgscalewidth)?0.3:$forceimgscalewidth); // Scale of image for width (1=Full width of sticker)
71  $imgscaleheight=(empty($forceimgscaleheight)?0.5:$forceimgscaleheight); // Scale of image for height (1=Full height of sticker)
72 
73  // We are in a new page, then we must add a page
74  if (($this->_COUNTX ==0) && ($this->_COUNTY==0) and (!$this->_First==1)) {
75  $pdf->AddPage();
76  }
77  $this->_First=0;
78  $_PosX = $this->_Margin_Left+($this->_COUNTX*($this->_Width+$this->_X_Space));
79  $_PosY = $this->_Margin_Top+($this->_COUNTY*($this->_Height+$this->_Y_Space));
80 
81  // Define logo
82  $logo=$conf->mycompany->dir_output.'/logos/'.$mysoc->logo;
83  if (! is_readable($logo))
84  {
85  $logo='';
86  if (! empty($mysoc->logo_small) && is_readable($conf->mycompany->dir_output.'/logos/thumbs/'.$mysoc->logo_small))
87  {
88  $logo=$conf->mycompany->dir_output.'/logos/thumbs/'.$mysoc->logo_small;
89  }
90  elseif (! empty($mysoc->logo) && is_readable($conf->mycompany->dir_output.'/logos/'.$mysoc->logo))
91  {
92  $logo=$conf->mycompany->dir_output.'/logos/'.$mysoc->logo;
93  }
94  }
95 
96  // Define photo
97  if (! empty($photo))
98  {
99  if (! is_readable($photo)) $photo='';
100  }
101 
102  // Define background image
103  $backgroundimage='';
104 
105  // Print lines
106  if ($this->code == "CARD")
107  {
108  $this->Tformat=$this->_Avery_Labels["CARD"];
109  //$this->_Pointille($pdf,$_PosX,$_PosY,$_PosX+$this->_Width,$_PosY+$this->_Height,0.3,25);
110  $this->_Croix($pdf,$_PosX,$_PosY,$_PosX+$this->_Width,$_PosY+$this->_Height,0.1,10);
111  }
112 
113  // Background
114  if ($backgroundimage)
115  {
116  $pdf->image($backgroundimage,$_PosX,$_PosY,$this->_Width,$this->_Height);
117  }
118 
119  $xleft=2; $ytop=2;
120 
121  // Top
122  if ($header!='')
123  {
124  if ($this->code == "CARD")
125  {
126  $pdf->SetDrawColor(128,128,128);
127  $pdf->Line($_PosX, $_PosY+$this->_Line_Height+1, $_PosX+$this->_Width, $_PosY+$this->_Line_Height+1); // Only 1 mm and not ytop for top text
128  $pdf->SetDrawColor(0,0,0);
129  }
130  $pdf->SetXY($_PosX+$xleft, $_PosY+1); // Only 1 mm and not ytop for top text
131  $pdf->Cell($this->_Width-2*$xleft, $this->_Line_Height, $outputlangs->convToOutputCharset($header),0,1,'C');
132  }
133 
134 
135  $ytop+=(empty($header)?0:(1+$this->_Line_Height));
136 
137  // Define widthtouse and heighttouse
138  $maxwidthtouse=round(($this->_Width - 2*$xleft)*$imgscalewidth); $maxheighttouse=round(($this->_Height - 2*$ytop)*$imgscaleheight);
139  $defaultratio=($maxwidthtouse/$maxheighttouse);
140  $widthtouse=$maxwidthtouse; $heighttouse=0; // old value for image
141  $tmp=dol_getImageSize($photo, false);
142  if ($tmp['height'])
143  {
144  $imgratio=$tmp['width']/$tmp['height'];
145  if ($imgratio >= $defaultratio) { $widthtouse = $maxwidthtouse; $heighttouse = round($widthtouse / $imgratio); }
146  else { $heightouse = $maxheighttouse; $widthtouse = round($heightouse * $imgratio); }
147  }
148  //var_dump($this->_Width.'x'.$this->_Height.' with border and scale '.$imgscale.' => max '.$maxwidthtouse.'x'.$maxheighttouse.' => We use '.$widthtouse.'x'.$heighttouse);exit;
149 
150  // Center
151  if ($textright=='') // Only a left part
152  {
153  // Output left area
154  if ($textleft == '%LOGO%' && $logo) $pdf->Image($logo,$_PosX+$xleft,$_PosY+$ytop,$widthtouse,$heighttouse);
155  else if ($textleft == '%PHOTO%' && $photo) $pdf->Image($photo,$_PosX+$xleft,$_PosY+$ytop,$widthtouse,$heighttouse);
156  else
157  {
158  $pdf->SetXY($_PosX+$xleft, $_PosY+$ytop);
159  $pdf->MultiCell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft),0,'L');
160  }
161  }
162  else if ($textleft!='' && $textright!='') //
163  {
164  if ($textleft == '%LOGO%' || $textleft == '%PHOTO%')
165  {
166  if ($textleft == '%LOGO%' && $logo) $pdf->Image($logo,$_PosX+$xleft,$_PosY+$ytop,$widthtouse,$heighttouse);
167  else if ($textleft == '%PHOTO%' && $photo) $pdf->Image($photo,$_PosX+$xleft,$_PosY+$ytop,$widthtouse,$heighttouse);
168  $pdf->SetXY($_PosX+$xleft+$widthtouse+1, $_PosY+$ytop);
169  $pdf->MultiCell($this->_Width-$xleft-$xleft-$widthtouse-1, $this->_Line_Height, $outputlangs->convToOutputCharset($textright),0,'R');
170  }
171  else if ($textright == '%LOGO%' || $textright == '%PHOTO%')
172  {
173  if ($textright == '%LOGO%' && $logo) $pdf->Image($logo,$_PosX+$this->_Width-$widthtouse-$xleft,$_PosY+$ytop,$widthtouse,$heighttouse);
174  else if ($textright == '%PHOTO%' && $photo) $pdf->Image($photo,$_PosX+$this->_Width-$widthtouse-$xleft,$_PosY+$ytop,$widthtouse,$heighttouse);
175  $pdf->SetXY($_PosX+$xleft, $_PosY+$ytop);
176  $pdf->MultiCell($this->_Width-$widthtouse-$xleft-$xleft-1, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft),0,'L');
177  }
178  else // text on halft left and text on half right
179  {
180  $pdf->SetXY($_PosX+$xleft, $_PosY+$ytop);
181  $pdf->MultiCell(round($this->_Width/2), $this->_Line_Height, $outputlangs->convToOutputCharset($textleft),0,'L');
182  $pdf->SetXY($_PosX+round($this->_Width/2), $_PosY+$ytop);
183  $pdf->MultiCell(round($this->_Width/2)-2, $this->_Line_Height, $outputlangs->convToOutputCharset($textright),0,'R');
184  }
185  }
186  else // Only a right part
187  {
188  // Output right area
189  if ($textright == '%LOGO%' && $logo) $pdf->Image($logo,$_PosX+$this->_Width-$widthtouse-$xleft,$_PosY+$ytop,$widthtouse,$heighttouse);
190  else if ($textright == '%PHOTO%' && $photo) $pdf->Image($photo,$_PosX+$this->_Width-$widthtouse-$xleft,$_PosY+$ytop,$widthtouse,$heighttouse);
191  else
192  {
193  $pdf->SetXY($_PosX+$xleft, $_PosY+$ytop);
194  $pdf->MultiCell($this->_Width-$xleft, $this->_Line_Height, $outputlangs->convToOutputCharset($textright),0,'R');
195  }
196  }
197 
198  // Bottom
199  if ($footer!='')
200  {
201  if ($this->code == "CARD")
202  {
203  $pdf->SetDrawColor(128,128,128);
204  $pdf->Line($_PosX, $_PosY+$this->_Height-$this->_Line_Height-2, $_PosX+$this->_Width, $_PosY+$this->_Height-$this->_Line_Height-2);
205  $pdf->SetDrawColor(0,0,0);
206  }
207  $pdf->SetXY($_PosX, $_PosY+$this->_Height-$this->_Line_Height-1);
208  $pdf->Cell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($footer),0,1,'C');
209  }
210  //print "$_PosY+$this->_Height-$this->_Line_Height-1<br>\n";
211 
212  $this->_COUNTY++;
213 
214  if ($this->_COUNTY == $this->_Y_Number) {
215  // Si on est en bas de page, on remonte le 'curseur' de position
216  $this->_COUNTX++;
217  $this->_COUNTY=0;
218  }
219 
220  if ($this->_COUNTX == $this->_X_Number) {
221  // Si on est en bout de page, alors on repart sur une nouvelle page
222  $this->_COUNTX=0;
223  $this->_COUNTY=0;
224  }
225  }
226 
227 
228 
229 
230  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
241  function write_file($arrayofrecords,$outputlangs,$srctemplatepath,$outputdir='',$filename='tmp_address_sheet.pdf')
242  {
243  // phpcs:enable
244  global $user,$conf,$langs,$mysoc,$_Avery_Labels;
245 
246  $this->code=$srctemplatepath;
247  $this->Tformat = $_Avery_Labels[$this->code];
248  if (empty($this->Tformat)) { dol_print_error('','ErrorBadTypeForCard'.$this->code); exit; }
249  $this->type = 'pdf';
250  // standard format or custom
251  if ($this->Tformat['paper-size']!='custom') {
252  $this->format = $this->Tformat['paper-size'];
253  } else {
254  //custom
255  $resolution= array($this->Tformat['custom_x'], $this->Tformat['custom_y']);
256  $this->format = $resolution;
257  }
258 
259  if (! is_object($outputlangs)) $outputlangs=$langs;
260  // For backward compatibility with FPDF, force output charset to ISO, because FPDF expect text to be encoded in ISO
261  if (! empty($conf->global->MAIN_USE_FPDF)) $outputlangs->charset_output='ISO-8859-1';
262 
263  // Load traductions files requiredby by page
264  $outputlangs->loadLangs(array("main", "dict", "companies", "admin"));
265 
266  $title=$outputlangs->transnoentities('Labels');
267  $keywords=$title." ".$outputlangs->convToOutputCharset($mysoc->name);
268 
269  $dir = (empty($outputdir)?$conf->adherent->dir_temp:$outputdir);
270  $file = $dir."/".$filename;
271 
272  if (! file_exists($dir))
273  {
274  if (dol_mkdir($dir) < 0)
275  {
276  $this->error=$langs->trans("ErrorCanNotCreateDir",$dir);
277  return 0;
278  }
279  }
280 
281  $pdf=pdf_getInstance($this->format,$this->Tformat['metric'], $this->Tformat['orientation']);
282 
283  if (class_exists('TCPDF'))
284  {
285  $pdf->setPrintHeader(false);
286  $pdf->setPrintFooter(false);
287  }
288  $pdf->SetFont(pdf_getPDFFont($outputlangs));
289 
290  $pdf->SetTitle($title);
291  $pdf->SetSubject($title);
292  $pdf->SetCreator("Dolibarr ".DOL_VERSION);
293  $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
294  $pdf->SetKeyWords($keywords);
295  if (! empty($conf->global->MAIN_DISABLE_PDF_COMPRESSION)) $pdf->SetCompression(false);
296 
297  $pdf->SetMargins(0,0);
298  $pdf->SetAutoPageBreak(false);
299 
300  $this->_Metric_Doc = $this->Tformat['metric'];
301  // Permet de commencer l'impression de l'etiquette desiree dans le cas ou la page a deja servie
302  $posX=1;
303  $posY=1;
304  if ($posX > 0) $posX--; else $posX=0;
305  if ($posY > 0) $posY--; else $posY=0;
306  $this->_COUNTX = $posX;
307  $this->_COUNTY = $posY;
308  $this->_Set_Format($pdf, $this->Tformat);
309 
310 
311  $pdf->Open();
312  $pdf->AddPage();
313 
314 
315  // Add each record
316  foreach($arrayofrecords as $val)
317  {
318  // imprime le texte specifique sur la carte
319  $this->Add_PDF_label($pdf,$val['textleft'],$val['textheader'],$val['textfooter'],$langs,$val['textright'],$val['photo']);
320  }
321 
322  //$pdf->SetXY(10, 295);
323  //$pdf->Cell($this->_Width, $this->_Line_Height, 'XXX',0,1,'C');
324 
325 
326  // Output to file
327  $pdf->Output($file,'F');
328 
329  if (! empty($conf->global->MAIN_UMASK))
330  @chmod($file, octdec($conf->global->MAIN_UMASK));
331 
332  // Output to http stream
333  clearstatcache();
334 
335  $attachment=true;
336  if (! empty($conf->global->MAIN_DISABLE_FORCE_SAVEAS)) $attachment=false;
337  $type=dol_mimetype($filename);
338 
339  //if ($encoding) header('Content-Encoding: '.$encoding);
340  if ($type) header('Content-Type: '.$type);
341  if ($attachment) header('Content-Disposition: attachment; filename="'.$filename.'"');
342  else header('Content-Disposition: inline; filename="'.$filename.'"');
343 
344  // Ajout directives pour resoudre bug IE
345  header('Cache-Control: Public, must-revalidate');
346  header('Pragma: public');
347 
348  readfile($file);
349 
350  $this->result = array('fullpath'=>$file);
351 
352  return 1;
353  }
354 }
write_file($arrayofrecords, $outputlangs, $srctemplatepath, $outputdir='', $filename='tmp_address_sheet.pdf')
Function to build PDF on disk, then output on HTTP strem.
pdf_getInstance($format='', $metric='mm', $pagetype='P')
Return a PDF instance object.
Definition: pdf.lib.php:82
_Croix(&$pdf, $x1=0, $y1=0, $x2=210, $y2=297, $epaisseur=1, $taille=4)
protected Function realisant une croix aux 4 coins des cartes
dol_getImageSize($file, $url=false)
Return size of image file on disk (Supported extensions are gif, jpg, png and bmp) ...
Definition: images.lib.php:75
Class to generate stick sheet with format Avery or other personalised.
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
addSticker(&$pdf, $outputlangs, $param)
Output a sticker on page at position _COUNTX, _COUNTY (_COUNTX and _COUNTY start from 0) ...
dol_mkdir($dir, $dataroot='', $newmask=null)
Creation of a directory (this can create recursive subdir)
_Set_Format(&$pdf, $format)
protected Set format
Add_PDF_label(&$pdf, $textleft, $header, $footer, $outputlangs, $textright='', $photo='')
Output a sticker on page at position _COUNTX, _COUNTY (_COUNTX and _COUNTY start from 0) ...