dolibarr 21.0.0-alpha
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
29require_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 $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
208 $pdf->MultiCell(round($this->_Width / 2), $this->_Line_Height, $outputlangs->convToOutputCharset($textleft), 0, 'L');
209 $pdf->SetXY($_PosX + round($this->_Width / 2), $_PosY + $ytop);
210 $pdf->MultiCell(round($this->_Width / 2) - 2, $this->_Line_Height, $outputlangs->convToOutputCharset($textright), 0, 'R');
211 }
212 } else { // Only a right part
213 // Output right area
214 if ($textright == '%LOGO%' && $logo) {
215 $pdf->Image($logo, $_PosX + $this->_Width - $widthtouse - $xleft, $_PosY + $ytop, 0, $logoHeight);
216 } elseif ($code && !empty($encoding)) {
217 $this->writeBarcode($pdf, $code, $encoding, $is2d, $_PosX + $this->_Width - $widthtouse - $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
218 } else {
219 $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
220 $pdf->MultiCell($this->_Width - $xleft, $this->_Line_Height, $outputlangs->convToOutputCharset($textright), 0, 'R');
221 }
222 }
223
224 // Bottom
225 if ($footer != '') {
226 $pdf->SetXY($_PosX, $_PosY + $this->_Height - $this->_Line_Height - 1);
227 $pdf->Cell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($footer), 0, 1, 'C');
228 }
229 //print "$_PosY+$this->_Height-$this->_Line_Height-1<br>\n";
230
231 $this->_COUNTY++;
232
233 if ($this->_COUNTY == $this->_Y_Number) {
234 // Si on est en bas de page, on remonte le 'curseur' de position
235 $this->_COUNTX++;
236 $this->_COUNTY = 0;
237 }
238
239 if ($this->_COUNTX == $this->_X_Number) {
240 // Si on est en bout de page, alors on repart sur une nouvelle page
241 $this->_COUNTX = 0;
242 $this->_COUNTY = 0;
243 }
244 }
245
246
247
248
249 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
260 public function write_file($arrayofrecords, $outputlangs, $srctemplatepath, $outputdir = '', $filename = 'tmp_address_sheet.pdf')
261 {
262 // phpcs:enable
263 global $user, $conf, $langs, $mysoc, $_Avery_Labels;
264
265 $this->code = $srctemplatepath;
266 $this->Tformat = $_Avery_Labels[$this->code];
267 if (empty($this->Tformat)) {
268 dol_print_error(null, 'ErrorBadTypeForCard'.$this->code);
269 exit;
270 }
271 $this->type = 'pdf';
272 // standard format or custom
273 if ($this->Tformat['paper-size'] != 'custom') {
274 $this->format = $this->Tformat['paper-size'];
275 } else {
276 //custom
277 $resolution = array($this->Tformat['custom_x'], $this->Tformat['custom_y']);
278 $this->format = $resolution;
279 }
280
281 if (!is_object($outputlangs)) {
282 $outputlangs = $langs;
283 }
284 // For backward compatibility with FPDF, force output charset to ISO, because FPDF expect text to be encoded in ISO
285 if (getDolGlobalString('MAIN_USE_FPDF')) {
286 $outputlangs->charset_output = 'ISO-8859-1';
287 }
288
289 // Load traductions files required by page
290 $outputlangs->loadLangs(array("main", "dict", "companies", "admin"));
291
292 $title = $outputlangs->transnoentities('Labels');
293 $keywords = $title." ".$outputlangs->convToOutputCharset($mysoc->name);
294
295 $dir = (empty($outputdir) ? $conf->adherent->dir_temp : $outputdir);
296 $file = $dir."/".$filename;
297
298 if (!file_exists($dir)) {
299 if (dol_mkdir($dir) < 0) {
300 $this->error = $langs->trans("ErrorCanNotCreateDir", $dir);
301 return 0;
302 }
303 }
304
305 $pdf = pdf_getInstance($this->format, $this->Tformat['metric'], $this->Tformat['orientation']);
306
307 if (class_exists('TCPDF')) {
308 $pdf->setPrintHeader(false);
309 $pdf->setPrintFooter(false);
310 }
311 $pdf->SetFont(pdf_getPDFFont($outputlangs));
312
313 $pdf->SetTitle($title);
314 $pdf->SetSubject($title);
315 $pdf->SetCreator("Dolibarr ".DOL_VERSION);
316 $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
317 $pdf->SetKeyWords($keywords);
318 if (getDolGlobalString('MAIN_DISABLE_PDF_COMPRESSION')) {
319 $pdf->SetCompression(false);
320 }
321
322 $pdf->SetMargins(0, 0);
323 $pdf->SetAutoPageBreak(false);
324
325 $this->_Metric_Doc = $this->Tformat['metric'];
326 // Permet de commencer l'impression de l'etiquette desiree dans le cas ou la page a deja service
327 $posX = 1;
328 $posY = 1;
329 if ($posX > 0) {
330 $posX--;
331 } else {
332 $posX = 0;
333 }
334 if ($posY > 0) {
335 $posY--;
336 } else {
337 $posY = 0;
338 }
339 $this->_COUNTX = $posX;
340 $this->_COUNTY = $posY;
341 $this->_Set_Format($pdf, $this->Tformat);
342
343
344 $pdf->Open();
345 $pdf->AddPage();
346
347
348 // Add each record
349 foreach ($arrayofrecords as $val) {
350 // imprime le texte specifique sur la carte
351 $this->addSticker($pdf, $outputlangs, $val);
352 }
353
354 //$pdf->SetXY(10, 295);
355 //$pdf->Cell($this->_Width, $this->_Line_Height, 'XXX',0,1,'C');
356
357
358 // Output to file
359 $pdf->Output($file, 'F');
360
361 dolChmod($file);
362
363 $this->result = array('fullpath'=>$file);
364
365 return 1;
366 }
367}
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 stream.
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)
dolChmod($filepath, $newmask='')
Change mod of a file.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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:267
pdf_getInstance($format='', $metric='mm', $pagetype='P')
Return a PDF instance object.
Definition pdf.lib.php:128
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition repair.php:139