dolibarr  18.0.0-alpha
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  $logo = '';
85  if (!empty($mysoc->logo_small) && is_readable($conf->mycompany->dir_output.'/logos/thumbs/'.$mysoc->logo_small)) {
86  $logo = $conf->mycompany->dir_output.'/logos/thumbs/'.$mysoc->logo_small;
87  } elseif (!empty($mysoc->logo) && is_readable($conf->mycompany->dir_output.'/logos/'.$mysoc->logo)) {
88  $logo = $conf->mycompany->dir_output.'/logos/'.$mysoc->logo;
89  }
90  }
91 
92  // Define photo
93  if (!empty($photo)) {
94  if (!is_readable($photo)) {
95  $photo = '';
96  }
97  }
98 
99  // Define background image
100  $backgroundimage = '';
101 
102  // Print lines
103  if ($this->code == "CARD") {
104  $this->Tformat = $this->_Avery_Labels["CARD"];
105  //$this->_Pointille($pdf,$_PosX,$_PosY,$_PosX+$this->_Width,$_PosY+$this->_Height,0.3,25);
106  $this->_Croix($pdf, $_PosX, $_PosY, $_PosX + $this->_Width, $_PosY + $this->_Height, 0.1, 10);
107  }
108 
109  // Background
110  if ($backgroundimage) {
111  $pdf->image($backgroundimage, $_PosX, $_PosY, $this->_Width, $this->_Height);
112  }
113 
114  $xleft = 2;
115  $ytop = 2;
116 
117  // Top
118  if ($header != '') {
119  if ($this->code == "CARD") {
120  $pdf->SetDrawColor(128, 128, 128);
121  $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
122  $pdf->SetDrawColor(0, 0, 0);
123  }
124  $pdf->SetXY($_PosX + $xleft, $_PosY + 1); // Only 1 mm and not ytop for top text
125  $pdf->Cell($this->_Width - 2 * $xleft, $this->_Line_Height, $outputlangs->convToOutputCharset($header), 0, 1, 'C');
126  }
127 
128 
129  $ytop += (empty($header) ? 0 : (1 + $this->_Line_Height));
130 
131  // Define widthtouse and heighttouse
132  $maxwidthtouse = round(($this->_Width - 2 * $xleft) * $imgscalewidth);
133  $maxheighttouse = round(($this->_Height - 2 * $ytop) * $imgscaleheight);
134  $defaultratio = ($maxwidthtouse / $maxheighttouse);
135  $widthtouse = $maxwidthtouse;
136  $heighttouse = 0; // old value for image
137  $tmp = dol_getImageSize($photo, false);
138  if (!empty($tmp['height'])) {
139  $imgratio = $tmp['width'] / $tmp['height'];
140  if ($imgratio >= $defaultratio) {
141  $widthtouse = $maxwidthtouse;
142  $heighttouse = round($widthtouse / $imgratio);
143  } else {
144  $heighttouse = $maxheighttouse;
145  $widthtouse = round($heighttouse * $imgratio);
146  }
147  }
148  //var_dump($this->_Width.'x'.$this->_Height.' with border and scale '.$imgscale.' => max '.$maxwidthtouse.'x'.$maxheighttouse.' => We use '.$widthtouse.'x'.$heighttouse);exit;
149 
150  // Center
151  if ($textright == '') { // Only a left part
152  // Output left area
153  if ($textleft == '%LOGO%' && $logo) {
154  $pdf->Image($logo, $_PosX + $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
155  } elseif ($textleft == '%PHOTO%' && $photo) {
156  $pdf->Image($photo, $_PosX + $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
157  } else {
158  $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
159  $pdf->MultiCell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft), 0, 'L');
160  }
161  } elseif ($textleft != '' && $textright != '') { //
162  if ($textleft == '%LOGO%' || $textleft == '%PHOTO%') {
163  if ($textleft == '%LOGO%' && $logo) {
164  $pdf->Image($logo, $_PosX + $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
165  } elseif ($textleft == '%PHOTO%' && $photo) {
166  $pdf->Image($photo, $_PosX + $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
167  }
168  $pdf->SetXY($_PosX + $xleft + $widthtouse + 1, $_PosY + $ytop);
169  $pdf->MultiCell($this->_Width - $xleft - $xleft - $widthtouse - 1, $this->_Line_Height, $outputlangs->convToOutputCharset($textright), 0, 'R');
170  } elseif ($textright == '%LOGO%' || $textright == '%PHOTO%') {
171  if ($textright == '%LOGO%' && $logo) {
172  $pdf->Image($logo, $_PosX + $this->_Width - $widthtouse - $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
173  } elseif ($textright == '%PHOTO%' && $photo) {
174  $pdf->Image($photo, $_PosX + $this->_Width - $widthtouse - $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
175  }
176  $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
177  $pdf->MultiCell($this->_Width - $widthtouse - $xleft - $xleft - 1, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft), 0, 'L');
178  } else // text on halft left and text on half right
179  {
180  $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
181  $pdf->MultiCell(round($this->_Width / 2), $this->_Line_Height, $outputlangs->convToOutputCharset($textleft), 0, 'L');
182  $pdf->SetXY($_PosX + round($this->_Width / 2), $_PosY + $ytop);
183  $pdf->MultiCell(round($this->_Width / 2) - 2, $this->_Line_Height, $outputlangs->convToOutputCharset($textright), 0, 'R');
184  }
185  } else // Only a right part
186  {
187  // Output right area
188  if ($textright == '%LOGO%' && $logo) {
189  $pdf->Image($logo, $_PosX + $this->_Width - $widthtouse - $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
190  } elseif ($textright == '%PHOTO%' && $photo) {
191  $pdf->Image($photo, $_PosX + $this->_Width - $widthtouse - $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
192  } else {
193  $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
194  $pdf->MultiCell($this->_Width - $xleft, $this->_Line_Height, $outputlangs->convToOutputCharset($textright), 0, 'R');
195  }
196  }
197 
198  // Bottom
199  if ($footer != '') {
200  if ($this->code == "CARD") {
201  $pdf->SetDrawColor(128, 128, 128);
202  $pdf->Line($_PosX, $_PosY + $this->_Height - $this->_Line_Height - 2, $_PosX + $this->_Width, $_PosY + $this->_Height - $this->_Line_Height - 2);
203  $pdf->SetDrawColor(0, 0, 0);
204  }
205  $pdf->SetXY($_PosX, $_PosY + $this->_Height - $this->_Line_Height - 1);
206  $pdf->Cell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($footer), 0, 1, 'C');
207  }
208  //print "$_PosY+$this->_Height-$this->_Line_Height-1<br>\n";
209 
210  $this->_COUNTY++;
211 
212  if ($this->_COUNTY == $this->_Y_Number) {
213  // Si on est en bas de page, on remonte le 'curseur' de position
214  $this->_COUNTX++;
215  $this->_COUNTY = 0;
216  }
217 
218  if ($this->_COUNTX == $this->_X_Number) {
219  // Si on est en bout de page, alors on repart sur une nouvelle page
220  $this->_COUNTX = 0;
221  $this->_COUNTY = 0;
222  }
223  }
224 
225 
226 
227 
228  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
239  public function write_file($arrayofrecords, $outputlangs, $srctemplatepath, $outputdir = '', $filename = 'tmp_address_sheet.pdf')
240  {
241  // phpcs:enable
242  global $user, $conf, $langs, $mysoc, $_Avery_Labels;
243 
244  $this->code = $srctemplatepath;
245  $this->Tformat = $_Avery_Labels[$this->code];
246  if (empty($this->Tformat)) {
247  dol_print_error('', 'ErrorBadTypeForCard'.$this->code);
248  exit;
249  }
250  $this->type = 'pdf';
251  // standard format or custom
252  if ($this->Tformat['paper-size'] != 'custom') {
253  $this->format = $this->Tformat['paper-size'];
254  } else {
255  //custom
256  $resolution = array($this->Tformat['custom_x'], $this->Tformat['custom_y']);
257  $this->format = $resolution;
258  }
259 
260  if (!is_object($outputlangs)) {
261  $outputlangs = $langs;
262  }
263  // For backward compatibility with FPDF, force output charset to ISO, because FPDF expect text to be encoded in ISO
264  if (!empty($conf->global->MAIN_USE_FPDF)) {
265  $outputlangs->charset_output = 'ISO-8859-1';
266  }
267 
268  // Load traductions files required by page
269  $outputlangs->loadLangs(array("main", "dict", "companies", "admin"));
270 
271  $title = $outputlangs->transnoentities('Labels');
272  $keywords = $title." ".$outputlangs->convToOutputCharset($mysoc->name);
273 
274  $dir = (empty($outputdir) ? $conf->adherent->dir_temp : $outputdir);
275  $file = $dir."/".$filename;
276 
277  if (!file_exists($dir)) {
278  if (dol_mkdir($dir) < 0) {
279  $this->error = $langs->trans("ErrorCanNotCreateDir", $dir);
280  return 0;
281  }
282  }
283 
284  $pdf = pdf_getInstance($this->format, $this->Tformat['metric'], $this->Tformat['orientation']);
285 
286  if (class_exists('TCPDF')) {
287  $pdf->setPrintHeader(false);
288  $pdf->setPrintFooter(false);
289  }
290  $pdf->SetFont(pdf_getPDFFont($outputlangs));
291 
292  $pdf->SetTitle($title);
293  $pdf->SetSubject($title);
294  $pdf->SetCreator("Dolibarr ".DOL_VERSION);
295  $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
296  $pdf->SetKeyWords($keywords);
297  if (getDolGlobalString('MAIN_DISABLE_PDF_COMPRESSION')) {
298  $pdf->SetCompression(false);
299  }
300 
301  $pdf->SetMargins(0, 0);
302  $pdf->SetAutoPageBreak(false);
303 
304  $this->_Metric_Doc = $this->Tformat['metric'];
305  // Permet de commencer l'impression de l'etiquette desiree dans le cas ou la page a deja servie
306  $posX = 1;
307  $posY = 1;
308  if ($posX > 0) {
309  $posX--;
310  } else {
311  $posX = 0;
312  }
313  if ($posY > 0) {
314  $posY--;
315  } else {
316  $posY = 0;
317  }
318  $this->_COUNTX = $posX;
319  $this->_COUNTY = $posY;
320  $this->_Set_Format($pdf, $this->Tformat);
321 
322 
323  $pdf->Open();
324  $pdf->AddPage();
325 
326 
327  // Add each record
328  foreach ($arrayofrecords as $val) {
329  // imprime le texte specifique sur la carte
330  $this->Add_PDF_label($pdf, $val['textleft'], $val['textheader'], $val['textfooter'], $langs, $val['textright'], $val['photo']);
331  }
332 
333  //$pdf->SetXY(10, 295);
334  //$pdf->Cell($this->_Width, $this->_Line_Height, 'XXX',0,1,'C');
335 
336 
337  // Output to file
338  $pdf->Output($file, 'F');
339 
340  dolChmod($file);
341 
342  $this->result = array('fullpath'=>$file);
343 
344  return 1;
345  }
346 }
pdf_getInstance
pdf_getInstance($format='', $metric='mm', $pagetype='P')
Return a PDF instance object.
Definition: pdf.lib.php:126
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4994
pdf_getPDFFont
pdf_getPDFFont($outputlangs)
Return font name to use for PDF generation.
Definition: pdf.lib.php:265
dol_getImageSize
dol_getImageSize($file, $url=false)
Return size of image file on disk (Supported extensions are gif, jpg, png, bmp and webp)
Definition: images.lib.php:118
CommonStickerGenerator
Class to generate stick sheet with format Avery or other personalised.
Definition: commonstickergenerator.class.php:62
dolChmod
dolChmod($filepath, $newmask='')
Change mod of a file.
Definition: functions.lib.php:6882
pdf_standardlabel\Add_PDF_label
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)
Definition: pdf_standardlabel.class.php:64
pdf_standardlabel
Class to generate stick sheet with format Avery or other personalised.
Definition: pdf_standardlabel.class.php:34
CommonStickerGenerator\_Set_Format
_Set_Format(&$pdf, $format)
protected Set format
Definition: commonstickergenerator.class.php:324
code
print *****$script_file(".$version.") pid code
! Closing after partial payment: discount_vat, badcustomer or badsupplier, bankcharge,...
Definition: sync_members_ldap2dolibarr.php:60
pdf_standardlabel\write_file
write_file($arrayofrecords, $outputlangs, $srctemplatepath, $outputdir='', $filename='tmp_address_sheet.pdf')
Function to build PDF on disk, then output on HTTP strem.
Definition: pdf_standardlabel.class.php:239
getDolGlobalString
if(!function_exists('utf8_encode')) if(!function_exists('utf8_decode')) getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
Definition: functions.lib.php:82
pdf_standardlabel\addSticker
addSticker(&$pdf, $outputlangs, $param)
Output a sticker on page at position _COUNTX, _COUNTY (_COUNTX and _COUNTY start from 0)
Definition: pdf_standardlabel.class.php:44
CommonStickerGenerator\_Croix
_Croix(&$pdf, $x1=0, $y1=0, $x2=210, $y2=297, $epaisseur=1, $taille=4)
protected Function realisant une croix aux 4 coins des cartes
Definition: commonstickergenerator.class.php:250
dol_mkdir
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
Definition: functions.lib.php:6811
type
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:119