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 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 */
23
30require_once DOL_DOCUMENT_ROOT.'/core/class/commonstickergenerator.class.php';
31
36{
45 public function addSticker(&$pdf, $outputlangs, $param)
46 {
47 // use this method in future refactoring
48 }
49
50 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
65 public function Add_PDF_label(&$pdf, $textleft, $header, $footer, $outputlangs, $textright = '', $photo = '')
66 {
67 // phpcs:enable
68 global $mysoc, $conf, $langs;
69 global $forceimgscalewidth, $forceimgscaleheight;
70
71 $imgscalewidth = (empty($forceimgscalewidth) ? 0.3 : $forceimgscalewidth); // Scale of image for width (1=Full width of sticker)
72 $imgscaleheight = (empty($forceimgscaleheight) ? 0.5 : $forceimgscaleheight); // Scale of image for height (1=Full height of sticker)
73
74 // We are in a new page, then we must add a page
75 if (($this->_COUNTX == 0) && ($this->_COUNTY == 0) and (!$this->_First == 1)) {
76 $pdf->AddPage();
77 }
78 $this->_First = 0;
79 $_PosX = $this->_Margin_Left + ($this->_COUNTX * ($this->_Width + $this->_X_Space));
80 $_PosY = $this->_Margin_Top + ($this->_COUNTY * ($this->_Height + $this->_Y_Space));
81
82 // Define logo
83 $logo = $conf->mycompany->dir_output.'/logos/'.$mysoc->logo;
84 if (!is_readable($logo)) {
85 $logo = '';
86 if (!empty($mysoc->logo_small) && is_readable($conf->mycompany->dir_output.'/logos/thumbs/'.$mysoc->logo_small)) {
87 $logo = $conf->mycompany->dir_output.'/logos/thumbs/'.$mysoc->logo_small;
88 } elseif (!empty($mysoc->logo) && is_readable($conf->mycompany->dir_output.'/logos/'.$mysoc->logo)) {
89 $logo = $conf->mycompany->dir_output.'/logos/'.$mysoc->logo;
90 }
91 }
92
93 // Define photo
94 if (!empty($photo)) {
95 if (!is_readable($photo)) {
96 $photo = '';
97 }
98 }
99
100 // Define background image
101 $backgroundimage = '';
102
103 // Print lines
104 if ($this->code == "CARD") {
105 $this->Tformat = $this->_Avery_Labels["CARD"];
106 //$this->_Pointille($pdf,$_PosX,$_PosY,$_PosX+$this->_Width,$_PosY+$this->_Height,0.3,25);
107 $this->_Croix($pdf, $_PosX, $_PosY, $_PosX + $this->_Width, $_PosY + $this->_Height, 0.1, 10);
108 }
109
110 // Background
111 if ($backgroundimage) {
112 $pdf->image($backgroundimage, $_PosX, $_PosY, $this->_Width, $this->_Height);
113 }
114
115 $xleft = 2;
116 $ytop = 2;
117
118 // Top
119 if ($header != '') {
120 if ($this->code == "CARD") {
121 $pdf->SetDrawColor(128, 128, 128);
122 $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
123 $pdf->SetDrawColor(0, 0, 0);
124 }
125 $pdf->SetXY($_PosX + $xleft, $_PosY + 1); // Only 1 mm and not ytop for top text
126 $pdf->Cell($this->_Width - 2 * $xleft, $this->_Line_Height, $outputlangs->convToOutputCharset($header), 0, 1, 'C');
127 }
128
129
130 $ytop += (empty($header) ? 0 : (1 + $this->_Line_Height));
131
132 // Define widthtouse and heighttouse
133 $maxwidthtouse = round(($this->_Width - 2 * $xleft) * $imgscalewidth);
134 $maxheighttouse = round(($this->_Height - 2 * $ytop) * $imgscaleheight);
135 $defaultratio = ($maxwidthtouse / $maxheighttouse);
136 $widthtouse = $maxwidthtouse;
137 $heighttouse = 0; // old value for image
138 $tmp = dol_getImageSize($photo, false);
139 if (!empty($tmp['height'])) {
140 $imgratio = $tmp['width'] / $tmp['height'];
141 if ($imgratio >= $defaultratio) {
142 $widthtouse = $maxwidthtouse;
143 $heighttouse = round($widthtouse / $imgratio);
144 } else {
145 $heighttouse = $maxheighttouse;
146 $widthtouse = round($heighttouse * $imgratio);
147 }
148 }
149 //var_dump($this->_Width.'x'.$this->_Height.' with border and scale '.$imgscale.' => max '.$maxwidthtouse.'x'.$maxheighttouse.' => We use '.$widthtouse.'x'.$heighttouse);exit;
150
151 // Center
152 if ($textright == '') { // Only a left part
153 // Output left area
154 if ($textleft == '%LOGO%' && $logo) {
155 $pdf->Image($logo, $_PosX + $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
156 } elseif ($textleft == '%PHOTO%' && $photo) {
157 $pdf->Image($photo, $_PosX + $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
158 } else {
159 $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
160 $pdf->MultiCell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft), 0, 'L');
161 }
162 } elseif ($textleft != '' && $textright != '') { //
163 if ($textleft == '%LOGO%' || $textleft == '%PHOTO%') {
164 if ($textleft == '%LOGO%' && $logo) {
165 $pdf->Image($logo, $_PosX + $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
166 } elseif ($textleft == '%PHOTO%' && $photo) {
167 $pdf->Image($photo, $_PosX + $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
168 }
169 $pdf->SetXY($_PosX + $xleft + $widthtouse + 1, $_PosY + $ytop);
170 $pdf->MultiCell($this->_Width - $xleft - $xleft - $widthtouse - 1, $this->_Line_Height, $outputlangs->convToOutputCharset($textright), 0, 'R');
171 } elseif ($textright == '%LOGO%' || $textright == '%PHOTO%') {
172 if ($textright == '%LOGO%' && $logo) {
173 $pdf->Image($logo, $_PosX + $this->_Width - $widthtouse - $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
174 } elseif ($textright == '%PHOTO%' && $photo) {
175 $pdf->Image($photo, $_PosX + $this->_Width - $widthtouse - $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
176 }
177 $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
178 $pdf->MultiCell($this->_Width - $widthtouse - $xleft - $xleft - 1, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft), 0, 'L');
179 } else { // text on halft left and text on half right
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 // Output right area
187 if ($textright == '%LOGO%' && $logo) {
188 $pdf->Image($logo, $_PosX + $this->_Width - $widthtouse - $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
189 } elseif ($textright == '%PHOTO%' && $photo) {
190 $pdf->Image($photo, $_PosX + $this->_Width - $widthtouse - $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
191 } else {
192 $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
193 $pdf->MultiCell($this->_Width - $xleft, $this->_Line_Height, $outputlangs->convToOutputCharset($textright), 0, 'R');
194 }
195 }
196
197 // Bottom
198 if ($footer != '') {
199 if ($this->code == "CARD") {
200 $pdf->SetDrawColor(128, 128, 128);
201 $pdf->Line($_PosX, $_PosY + $this->_Height - $this->_Line_Height - 2, $_PosX + $this->_Width, $_PosY + $this->_Height - $this->_Line_Height - 2);
202 $pdf->SetDrawColor(0, 0, 0);
203 }
204 $pdf->SetXY($_PosX, $_PosY + $this->_Height - $this->_Line_Height - 1);
205 $pdf->Cell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($footer), 0, 1, 'C');
206 }
207 //print "$_PosY+$this->_Height-$this->_Line_Height-1<br>\n";
208
209 $this->_COUNTY++;
210
211 if ($this->_COUNTY == $this->_Y_Number) {
212 // Si on est en bas de page, on remonte le 'curseur' de position
213 $this->_COUNTX++;
214 $this->_COUNTY = 0;
215 }
216
217 if ($this->_COUNTX == $this->_X_Number) {
218 // Si on est en bout de page, alors on repart sur une nouvelle page
219 $this->_COUNTX = 0;
220 $this->_COUNTY = 0;
221 }
222 }
223
224
225
226
227 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
238 public function write_file($arrayofrecords, $outputlangs, $srctemplatepath, $outputdir = '', $filename = 'tmp_address_sheet.pdf')
239 {
240 // phpcs:enable
241 global $user, $conf, $langs, $mysoc, $_Avery_Labels;
242
243 $this->code = $srctemplatepath;
244 $this->Tformat = $_Avery_Labels[$this->code];
245 if (empty($this->Tformat)) {
246 dol_print_error(null, 'ErrorBadTypeForCard'.$this->code);
247 exit;
248 }
249 $this->type = 'pdf';
250 // standard format or custom
251 $paper_size = $this->Tformat['paper-size'];
252 if (!is_string($paper_size) || $paper_size != 'custom') {
253 $this->format = $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 (getDolGlobalString('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 // Enable printing the label when the page was already started
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}
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 a 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:265
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:137