dolibarr  13.0.2
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 <https://www.gnu.org/licenses/>.
21  */
22 
29 require_once DOL_DOCUMENT_ROOT.'/core/class/commonstickergenerator.class.php';
30 
35 {
44  public function addSticker(&$pdf, $outputlangs, $param)
45  {
46  // use this method in future refactoring
47  }
48 
49  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
64  public 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  } 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  // Define photo
96  if (!empty($photo))
97  {
98  if (!is_readable($photo)) $photo = '';
99  }
100 
101  // Define background image
102  $backgroundimage = '';
103 
104  // Print lines
105  if ($this->code == "CARD")
106  {
107  $this->Tformat = $this->_Avery_Labels["CARD"];
108  //$this->_Pointille($pdf,$_PosX,$_PosY,$_PosX+$this->_Width,$_PosY+$this->_Height,0.3,25);
109  $this->_Croix($pdf, $_PosX, $_PosY, $_PosX + $this->_Width, $_PosY + $this->_Height, 0.1, 10);
110  }
111 
112  // Background
113  if ($backgroundimage)
114  {
115  $pdf->image($backgroundimage, $_PosX, $_PosY, $this->_Width, $this->_Height);
116  }
117 
118  $xleft = 2; $ytop = 2;
119 
120  // Top
121  if ($header != '')
122  {
123  if ($this->code == "CARD")
124  {
125  $pdf->SetDrawColor(128, 128, 128);
126  $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
127  $pdf->SetDrawColor(0, 0, 0);
128  }
129  $pdf->SetXY($_PosX + $xleft, $_PosY + 1); // Only 1 mm and not ytop for top text
130  $pdf->Cell($this->_Width - 2 * $xleft, $this->_Line_Height, $outputlangs->convToOutputCharset($header), 0, 1, 'C');
131  }
132 
133 
134  $ytop += (empty($header) ? 0 : (1 + $this->_Line_Height));
135 
136  // Define widthtouse and heighttouse
137  $maxwidthtouse = round(($this->_Width - 2 * $xleft) * $imgscalewidth); $maxheighttouse = round(($this->_Height - 2 * $ytop) * $imgscaleheight);
138  $defaultratio = ($maxwidthtouse / $maxheighttouse);
139  $widthtouse = $maxwidthtouse; $heighttouse = 0; // old value for image
140  $tmp = dol_getImageSize($photo, false);
141  if ($tmp['height'])
142  {
143  $imgratio = $tmp['width'] / $tmp['height'];
144  if ($imgratio >= $defaultratio) { $widthtouse = $maxwidthtouse; $heighttouse = round($widthtouse / $imgratio); } else { $heightouse = $maxheighttouse; $widthtouse = round($heightouse * $imgratio); }
145  }
146  //var_dump($this->_Width.'x'.$this->_Height.' with border and scale '.$imgscale.' => max '.$maxwidthtouse.'x'.$maxheighttouse.' => We use '.$widthtouse.'x'.$heighttouse);exit;
147 
148  // Center
149  if ($textright == '') // Only a left part
150  {
151  // Output left area
152  if ($textleft == '%LOGO%' && $logo) $pdf->Image($logo, $_PosX + $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
153  elseif ($textleft == '%PHOTO%' && $photo) $pdf->Image($photo, $_PosX + $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
154  else {
155  $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
156  $pdf->MultiCell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft), 0, 'L');
157  }
158  } elseif ($textleft != '' && $textright != '') //
159  {
160  if ($textleft == '%LOGO%' || $textleft == '%PHOTO%')
161  {
162  if ($textleft == '%LOGO%' && $logo) $pdf->Image($logo, $_PosX + $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
163  elseif ($textleft == '%PHOTO%' && $photo) $pdf->Image($photo, $_PosX + $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
164  $pdf->SetXY($_PosX + $xleft + $widthtouse + 1, $_PosY + $ytop);
165  $pdf->MultiCell($this->_Width - $xleft - $xleft - $widthtouse - 1, $this->_Line_Height, $outputlangs->convToOutputCharset($textright), 0, 'R');
166  } elseif ($textright == '%LOGO%' || $textright == '%PHOTO%')
167  {
168  if ($textright == '%LOGO%' && $logo) $pdf->Image($logo, $_PosX + $this->_Width - $widthtouse - $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
169  elseif ($textright == '%PHOTO%' && $photo) $pdf->Image($photo, $_PosX + $this->_Width - $widthtouse - $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
170  $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
171  $pdf->MultiCell($this->_Width - $widthtouse - $xleft - $xleft - 1, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft), 0, 'L');
172  } else // text on halft left and text on half right
173  {
174  $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
175  $pdf->MultiCell(round($this->_Width / 2), $this->_Line_Height, $outputlangs->convToOutputCharset($textleft), 0, 'L');
176  $pdf->SetXY($_PosX + round($this->_Width / 2), $_PosY + $ytop);
177  $pdf->MultiCell(round($this->_Width / 2) - 2, $this->_Line_Height, $outputlangs->convToOutputCharset($textright), 0, 'R');
178  }
179  } else // Only a right part
180  {
181  // Output right area
182  if ($textright == '%LOGO%' && $logo) $pdf->Image($logo, $_PosX + $this->_Width - $widthtouse - $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
183  elseif ($textright == '%PHOTO%' && $photo) $pdf->Image($photo, $_PosX + $this->_Width - $widthtouse - $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
184  else {
185  $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
186  $pdf->MultiCell($this->_Width - $xleft, $this->_Line_Height, $outputlangs->convToOutputCharset($textright), 0, 'R');
187  }
188  }
189 
190  // Bottom
191  if ($footer != '')
192  {
193  if ($this->code == "CARD")
194  {
195  $pdf->SetDrawColor(128, 128, 128);
196  $pdf->Line($_PosX, $_PosY + $this->_Height - $this->_Line_Height - 2, $_PosX + $this->_Width, $_PosY + $this->_Height - $this->_Line_Height - 2);
197  $pdf->SetDrawColor(0, 0, 0);
198  }
199  $pdf->SetXY($_PosX, $_PosY + $this->_Height - $this->_Line_Height - 1);
200  $pdf->Cell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($footer), 0, 1, 'C');
201  }
202  //print "$_PosY+$this->_Height-$this->_Line_Height-1<br>\n";
203 
204  $this->_COUNTY++;
205 
206  if ($this->_COUNTY == $this->_Y_Number) {
207  // Si on est en bas de page, on remonte le 'curseur' de position
208  $this->_COUNTX++;
209  $this->_COUNTY = 0;
210  }
211 
212  if ($this->_COUNTX == $this->_X_Number) {
213  // Si on est en bout de page, alors on repart sur une nouvelle page
214  $this->_COUNTX = 0;
215  $this->_COUNTY = 0;
216  }
217  }
218 
219 
220 
221 
222  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
233  public function write_file($arrayofrecords, $outputlangs, $srctemplatepath, $outputdir = '', $filename = 'tmp_address_sheet.pdf')
234  {
235  // phpcs:enable
236  global $user, $conf, $langs, $mysoc, $_Avery_Labels;
237 
238  $this->code = $srctemplatepath;
239  $this->Tformat = $_Avery_Labels[$this->code];
240  if (empty($this->Tformat)) { dol_print_error('', 'ErrorBadTypeForCard'.$this->code); exit; }
241  $this->type = 'pdf';
242  // standard format or custom
243  if ($this->Tformat['paper-size'] != 'custom') {
244  $this->format = $this->Tformat['paper-size'];
245  } else {
246  //custom
247  $resolution = array($this->Tformat['custom_x'], $this->Tformat['custom_y']);
248  $this->format = $resolution;
249  }
250 
251  if (!is_object($outputlangs)) $outputlangs = $langs;
252  // For backward compatibility with FPDF, force output charset to ISO, because FPDF expect text to be encoded in ISO
253  if (!empty($conf->global->MAIN_USE_FPDF)) $outputlangs->charset_output = 'ISO-8859-1';
254 
255  // Load traductions files required by page
256  $outputlangs->loadLangs(array("main", "dict", "companies", "admin"));
257 
258  $title = $outputlangs->transnoentities('Labels');
259  $keywords = $title." ".$outputlangs->convToOutputCharset($mysoc->name);
260 
261  $dir = (empty($outputdir) ? $conf->adherent->dir_temp : $outputdir);
262  $file = $dir."/".$filename;
263 
264  if (!file_exists($dir))
265  {
266  if (dol_mkdir($dir) < 0)
267  {
268  $this->error = $langs->trans("ErrorCanNotCreateDir", $dir);
269  return 0;
270  }
271  }
272 
273  $pdf = pdf_getInstance($this->format, $this->Tformat['metric'], $this->Tformat['orientation']);
274 
275  if (class_exists('TCPDF'))
276  {
277  $pdf->setPrintHeader(false);
278  $pdf->setPrintFooter(false);
279  }
280  $pdf->SetFont(pdf_getPDFFont($outputlangs));
281 
282  $pdf->SetTitle($title);
283  $pdf->SetSubject($title);
284  $pdf->SetCreator("Dolibarr ".DOL_VERSION);
285  $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
286  $pdf->SetKeyWords($keywords);
287  if (!empty($conf->global->MAIN_DISABLE_PDF_COMPRESSION)) $pdf->SetCompression(false);
288 
289  $pdf->SetMargins(0, 0);
290  $pdf->SetAutoPageBreak(false);
291 
292  $this->_Metric_Doc = $this->Tformat['metric'];
293  // Permet de commencer l'impression de l'etiquette desiree dans le cas ou la page a deja servie
294  $posX = 1;
295  $posY = 1;
296  if ($posX > 0) $posX--; else $posX = 0;
297  if ($posY > 0) $posY--; else $posY = 0;
298  $this->_COUNTX = $posX;
299  $this->_COUNTY = $posY;
300  $this->_Set_Format($pdf, $this->Tformat);
301 
302 
303  $pdf->Open();
304  $pdf->AddPage();
305 
306 
307  // Add each record
308  foreach ($arrayofrecords as $val)
309  {
310  // imprime le texte specifique sur la carte
311  $this->Add_PDF_label($pdf, $val['textleft'], $val['textheader'], $val['textfooter'], $langs, $val['textright'], $val['photo']);
312  }
313 
314  //$pdf->SetXY(10, 295);
315  //$pdf->Cell($this->_Width, $this->_Line_Height, 'XXX',0,1,'C');
316 
317 
318  // Output to file
319  $pdf->Output($file, 'F');
320 
321  if (!empty($conf->global->MAIN_UMASK))
322  @chmod($file, octdec($conf->global->MAIN_UMASK));
323 
324  $this->result = array('fullpath'=>$file);
325 
326  return 1;
327  }
328 }
_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
Class to generate stick sheet with format Avery or other personalised.
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.
addSticker(&$pdf, $outputlangs, $param)
Output a sticker on page at position _COUNTX, _COUNTY (_COUNTX and _COUNTY start from 0) ...
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) ...
pdf_getInstance($format= '', $metric= 'mm', $pagetype= 'P')
Return a PDF instance object.
Definition: pdf.lib.php:88
_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...
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:105
dol_mkdir($dir, $dataroot= '', $newmask=null)
Creation of a directory (this can create recursive subdir)
write_file($arrayofrecords, $outputlangs, $srctemplatepath, $outputdir= '', $filename= 'tmp_address_sheet.pdf')
Function to build PDF on disk, then output on HTTP strem.