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