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