dolibarr 21.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
29require_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 $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
180 $pdf->MultiCell(round($this->_Width / 2), $this->_Line_Height, $outputlangs->convToOutputCharset($textleft), 0, 'L');
181 $pdf->SetXY($_PosX + round($this->_Width / 2), $_PosY + $ytop);
182 $pdf->MultiCell(round($this->_Width / 2) - 2, $this->_Line_Height, $outputlangs->convToOutputCharset($textright), 0, 'R');
183 }
184 } else { // Only a right part
185 // Output right area
186 if ($textright == '%LOGO%' && $logo) {
187 $pdf->Image($logo, $_PosX + $this->_Width - $widthtouse - $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
188 } elseif ($textright == '%PHOTO%' && $photo) {
189 $pdf->Image($photo, $_PosX + $this->_Width - $widthtouse - $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
190 } else {
191 $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
192 $pdf->MultiCell($this->_Width - $xleft, $this->_Line_Height, $outputlangs->convToOutputCharset($textright), 0, 'R');
193 }
194 }
195
196 // Bottom
197 if ($footer != '') {
198 if ($this->code == "CARD") {
199 $pdf->SetDrawColor(128, 128, 128);
200 $pdf->Line($_PosX, $_PosY + $this->_Height - $this->_Line_Height - 2, $_PosX + $this->_Width, $_PosY + $this->_Height - $this->_Line_Height - 2);
201 $pdf->SetDrawColor(0, 0, 0);
202 }
203 $pdf->SetXY($_PosX, $_PosY + $this->_Height - $this->_Line_Height - 1);
204 $pdf->Cell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($footer), 0, 1, 'C');
205 }
206 //print "$_PosY+$this->_Height-$this->_Line_Height-1<br>\n";
207
208 $this->_COUNTY++;
209
210 if ($this->_COUNTY == $this->_Y_Number) {
211 // Si on est en bas de page, on remonte le 'curseur' de position
212 $this->_COUNTX++;
213 $this->_COUNTY = 0;
214 }
215
216 if ($this->_COUNTX == $this->_X_Number) {
217 // Si on est en bout de page, alors on repart sur une nouvelle page
218 $this->_COUNTX = 0;
219 $this->_COUNTY = 0;
220 }
221 }
222
223
224
225
226 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
237 public function write_file($arrayofrecords, $outputlangs, $srctemplatepath, $outputdir = '', $filename = 'tmp_address_sheet.pdf')
238 {
239 // phpcs:enable
240 global $user, $conf, $langs, $mysoc, $_Avery_Labels;
241
242 $this->code = $srctemplatepath;
243 $this->Tformat = $_Avery_Labels[$this->code];
244 if (empty($this->Tformat)) {
245 dol_print_error(null, 'ErrorBadTypeForCard'.$this->code);
246 exit;
247 }
248 $this->type = 'pdf';
249 // standard format or custom
250 if ($this->Tformat['paper-size'] != 'custom') {
251 $this->format = $this->Tformat['paper-size'];
252 } else {
253 //custom
254 $resolution = array($this->Tformat['custom_x'], $this->Tformat['custom_y']);
255 $this->format = $resolution;
256 }
257
258 if (!is_object($outputlangs)) {
259 $outputlangs = $langs;
260 }
261 // For backward compatibility with FPDF, force output charset to ISO, because FPDF expect text to be encoded in ISO
262 if (getDolGlobalString('MAIN_USE_FPDF')) {
263 $outputlangs->charset_output = 'ISO-8859-1';
264 }
265
266 // Load traductions files required by page
267 $outputlangs->loadLangs(array("main", "dict", "companies", "admin"));
268
269 $title = $outputlangs->transnoentities('Labels');
270 $keywords = $title." ".$outputlangs->convToOutputCharset($mysoc->name);
271
272 $dir = (empty($outputdir) ? $conf->adherent->dir_temp : $outputdir);
273 $file = $dir."/".$filename;
274
275 if (!file_exists($dir)) {
276 if (dol_mkdir($dir) < 0) {
277 $this->error = $langs->trans("ErrorCanNotCreateDir", $dir);
278 return 0;
279 }
280 }
281
282 $pdf = pdf_getInstance($this->format, $this->Tformat['metric'], $this->Tformat['orientation']);
283
284 if (class_exists('TCPDF')) {
285 $pdf->setPrintHeader(false);
286 $pdf->setPrintFooter(false);
287 }
288 $pdf->SetFont(pdf_getPDFFont($outputlangs));
289
290 $pdf->SetTitle($title);
291 $pdf->SetSubject($title);
292 $pdf->SetCreator("Dolibarr ".DOL_VERSION);
293 $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
294 $pdf->SetKeyWords($keywords);
295 if (getDolGlobalString('MAIN_DISABLE_PDF_COMPRESSION')) {
296 $pdf->SetCompression(false);
297 }
298
299 $pdf->SetMargins(0, 0);
300 $pdf->SetAutoPageBreak(false);
301
302 $this->_Metric_Doc = $this->Tformat['metric'];
303 // Enable printing the label when the page was already started
304 $posX = 1;
305 $posY = 1;
306 if ($posX > 0) {
307 $posX--;
308 } else {
309 $posX = 0;
310 }
311 if ($posY > 0) {
312 $posY--;
313 } else {
314 $posY = 0;
315 }
316 $this->_COUNTX = $posX;
317 $this->_COUNTY = $posY;
318 $this->_Set_Format($pdf, $this->Tformat);
319
320
321 $pdf->Open();
322 $pdf->AddPage();
323
324
325 // Add each record
326 foreach ($arrayofrecords as $val) {
327 // imprime le texte specifique sur la carte
328 $this->Add_PDF_label($pdf, $val['textleft'], $val['textheader'], $val['textfooter'], $langs, $val['textright'], $val['photo']);
329 }
330
331 //$pdf->SetXY(10, 295);
332 //$pdf->Cell($this->_Width, $this->_Line_Height, 'XXX',0,1,'C');
333
334
335 // Output to file
336 $pdf->Output($file, 'F');
337
338 dolChmod($file);
339
340 $this->result = array('fullpath'=>$file);
341
342 return 1;
343 }
344}
Class to generate stick sheet with format Avery or other personalised.
_Croix(&$pdf, $x1=0, $y1=0, $x2=210, $y2=297, $epaisseur=1, $taille=4)
protected Function realisant une croix aux 4 coins des cartes
_Set_Format(&$pdf, $format)
protected Set format
Class to generate stick sheet with format Avery or other personalised.
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)
write_file($arrayofrecords, $outputlangs, $srctemplatepath, $outputdir='', $filename='tmp_address_sheet.pdf')
Function to build PDF on disk, then output on HTTP stream.
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)
dol_getImageSize($file, $url=false)
Return size of image file on disk (Supported extensions are gif, jpg, png, bmp and webp)
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