dolibarr  17.0.4
pdf_tcpdflabel.class.php
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  // define 1d barcode style
37  private $_style1d = array(
38  'position' => '',
39  'align' => 'C',
40  'stretch' => false,
41  'fitwidth' => true,
42  'cellfitalign' => '',
43  'border' => false,
44  'hpadding' => 'auto',
45  'vpadding' => 'auto',
46  'fgcolor' => array(0, 0, 0),
47  'bgcolor' => false,
48  'text' => true,
49  'font' => 'helvetica',
50  'fontsize' => 8,
51  'stretchtext' => 4
52  );
53 
54  // set style for 2d barcode
55  private $_style2d = array(
56  'border' => false,
57  'vpadding' => 'auto',
58  'hpadding' => 'auto',
59  'fgcolor' => array(0, 0, 0),
60  'bgcolor' => false,
61  'module_width' => 1, // width of a single module in points
62  'module_height' => 1 // height of a single module in points
63  );
64 
65  private $_align2d = 'N';
66 
67  private $_xres = 0.4;
68 
82  private function writeBarcode(&$pdf, $code, $encoding, $is2d, $x, $y, $w, $h)
83  {
84  if ($is2d) {
85  $pdf->write2DBarcode($code, $encoding, $x, $y, $w, $h, $this->_style2d, $this->_align2d);
86  } else {
87  $pdf->write1DBarcode($code, $encoding, $x, $y, $w, $h, $this->_xres, $this->_style1d);
88  }
89  }
90 
99  public function addSticker(&$pdf, $outputlangs, $param)
100  {
101  global $mysoc, $conf;
102 
103  $textleft = $param['textleft'];
104  $header = $param['textheader'];
105  $footer = $param['textfooter'];
106  $textright = $param['textright'];
107  $code = $param['code'];
108  $encoding = $param['encoding'];
109  $is2d = $param['is2d'];
110 
111 
112 
113  // We are in a new page, then we must add a page
114  if (($this->_COUNTX == 0) && ($this->_COUNTY == 0) and (!$this->_First == 1)) {
115  $pdf->AddPage();
116  }
117  $this->_First = 0;
118  $_PosX = $this->_Margin_Left + ($this->_COUNTX * ($this->_Width + $this->_X_Space));
119  $_PosY = $this->_Margin_Top + ($this->_COUNTY * ($this->_Height + $this->_Y_Space));
120 
121  // Define logo
122  $logo = $conf->mycompany->dir_output.'/logos/'.$mysoc->logo;
123  if (!is_readable($logo)) {
124  $logo = '';
125  if (!empty($mysoc->logo_small) && is_readable($conf->mycompany->dir_output.'/logos/thumbs/'.$mysoc->logo_small)) {
126  $logo = $conf->mycompany->dir_output.'/logos/thumbs/'.$mysoc->logo_small;
127  } elseif (!empty($mysoc->logo) && is_readable($conf->mycompany->dir_output.'/logos/'.$mysoc->logo)) {
128  $logo = $conf->mycompany->dir_output.'/logos/'.$mysoc->logo;
129  }
130  }
131 
132  $xleft = 2;
133  $ytop = 2;
134 
135  // Top
136  if ($header != '') {
137  $pdf->SetXY($_PosX + $xleft, $_PosY + 1); // Only 1 mm and not ytop for top text
138  $pdf->Cell(2 * strlen($header), $this->_Line_Height, $outputlangs->convToOutputCharset($header), 0, 1, 'C');
139  }
140 
141  $ytop += (empty($header) ? 0 : (1 + $this->_Line_Height));
142 
143  // Define widthtouse and heighttouse
144  $pageMargins = $pdf->getMargins();
145  $maxwidthtouse = round($this->_Width - 2 * $xleft);
146  $maxheighttouse = round($this->_Height - 2 * $ytop);
147  $maxheighttouse -= (empty($footer) ? 0 : (1 + $this->_Line_Height));
148  $defaultratio = ($maxwidthtouse / $maxheighttouse);
149  $widthtouse = $maxwidthtouse;
150  $heighttouse = $maxheighttouse;
151  $logoHeight = $heighttouse;
152  $logoWidth = $widthtouse;
153 
154  //var_dump($this->_Width.'x'.$this->_Height.' with border and scale '.$imgscale.' => max '.$maxwidthtouse.'x'.$maxheighttouse.' => We use '.$widthtouse.'x'.$heighttouse);exit;
155 
156  // Center
157  if ($textright == '') { // Only a left part
158  // Output left area
159  if ($textleft == '%LOGO%' && $logo) {
160  $pdf->Image($logo, $_PosX + $xleft, $_PosY + $ytop, 0, $logoHeight);
161  } elseif ($code && !empty($encoding)) {
162  $this->writeBarcode($pdf, $code, $encoding, $is2d, $_PosX + $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
163  } else {
164  $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
165  $pdf->MultiCell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft), 0, 'L');
166  }
167  } elseif ($textleft != '' && $textright != '') { // left and right part
168  $logoHeight = $heighttouse / 2;
169  $logoWidth = $widthtouse / 2;
170  if (($textleft == '%LOGO%' || $textleft == '%PHOTO%' || $textleft == '%BARCODE%') && !strstr($textright, '%')) { // left part logo/barcode right part text
171  if ($textleft == '%LOGO%' && $logo) {
172  $pdf->Image($logo, $_PosX + $xleft, $_PosY + $ytop, $logoWidth, 0);
173  } elseif ($code && !empty($encoding)) {
174  $this->writeBarcode($pdf, $code, $encoding, $is2d, $_PosX + $xleft, $_PosY + $ytop, $widthtouse / 2, $heighttouse);
175  }
176  $pdf->SetXY($_PosX + ($widthtouse / 2), $_PosY + $ytop);
177  $pdf->MultiCell($widthtouse / 2, $this->_Line_Height, $outputlangs->convToOutputCharset($textright), 0, 'R');
178  } elseif (($textright == '%LOGO%' || $textright == '%PHOTO%' || $textright == '%BARCODE%') && !strstr($textleft, '%')) { // right part logo/barcode left part text
179  if ($textright == '%LOGO%' && $logo) {
180  $pdf->Image($logo, $_PosX + ($widthtouse / 2), $_PosY + $ytop, $logoWidth, 0);
181  } elseif ($code && !empty($encoding)) {
182  $this->writeBarcode($pdf, $code, $encoding, $is2d, $_PosX + ($widthtouse / 2), $_PosY + $ytop, $widthtouse / 2, $heighttouse);
183  }
184  $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
185  $pdf->MultiCell($widthtouse / 2, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft), 0, 'L');
186  } elseif ($textleft == '%LOGO%') { // left part logo right part text/barcode
187  if ($logo) {
188  $pdf->Image($logo, $_PosX + $xleft, $_PosY + $ytop, 0, $logoHeight);
189  }
190  if ($code && !empty($encoding)) {
191  $this->writeBarcode($pdf, $code, $encoding, $is2d, $_PosX + $xleft + $logoWidth + 1, $_PosY + $ytop, $widthtouse - $logoWidth - 1, $heighttouse);
192  } else {
193  $pdf->SetXY($_PosX + $xleft + $logoWidth + 1, $_PosY + $ytop);
194  $pdf->MultiCell($widthtouse - $logoWidth - 1, $this->_Line_Height, $outputlangs->convToOutputCharset($textright), 0, 'R');
195  }
196  } elseif ($textright == '%LOGO%') { // right part logo left part text/barcode
197  if ($logo) {
198  $pdf->Image($logo, $_PosX + $xleft + $widthtouse - $logoWidth + 1, $_PosY + $ytop, 0, $logoHeight);
199  }
200  if ($code && !empty($encoding)) {
201  $this->writeBarcode($pdf, $code, $encoding, $is2d, $_PosX + $xleft, $_PosY + $ytop, $widthtouse - $logoWidth - 1, $heighttouse);
202  } else {
203  $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
204  $pdf->MultiCell($widthtouse - $logoWidth - 1, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft), 0, 'L');
205  }
206  } else // text on halft left and text on half right
207  {
208  $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
209  $pdf->MultiCell(round($this->_Width / 2), $this->_Line_Height, $outputlangs->convToOutputCharset($textleft), 0, 'L');
210  $pdf->SetXY($_PosX + round($this->_Width / 2), $_PosY + $ytop);
211  $pdf->MultiCell(round($this->_Width / 2) - 2, $this->_Line_Height, $outputlangs->convToOutputCharset($textright), 0, 'R');
212  }
213  } else // Only a right part
214  {
215  // Output right area
216  if ($textright == '%LOGO%' && $logo) {
217  $pdf->Image($logo, $_PosX + $this->_Width - $widthtouse - $xleft, $_PosY + $ytop, 0, $logoHeight);
218  } elseif ($code && !empty($encoding)) {
219  $this->writeBarcode($pdf, $code, $encoding, $is2d, $_PosX + $this->_Width - $widthtouse - $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
220  } else {
221  $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
222  $pdf->MultiCell($this->_Width - $xleft, $this->_Line_Height, $outputlangs->convToOutputCharset($textright), 0, 'R');
223  }
224  }
225 
226  // Bottom
227  if ($footer != '') {
228  $pdf->SetXY($_PosX, $_PosY + $this->_Height - $this->_Line_Height - 1);
229  $pdf->Cell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($footer), 0, 1, 'C');
230  }
231  //print "$_PosY+$this->_Height-$this->_Line_Height-1<br>\n";
232 
233  $this->_COUNTY++;
234 
235  if ($this->_COUNTY == $this->_Y_Number) {
236  // Si on est en bas de page, on remonte le 'curseur' de position
237  $this->_COUNTX++;
238  $this->_COUNTY = 0;
239  }
240 
241  if ($this->_COUNTX == $this->_X_Number) {
242  // Si on est en bout de page, alors on repart sur une nouvelle page
243  $this->_COUNTX = 0;
244  $this->_COUNTY = 0;
245  }
246  }
247 
248 
249 
250 
251  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
262  public function write_file($arrayofrecords, $outputlangs, $srctemplatepath, $outputdir = '', $filename = 'tmp_address_sheet.pdf')
263  {
264  // phpcs:enable
265  global $user, $conf, $langs, $mysoc, $_Avery_Labels;
266 
267  $this->code = $srctemplatepath;
268  $this->Tformat = $_Avery_Labels[$this->code];
269  if (empty($this->Tformat)) {
270  dol_print_error('', 'ErrorBadTypeForCard'.$this->code);
271  exit;
272  }
273  $this->type = 'pdf';
274  // standard format or custom
275  if ($this->Tformat['paper-size'] != 'custom') {
276  $this->format = $this->Tformat['paper-size'];
277  } else {
278  //custom
279  $resolution = array($this->Tformat['custom_x'], $this->Tformat['custom_y']);
280  $this->format = $resolution;
281  }
282 
283  if (!is_object($outputlangs)) {
284  $outputlangs = $langs;
285  }
286  // For backward compatibility with FPDF, force output charset to ISO, because FPDF expect text to be encoded in ISO
287  if (!empty($conf->global->MAIN_USE_FPDF)) {
288  $outputlangs->charset_output = 'ISO-8859-1';
289  }
290 
291  // Load traductions files required by page
292  $outputlangs->loadLangs(array("main", "dict", "companies", "admin"));
293 
294  $title = $outputlangs->transnoentities('Labels');
295  $keywords = $title." ".$outputlangs->convToOutputCharset($mysoc->name);
296 
297  $dir = (empty($outputdir) ? $conf->adherent->dir_temp : $outputdir);
298  $file = $dir."/".$filename;
299 
300  if (!file_exists($dir)) {
301  if (dol_mkdir($dir) < 0) {
302  $this->error = $langs->trans("ErrorCanNotCreateDir", $dir);
303  return 0;
304  }
305  }
306 
307  $pdf = pdf_getInstance($this->format, $this->Tformat['metric'], $this->Tformat['orientation']);
308 
309  if (class_exists('TCPDF')) {
310  $pdf->setPrintHeader(false);
311  $pdf->setPrintFooter(false);
312  }
313  $pdf->SetFont(pdf_getPDFFont($outputlangs));
314 
315  $pdf->SetTitle($title);
316  $pdf->SetSubject($title);
317  $pdf->SetCreator("Dolibarr ".DOL_VERSION);
318  $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
319  $pdf->SetKeyWords($keywords);
320  if (getDolGlobalString('MAIN_DISABLE_PDF_COMPRESSION')) {
321  $pdf->SetCompression(false);
322  }
323 
324  $pdf->SetMargins(0, 0);
325  $pdf->SetAutoPageBreak(false);
326 
327  $this->_Metric_Doc = $this->Tformat['metric'];
328  // Permet de commencer l'impression de l'etiquette desiree dans le cas ou la page a deja servie
329  $posX = 1;
330  $posY = 1;
331  if ($posX > 0) {
332  $posX--;
333  } else {
334  $posX = 0;
335  }
336  if ($posY > 0) {
337  $posY--;
338  } else {
339  $posY = 0;
340  }
341  $this->_COUNTX = $posX;
342  $this->_COUNTY = $posY;
343  $this->_Set_Format($pdf, $this->Tformat);
344 
345 
346  $pdf->Open();
347  $pdf->AddPage();
348 
349 
350  // Add each record
351  foreach ($arrayofrecords as $val) {
352  // imprime le texte specifique sur la carte
353  $this->addSticker($pdf, $outputlangs, $val);
354  }
355 
356  //$pdf->SetXY(10, 295);
357  //$pdf->Cell($this->_Width, $this->_Line_Height, 'XXX',0,1,'C');
358 
359 
360  // Output to file
361  $pdf->Output($file, 'F');
362 
363  if (!empty($conf->global->MAIN_UMASK)) {
364  @chmod($file, octdec($conf->global->MAIN_UMASK));
365  }
366 
367 
368  $this->result = array('fullpath'=>$file);
369 
370  return 1;
371  }
372 }
Class to generate stick sheet with format Avery or other personalised.
_Set_Format(&$pdf, $format)
protected Set format
Class to generate stick sheet with format Avery or other personalised.
write_file($arrayofrecords, $outputlangs, $srctemplatepath, $outputdir='', $filename='tmp_address_sheet.pdf')
Function to build PDF on disk, then output on HTTP strem.
writeBarcode(&$pdf, $code, $encoding, $is2d, $x, $y, $w, $h)
write barcode to pdf
addSticker(&$pdf, $outputlangs, $param)
Output a sticker on page at position _COUNTX, _COUNTY (_COUNTX and _COUNTY start from 0)
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
if(!function_exists('utf8_encode')) if(!function_exists('utf8_decode')) getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
pdf_getPDFFont($outputlangs)
Return font name to use for PDF generation.
Definition: pdf.lib.php:265
pdf_getInstance($format='', $metric='mm', $pagetype='P')
Return a PDF instance object.
Definition: pdf.lib.php:126
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:119
print *****$script_file(".$version.") pid code
! Closing after partial payment: discount_vat, badcustomer or badsupplier, bankcharge,...