dolibarr  9.0.0
pdf_standard.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 {
36 
45  function addSticker(&$pdf,$outputlangs,$param)
46  {
47  // use this method in future refactoring
48  }
49 
50  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
66  function Add_PDF_card(&$pdf,$textleft,$header,$footer,$outputlangs,$textright='',$idmember=0,$photo='')
67  {
68  // phpcs:enable
69  global $db,$mysoc,$conf,$langs;
70  global $forceimgscalewidth,$forceimgscaleheight;
71 
72  $imgscalewidth=(empty($forceimgscalewidth)?0.3:$forceimgscalewidth); // Scale of image for width (1=Full width of sticker)
73  $imgscaleheight=(empty($forceimgscalewidth)?0.5:$forceimgscalewidth); // Scale of image for height (1=Full height of sticker)
74 
75  // We are in a new page, then we must add a page
76  if (($this->_COUNTX ==0) && ($this->_COUNTY==0) and (!$this->_First==1)) {
77  $pdf->AddPage();
78  }
79  $this->_First=0;
80  $_PosX = $this->_Margin_Left+($this->_COUNTX*($this->_Width+$this->_X_Space));
81  $_PosY = $this->_Margin_Top+($this->_COUNTY*($this->_Height+$this->_Y_Space));
82 
83  // Define logo
84  $logo=$conf->mycompany->dir_output.'/logos/'.$mysoc->logo;
85  if (! is_readable($logo))
86  {
87  $logo='';
88  if (! empty($mysoc->logo_small) && is_readable($conf->mycompany->dir_output.'/logos/thumbs/'.$mysoc->logo_small))
89  {
90  $logo=$conf->mycompany->dir_output.'/logos/thumbs/'.$mysoc->logo_small;
91  }
92  elseif (! empty($mysoc->logo) && is_readable($conf->mycompany->dir_output.'/logos/'.$mysoc->logo))
93  {
94  $logo=$conf->mycompany->dir_output.'/logos/'.$mysoc->logo;
95  }
96  }
97 
98  $member=new Adherent($db);
99  $member->id = $idmember;
100  $member->ref = $idmember;
101 
102  // Define photo
103  $dir=$conf->adherent->dir_output;
104  if (! empty($photo))
105  {
106  $file=get_exdir(0,0,0,0,$member,'member').'photos/'.$photo;
107  $photo=$dir.'/'.$file;
108  if (! is_readable($photo)) $photo='';
109  }
110 
111  // Define background image
112  $backgroundimage='';
113  if(! empty($conf->global->ADHERENT_CARD_BACKGROUND) && file_exists($conf->adherent->dir_output.'/'.$conf->global->ADHERENT_CARD_BACKGROUND))
114  {
115  $backgroundimage=$conf->adherent->dir_output.'/'.$conf->global->ADHERENT_CARD_BACKGROUND;
116  }
117 
118  // Print lines
119  if ($this->code == "CARD")
120  {
121  $this->Tformat=$this->_Avery_Labels["CARD"];
122  //$this->_Pointille($pdf,$_PosX,$_PosY,$_PosX+$this->_Width,$_PosY+$this->_Height,0.3,25);
123  $this->_Croix($pdf,$_PosX,$_PosY,$_PosX+$this->_Width,$_PosY+$this->_Height,0.1,10);
124  }
125 
126  // Background
127  if ($backgroundimage)
128  {
129  $pdf->image($backgroundimage,$_PosX,$_PosY,$this->_Width,$this->_Height);
130  }
131 
132  $xleft=2; $ytop=2;
133 
134  // Top
135  if ($header!='')
136  {
137  if ($this->code == "CARD")
138  {
139  $pdf->SetDrawColor(128,128,128);
140  $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
141  $pdf->SetDrawColor(0,0,0);
142  }
143  $pdf->SetXY($_PosX+$xleft, $_PosY+1); // Only 1 mm and not ytop for top text
144  $pdf->Cell($this->_Width-2*$xleft, $this->_Line_Height, $outputlangs->convToOutputCharset($header),0,1,'C');
145  }
146 
147 
148  $ytop+=(empty($header)?0:(1+$this->_Line_Height));
149 
150  // Define widthtouse and heighttouse
151  $maxwidthtouse=round(($this->_Width - 2*$xleft)*$imgscalewidth); $maxheighttouse=round(($this->_Height - 2*$ytop)*$imgscaleheight);
152  $defaultratio=($maxwidthtouse/$maxheighttouse);
153  $widthtouse=$maxwidthtouse; $heighttouse=0; // old value for image
154  $tmp=dol_getImageSize($photo, false);
155  if ($tmp['height'])
156  {
157  $imgratio=$tmp['width']/$tmp['height'];
158  if ($imgratio >= $defaultratio) { $widthtouse = $maxwidthtouse; $heighttouse = round($widthtouse / $imgratio); }
159  else { $heightouse = $maxheighttouse; $widthtouse = round($heightouse * $imgratio); }
160  }
161  //var_dump($this->_Width.'x'.$this->_Height.' with border and scale '.$imgscale.' => max '.$maxwidthtouse.'x'.$maxheighttouse.' => We use '.$widthtouse.'x'.$heighttouse);exit;
162 
163  // Center
164  if ($textright=='') // Only a left part
165  {
166  // Output left area
167  if ($textleft == '__LOGO__' && $logo) $pdf->Image($logo,$_PosX+$xleft,$_PosY+$ytop,$widthtouse,$heighttouse);
168  else if ($textleft == '__PHOTO__' && $photo) $pdf->Image($photo,$_PosX+$xleft,$_PosY+$ytop,$widthtouse,$heighttouse);
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!='') //
176  {
177  if ($textleft == '__LOGO__' || $textleft == '__PHOTO__')
178  {
179  if ($textleft == '__LOGO__' && $logo) $pdf->Image($logo,$_PosX+$xleft,$_PosY+$ytop,$widthtouse,$heighttouse);
180  else if ($textleft == '__PHOTO__' && $photo) $pdf->Image($photo,$_PosX+$xleft,$_PosY+$ytop,$widthtouse,$heighttouse);
181  $pdf->SetXY($_PosX+$xleft+$widthtouse+1, $_PosY+$ytop);
182  $pdf->MultiCell($this->_Width-$xleft-$xleft-$widthtouse-1, $this->_Line_Height, $outputlangs->convToOutputCharset($textright),0,'R');
183  }
184  else if ($textright == '__LOGO__' || $textright == '__PHOTO__')
185  {
186  if ($textright == '__LOGO__' && $logo) $pdf->Image($logo,$_PosX+$this->_Width-$widthtouse-$xleft,$_PosY+$ytop,$widthtouse,$heighttouse);
187  else if ($textright == '__PHOTO__' && $photo) $pdf->Image($photo,$_PosX+$this->_Width-$widthtouse-$xleft,$_PosY+$ytop,$widthtouse,$heighttouse);
188  $pdf->SetXY($_PosX+$xleft, $_PosY+$ytop);
189  $pdf->MultiCell($this->_Width-$widthtouse-$xleft-$xleft-1, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft),0,'L');
190  }
191  else // text on halft left and text on half right
192  {
193  $pdf->SetXY($_PosX+$xleft, $_PosY+$ytop);
194  $pdf->MultiCell(round($this->_Width/2), $this->_Line_Height, $outputlangs->convToOutputCharset($textleft),0,'L');
195  $pdf->SetXY($_PosX+round($this->_Width/2), $_PosY+$ytop);
196  $pdf->MultiCell(round($this->_Width/2)-2, $this->_Line_Height, $outputlangs->convToOutputCharset($textright),0,'R');
197  }
198  }
199  else // Only a right part
200  {
201  // Output right area
202  if ($textright == '__LOGO__' && $logo) $pdf->Image($logo,$_PosX+$this->_Width-$widthtouse-$xleft,$_PosY+$ytop,$widthtouse,$heighttouse);
203  else if ($textright == '__PHOTO__' && $photo) $pdf->Image($photo,$_PosX+$this->_Width-$widthtouse-$xleft,$_PosY+$ytop,$widthtouse,$heighttouse);
204  else
205  {
206  $pdf->SetXY($_PosX+$xleft, $_PosY+$ytop);
207  $pdf->MultiCell($this->_Width-$xleft, $this->_Line_Height, $outputlangs->convToOutputCharset($textright),0,'R');
208  }
209  }
210 
211  // Bottom
212  if ($footer!='')
213  {
214  if ($this->code == "CARD")
215  {
216  $pdf->SetDrawColor(128,128,128);
217  $pdf->Line($_PosX, $_PosY+$this->_Height-$this->_Line_Height-2, $_PosX+$this->_Width, $_PosY+$this->_Height-$this->_Line_Height-2);
218  $pdf->SetDrawColor(0,0,0);
219  }
220  $pdf->SetXY($_PosX, $_PosY+$this->_Height-$this->_Line_Height-1);
221  $pdf->Cell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($footer),0,1,'C');
222  }
223  //print "$_PosY+$this->_Height-$this->_Line_Height-1<br>\n";
224 
225  $this->_COUNTY++;
226 
227  if ($this->_COUNTY == $this->_Y_Number) {
228  // Si on est en bas de page, on remonte le 'curseur' de position
229  $this->_COUNTX++;
230  $this->_COUNTY=0;
231  }
232 
233  if ($this->_COUNTX == $this->_X_Number) {
234  // Si on est en bout de page, alors on repart sur une nouvelle page
235  $this->_COUNTX=0;
236  $this->_COUNTY=0;
237  }
238  }
239 
240  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
251  function write_file($object, $outputlangs, $srctemplatepath, $mode='member', $nooutput=0)
252  {
253  // phpcs:enable
254  global $user,$conf,$langs,$mysoc,$_Avery_Labels;
255 
256  $this->code=$srctemplatepath;
257 
258  if (is_object($object))
259  {
260  if ($object->country == '-') $object->country='';
261 
262  // List of values to scan for a replacement
263  $substitutionarray = array (
264  '__ID__'=>$object->rowid,
265  '__LOGIN__'=>$object->login,
266  '__FIRSTNAME__'=>$object->firstname,
267  '__LASTNAME__'=>$object->lastname,
268  '__FULLNAME__'=>$object->getFullName($langs),
269  '__COMPANY__'=>$object->company,
270  '__ADDRESS__'=>$object->address,
271  '__ZIP__'=>$object->zip,
272  '__TOWN__'=>$object->town,
273  '__COUNTRY__'=>$object->country,
274  '__COUNTRY_CODE__'=>$object->country_code,
275  '__EMAIL__'=>$object->email,
276  '__BIRTH__'=>dol_print_date($object->birth,'day'),
277  '__TYPE__'=>$object->type,
278  '__YEAR__'=>$year,
279  '__MONTH__'=>$month,
280  '__DAY__'=>$day,
281  '__DOL_MAIN_URL_ROOT__'=>DOL_MAIN_URL_ROOT,
282  '__SERVER__'=>"http://".$_SERVER["SERVER_NAME"]."/"
283  );
284  complete_substitutions_array($substitutionarray, $langs);
285 
286  // For business cards
287  $textleft=make_substitutions($conf->global->ADHERENT_CARD_TEXT, $substitutionarray);
288  $textheader=make_substitutions($conf->global->ADHERENT_CARD_HEADER_TEXT, $substitutionarray);
289  $textfooter=make_substitutions($conf->global->ADHERENT_CARD_FOOTER_TEXT, $substitutionarray);
290  $textright=make_substitutions($conf->global->ADHERENT_CARD_TEXT_RIGHT, $substitutionarray);
291 
292  $nb = $_Avery_Labels[$this->code]['NX'] * $_Avery_Labels[$this->code]['NY'];
293  if ($nb <= 0) $nb=1; // Protection to avoid empty page
294 
295  for($j=0;$j<$nb;$j++)
296  {
297  $arrayofmembers[]=array(
298  'textleft'=>$textleft,
299  'textheader'=>$textheader,
300  'textfooter'=>$textfooter,
301  'textright'=>$textright,
302  'id'=>$object->rowid,
303  'photo'=>$object->photo
304  );
305  }
306 
307  $arrayofrecords = $arrayofmembers;
308  }
309  else
310  {
311  $arrayofrecords = $object;
312  }
313 
314  //var_dump($arrayofrecords);exit;
315 
316  $this->Tformat = $_Avery_Labels[$this->code];
317  if (empty($this->Tformat)) { dol_print_error('','ErrorBadTypeForCard'.$this->code); exit; }
318  $this->type = 'pdf';
319  // standard format or custom
320  if ($this->Tformat['paper-size']!='custom') {
321  $this->format = $this->Tformat['paper-size'];
322  } else {
323  //custom
324  $resolution= array($this->Tformat['custom_x'], $this->Tformat['custom_y']);
325  $this->format = $resolution;
326  }
327 
328  if (! is_object($outputlangs)) $outputlangs=$langs;
329  // For backward compatibility with FPDF, force output charset to ISO, because FPDF expect text to be encoded in ISO
330  if (! empty($conf->global->MAIN_USE_FPDF)) $outputlangs->charset_output='ISO-8859-1';
331 
332  // Load traductions files requiredby by page
333  $outputlangs->loadLangs(array("main", "dict", "companies", "admin", "members"));
334 
335  if (empty($mode) || $mode == 'member')
336  {
337  $title=$outputlangs->transnoentities('MembersCards');
338  $keywords=$outputlangs->transnoentities('MembersCards')." ".$outputlangs->transnoentities("Foundation")." ".$outputlangs->convToOutputCharset($mysoc->name);
339  }
340  else
341  {
342  dol_print_error('','Bad value for $mode');
343  return -1;
344  }
345 
346  $filename = 'tmp_cards.pdf';
347  if (is_object($object))
348  {
349  $outputdir = $conf->adherent->dir_output;
350  $dir = $outputdir."/".get_exdir(0, 0, 0, 0, $object, 'member');
351  $file = $dir.'/'.$filename;
352  }
353  else
354  {
355  $outputdir = $conf->adherent->dir_temp;
356  $dir = $outputdir;
357  $file = $dir.'/'.$filename;
358  }
359 
360  //var_dump($file);exit;
361 
362  if (! file_exists($dir))
363  {
364  if (dol_mkdir($dir) < 0)
365  {
366  $this->error=$langs->trans("ErrorCanNotCreateDir",$dir);
367  return 0;
368  }
369  }
370 
371  $pdf=pdf_getInstance($this->format,$this->Tformat['metric'], $this->Tformat['orientation']);
372 
373  if (class_exists('TCPDF'))
374  {
375  $pdf->setPrintHeader(false);
376  $pdf->setPrintFooter(false);
377  }
378  $pdf->SetFont(pdf_getPDFFont($outputlangs));
379 
380  $pdf->SetTitle($title);
381  $pdf->SetSubject($title);
382  $pdf->SetCreator("Dolibarr ".DOL_VERSION);
383  $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
384  $pdf->SetKeyWords($keywords);
385  if (! empty($conf->global->MAIN_DISABLE_PDF_COMPRESSION)) $pdf->SetCompression(false);
386 
387  $pdf->SetMargins(0,0);
388  $pdf->SetAutoPageBreak(false);
389 
390  $this->_Metric_Doc = $this->Tformat['metric'];
391  // Permet de commencer l'impression de l'etiquette desiree dans le cas ou la page a deja servie
392  $posX=1;
393  $posY=1;
394  if ($posX > 0) $posX--; else $posX=0;
395  if ($posY > 0) $posY--; else $posY=0;
396  $this->_COUNTX = $posX;
397  $this->_COUNTY = $posY;
398  $this->_Set_Format($pdf, $this->Tformat);
399 
400 
401  $pdf->Open();
402  $pdf->AddPage();
403 
404 
405  // Add each record
406  foreach($arrayofrecords as $val)
407  {
408  // imprime le texte specifique sur la carte
409  $this->Add_PDF_card($pdf,$val['textleft'],$val['textheader'],$val['textfooter'],$langs,$val['textright'],$val['id'],$val['photo']);
410  }
411 
412  //$pdf->SetXY(10, 295);
413  //$pdf->Cell($this->_Width, $this->_Line_Height, 'XXX',0,1,'C');
414 
415 
416  // Output to file
417  $pdf->Output($file,'F');
418 
419  if (! empty($conf->global->MAIN_UMASK))
420  @chmod($file, octdec($conf->global->MAIN_UMASK));
421 
422 
423  $this->result = array('fullpath'=>$file);
424 
425  // Output to http stream
426  if (empty($nooutput))
427  {
428  clearstatcache();
429 
430  $attachment=true;
431  if (! empty($conf->global->MAIN_DISABLE_FORCE_SAVEAS)) $attachment=false;
432  $type=dol_mimetype($filename);
433 
434  //if ($encoding) header('Content-Encoding: '.$encoding);
435  if ($type) header('Content-Type: '.$type);
436  if ($attachment) header('Content-Disposition: attachment; filename="'.$filename.'"');
437  else header('Content-Disposition: inline; filename="'.$filename.'"');
438 
439  // Ajout directives pour resoudre bug IE
440  header('Cache-Control: Public, must-revalidate');
441  header('Pragma: public');
442 
443  readfile($file);
444  }
445 
446  return 1;
447  }
448 }
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
get_exdir($num, $level, $alpha, $withoutslash, $object, $modulepart)
Return a path to have a the directory according to object where files are stored. ...
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.
Class to generate expense report based on standard model.
write_file($object, $outputlangs, $srctemplatepath, $mode='member', $nooutput=0)
Function to build PDF on disk, then output on HTTP stream.
type
Definition: viewcat.php:284
Class to manage members of a foundation.
dol_print_date($time, $format='', $tzoutput='tzserver', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_mkdir($dir, $dataroot='', $newmask=null)
Creation of a directory (this can create recursive subdir)
_Set_Format(&$pdf, $format)
protected Set format
make_substitutions($text, $substitutionarray, $outputlangs=null)
Make substitution into a text string, replacing keys with vals from $substitutionarray (oldval=>newva...
Add_PDF_card(&$pdf, $textleft, $header, $footer, $outputlangs, $textright='', $idmember=0, $photo='')
Output a sticker on page at position _COUNTX, _COUNTY (_COUNTX and _COUNTY start from 0) ...
addSticker(&$pdf, $outputlangs, $param)
Output a sticker on page at position _COUNTX, _COUNTY (_COUNTX and _COUNTY start from 0) ...
complete_substitutions_array(&$substitutionarray, $outputlangs, $object=null, $parameters=null, $callfunc="completesubstitutionarray")
Complete the $substitutionarray with more entries coming from external module that had set the "subst...