dolibarr  13.0.2
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 <https://www.gnu.org/licenses/>.
21  */
22 
29 require_once DOL_DOCUMENT_ROOT.'/core/class/commonstickergenerator.class.php';
30 
35 {
36 
45  public function addSticker(&$pdf, $outputlangs, $param)
46  {
47  // use this method in future refactoring
48  }
49 
50  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
66  public 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  } elseif (!empty($mysoc->logo) && is_readable($conf->mycompany->dir_output.'/logos/'.$mysoc->logo))
92  {
93  $logo = $conf->mycompany->dir_output.'/logos/'.$mysoc->logo;
94  }
95  }
96 
97  $member = new Adherent($db);
98  $member->id = $idmember;
99  $member->ref = $idmember;
100 
101  // Define photo
102  $dir = $conf->adherent->dir_output;
103  if (!empty($photo))
104  {
105  $file = get_exdir(0, 0, 0, 0, $member, 'member').'photos/'.$photo;
106  $photo = $dir.'/'.$file;
107  if (!is_readable($photo)) $photo = '';
108  }
109 
110  // Define background image
111  $backgroundimage = '';
112  if (!empty($conf->global->ADHERENT_CARD_BACKGROUND) && file_exists($conf->adherent->dir_output.'/'.$conf->global->ADHERENT_CARD_BACKGROUND))
113  {
114  $backgroundimage = $conf->adherent->dir_output.'/'.$conf->global->ADHERENT_CARD_BACKGROUND;
115  }
116 
117  // Print lines
118  if ($this->code == "CARD")
119  {
120  $this->Tformat = $this->_Avery_Labels["CARD"];
121  //$this->_Pointille($pdf,$_PosX,$_PosY,$_PosX+$this->_Width,$_PosY+$this->_Height,0.3,25);
122  $this->_Croix($pdf, $_PosX, $_PosY, $_PosX + $this->_Width, $_PosY + $this->_Height, 0.1, 10);
123  }
124 
125  // Background
126  if ($backgroundimage)
127  {
128  $pdf->image($backgroundimage, $_PosX, $_PosY, $this->_Width, $this->_Height);
129  }
130 
131  $xleft = 2; $ytop = 2;
132 
133  // Top
134  if ($header != '')
135  {
136  if ($this->code == "CARD")
137  {
138  $pdf->SetDrawColor(128, 128, 128);
139  $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
140  $pdf->SetDrawColor(0, 0, 0);
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 
147  $ytop += (empty($header) ? 0 : (1 + $this->_Line_Height));
148 
149  // Define widthtouse and heighttouse
150  $maxwidthtouse = round(($this->_Width - 2 * $xleft) * $imgscalewidth); $maxheighttouse = round(($this->_Height - 2 * $ytop) * $imgscaleheight);
151  $defaultratio = ($maxwidthtouse / $maxheighttouse);
152  $widthtouse = $maxwidthtouse; $heighttouse = 0; // old value for image
153  $tmp = dol_getImageSize($photo, false);
154  if ($tmp['height'])
155  {
156  $imgratio = $tmp['width'] / $tmp['height'];
157  if ($imgratio >= $defaultratio) { $widthtouse = $maxwidthtouse; $heighttouse = round($widthtouse / $imgratio); } else { $heightouse = $maxheighttouse; $widthtouse = round($heightouse * $imgratio); }
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, $widthtouse, $heighttouse);
166  elseif ($textleft == '__PHOTO__' && $photo) $pdf->Image($photo, $_PosX + $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
167  else {
168  $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
169  $pdf->MultiCell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft), 0, 'L');
170  }
171  } elseif ($textleft != '' && $textright != '') //
172  {
173  if ($textleft == '__LOGO__' || $textleft == '__PHOTO__')
174  {
175  if ($textleft == '__LOGO__' && $logo) $pdf->Image($logo, $_PosX + $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
176  elseif ($textleft == '__PHOTO__' && $photo) $pdf->Image($photo, $_PosX + $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
177  $pdf->SetXY($_PosX + $xleft + $widthtouse + 1, $_PosY + $ytop);
178  $pdf->MultiCell($this->_Width - $xleft - $xleft - $widthtouse - 1, $this->_Line_Height, $outputlangs->convToOutputCharset($textright), 0, 'R');
179  } elseif ($textright == '__LOGO__' || $textright == '__PHOTO__')
180  {
181  if ($textright == '__LOGO__' && $logo) $pdf->Image($logo, $_PosX + $this->_Width - $widthtouse - $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
182  elseif ($textright == '__PHOTO__' && $photo) $pdf->Image($photo, $_PosX + $this->_Width - $widthtouse - $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
183  $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
184  $pdf->MultiCell($this->_Width - $widthtouse - $xleft - $xleft - 1, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft), 0, 'L');
185  } else // text on halft left and text on half right
186  {
187  $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
188  $pdf->MultiCell(round($this->_Width / 2), $this->_Line_Height, $outputlangs->convToOutputCharset($textleft), 0, 'L');
189  $pdf->SetXY($_PosX + round($this->_Width / 2), $_PosY + $ytop);
190  $pdf->MultiCell(round($this->_Width / 2) - 2, $this->_Line_Height, $outputlangs->convToOutputCharset($textright), 0, 'R');
191  }
192  } else // Only a right part
193  {
194  // Output right area
195  if ($textright == '__LOGO__' && $logo) $pdf->Image($logo, $_PosX + $this->_Width - $widthtouse - $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
196  elseif ($textright == '__PHOTO__' && $photo) $pdf->Image($photo, $_PosX + $this->_Width - $widthtouse - $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
197  else {
198  $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
199  $pdf->MultiCell($this->_Width - $xleft, $this->_Line_Height, $outputlangs->convToOutputCharset($textright), 0, 'R');
200  }
201  }
202 
203  // Bottom
204  if ($footer != '')
205  {
206  if ($this->code == "CARD")
207  {
208  $pdf->SetDrawColor(128, 128, 128);
209  $pdf->Line($_PosX, $_PosY + $this->_Height - $this->_Line_Height - 2, $_PosX + $this->_Width, $_PosY + $this->_Height - $this->_Line_Height - 2);
210  $pdf->SetDrawColor(0, 0, 0);
211  }
212  $pdf->SetXY($_PosX, $_PosY + $this->_Height - $this->_Line_Height - 1);
213  $pdf->Cell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($footer), 0, 1, 'C');
214  }
215  //print "$_PosY+$this->_Height-$this->_Line_Height-1<br>\n";
216 
217  $this->_COUNTY++;
218 
219  if ($this->_COUNTY == $this->_Y_Number) {
220  // Si on est en bas de page, on remonte le 'curseur' de position
221  $this->_COUNTX++;
222  $this->_COUNTY = 0;
223  }
224 
225  if ($this->_COUNTX == $this->_X_Number) {
226  // Si on est en bout de page, alors on repart sur une nouvelle page
227  $this->_COUNTX = 0;
228  $this->_COUNTY = 0;
229  }
230  }
231 
232  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
243  public function write_file($object, $outputlangs, $srctemplatepath, $mode = 'member', $nooutput = 0)
244  {
245  // phpcs:enable
246  global $user, $conf, $langs, $mysoc, $_Avery_Labels;
247 
248  $this->code = $srctemplatepath;
249 
250  if (is_object($object))
251  {
252  if ($object->country == '-') $object->country = '';
253 
254  $now = dol_now();
255  $year = dol_print_date($now, '%Y');
256  $month = dol_print_date($now, '%m');
257  $day = dol_print_date($now, '%d');
258 
259  // List of values to scan for a replacement
260  $substitutionarray = array(
261  '__ID__' => $object->id,
262  '__LOGIN__'=>$object->login,
263  '__FIRSTNAME__'=>$object->firstname,
264  '__LASTNAME__'=>$object->lastname,
265  '__FULLNAME__'=>$object->getFullName($langs),
266  '__COMPANY__'=>$object->company,
267  '__ADDRESS__'=>$object->address,
268  '__ZIP__'=>$object->zip,
269  '__TOWN__'=>$object->town,
270  '__COUNTRY__'=>$object->country,
271  '__COUNTRY_CODE__'=>$object->country_code,
272  '__EMAIL__'=>$object->email,
273  '__BIRTH__'=>dol_print_date($object->birth, 'day'),
274  '__TYPE__'=>$object->type,
275  '__YEAR__'=>$year,
276  '__MONTH__'=>$month,
277  '__DAY__'=>$day,
278  '__DOL_MAIN_URL_ROOT__'=>DOL_MAIN_URL_ROOT,
279  '__SERVER__'=>"http://".$_SERVER["SERVER_NAME"]."/"
280  );
281  complete_substitutions_array($substitutionarray, $langs);
282 
283  // For business cards
284  $textleft = make_substitutions($conf->global->ADHERENT_CARD_TEXT, $substitutionarray);
285  $textheader = make_substitutions($conf->global->ADHERENT_CARD_HEADER_TEXT, $substitutionarray);
286  $textfooter = make_substitutions($conf->global->ADHERENT_CARD_FOOTER_TEXT, $substitutionarray);
287  $textright = make_substitutions($conf->global->ADHERENT_CARD_TEXT_RIGHT, $substitutionarray);
288 
289  $nb = $_Avery_Labels[$this->code]['NX'] * $_Avery_Labels[$this->code]['NY'];
290  if ($nb <= 0) $nb = 1; // Protection to avoid empty page
291 
292  for ($j = 0; $j < $nb; $j++)
293  {
294  $arrayofmembers[] = array(
295  'textleft'=>$textleft,
296  'textheader'=>$textheader,
297  'textfooter'=>$textfooter,
298  'textright'=>$textright,
299  'id'=>$object->rowid,
300  'photo'=>$object->photo
301  );
302  }
303 
304  $arrayofrecords = $arrayofmembers;
305  } else {
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  // Load traductions files required by page
328  $outputlangs->loadLangs(array("main", "dict", "companies", "admin", "members"));
329 
330  if (empty($mode) || $mode == 'member')
331  {
332  $title = $outputlangs->transnoentities('MembersCards');
333  $keywords = $outputlangs->transnoentities('MembersCards')." ".$outputlangs->transnoentities("Foundation")." ".$outputlangs->convToOutputCharset($mysoc->name);
334  } else {
335  dol_print_error('', 'Bad value for $mode');
336  return -1;
337  }
338 
339  $filename = 'tmp_cards.pdf';
340  if (is_object($object))
341  {
342  $outputdir = $conf->adherent->dir_output;
343  $dir = $outputdir."/".get_exdir(0, 0, 0, 0, $object, 'member');
344  $file = $dir.'/'.$filename;
345  } else {
346  $outputdir = $conf->adherent->dir_temp;
347  $dir = $outputdir;
348  $file = $dir.'/'.$filename;
349  }
350 
351  //var_dump($file);exit;
352 
353  if (!file_exists($dir))
354  {
355  if (dol_mkdir($dir) < 0)
356  {
357  $this->error = $langs->trans("ErrorCanNotCreateDir", $dir);
358  return 0;
359  }
360  }
361 
362  $pdf = pdf_getInstance($this->format, $this->Tformat['metric'], $this->Tformat['orientation']);
363 
364  if (class_exists('TCPDF'))
365  {
366  $pdf->setPrintHeader(false);
367  $pdf->setPrintFooter(false);
368  }
369  $pdf->SetFont(pdf_getPDFFont($outputlangs));
370 
371  $pdf->SetTitle($title);
372  $pdf->SetSubject($title);
373  $pdf->SetCreator("Dolibarr ".DOL_VERSION);
374  $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
375  $pdf->SetKeyWords($keywords);
376  if (!empty($conf->global->MAIN_DISABLE_PDF_COMPRESSION)) $pdf->SetCompression(false);
377 
378  $pdf->SetMargins(0, 0);
379  $pdf->SetAutoPageBreak(false);
380 
381  $this->_Metric_Doc = $this->Tformat['metric'];
382  // Permet de commencer l'impression de l'etiquette desiree dans le cas ou la page a deja servie
383  $posX = 1;
384  $posY = 1;
385  if ($posX > 0) $posX--; else $posX = 0;
386  if ($posY > 0) $posY--; else $posY = 0;
387  $this->_COUNTX = $posX;
388  $this->_COUNTY = $posY;
389  $this->_Set_Format($pdf, $this->Tformat);
390 
391 
392  $pdf->Open();
393  $pdf->AddPage();
394 
395 
396  // Add each record
397  foreach ($arrayofrecords as $val)
398  {
399  // imprime le texte specifique sur la carte
400  $this->Add_PDF_card($pdf, $val['textleft'], $val['textheader'], $val['textfooter'], $langs, $val['textright'], $val['id'], $val['photo']);
401  }
402 
403  //$pdf->SetXY(10, 295);
404  //$pdf->Cell($this->_Width, $this->_Line_Height, 'XXX',0,1,'C');
405 
406 
407  // Output to file
408  $pdf->Output($file, 'F');
409 
410  if (!empty($conf->global->MAIN_UMASK))
411  @chmod($file, octdec($conf->global->MAIN_UMASK));
412 
413 
414  $this->result = array('fullpath'=>$file);
415 
416  // Output to http stream
417  if (empty($nooutput))
418  {
419  clearstatcache();
420 
421  $attachment = true;
422  if (!empty($conf->global->MAIN_DISABLE_FORCE_SAVEAS)) $attachment = false;
423  $type = dol_mimetype($filename);
424 
425  //if ($encoding) header('Content-Encoding: '.$encoding);
426  if ($type) header('Content-Type: '.$type);
427  if ($attachment) header('Content-Disposition: attachment; filename="'.$filename.'"');
428  else header('Content-Disposition: inline; filename="'.$filename.'"');
429 
430  // Ajout directives pour resoudre bug IE
431  header('Cache-Control: Public, must-revalidate');
432  header('Pragma: public');
433 
434  readfile($file);
435  }
436 
437  return 1;
438  }
439 }
_Croix(&$pdf, $x1=0, $y1=0, $x2=210, $y2=297, $epaisseur=1, $taille=4)
protected Function realisant une croix aux 4 coins des cartes
if(!empty($arrayfields['country.code_iso']['checked'])) print_liste_field_titre($arrayfields['country.code_iso']['label'] country if(!empty($arrayfields['typent.code']['checked'])) print_liste_field_titre($arrayfields['typent.code']['label'] typent code
Definition: list.php:566
dol_getImageSize($file, $url=false)
Return size of image file on disk (Supported extensions are gif, jpg, png and bmp) ...
Definition: images.lib.php:86
dol_now($mode= 'auto')
Return date for now.
pdf_getPDFFont($outputlangs)
Return font name to use for PDF generation.
Definition: pdf.lib.php:222
Class to generate stick sheet with format Avery or other personalised.
write_file($object, $outputlangs, $srctemplatepath, $mode= 'member', $nooutput=0)
Function to build PDF on disk, then output on HTTP stream.
Class to generate expense report based on standard model.
dol_mimetype($file, $default= 'application/octet-stream', $mode=0)
Return mime type of a file.
get_exdir($num, $level, $alpha, $withoutslash, $object, $modulepart= '')
Return a path to have a the directory according to object where files are stored. ...
Class to manage members of a foundation.
pdf_getInstance($format= '', $metric= 'mm', $pagetype= 'P')
Return a PDF instance object.
Definition: pdf.lib.php:88
dol_print_date($time, $format= '', $tzoutput= 'auto', $outputlangs= '', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
_Set_Format(&$pdf, $format)
protected Set format
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
make_substitutions($text, $substitutionarray, $outputlangs=null)
Make substitution into a text string, replacing keys with vals from $substitutionarray (oldval=&gt;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) ...
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:105
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)
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 &quot;subst...