dolibarr 20.0.0
pdf_standard.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 Frédéric France <frederic.france@free.fr>
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{
41 public $version = 'dolibarr';
42
43
49 public function __construct($db)
50 {
51 global $conf, $langs, $mysoc;
52
53 // Translations
54 $langs->loadLangs(array("main", "admin"));
55
56 $this->db = $db;
57 $this->name = "standard";
58 $this->description = $langs->trans('TemplateforBusinessCards');
59 //$this->update_main_doc_field = 1; // Save the name of generated file as the main doc when generating a doc with this template
60
61 $this->type = 'pdf-various-sizes';
62 }
63
72 public function addSticker(&$pdf, $outputlangs, $param)
73 {
74 // use this method in future refactoring
75 }
76
77 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
93 public function Add_PDF_card(&$pdf, $textleft, $header, $footer, $outputlangs, $textright = '', $idmember = 0, $photo = '')
94 {
95 // phpcs:enable
96 global $db, $mysoc, $conf, $langs;
97 global $forceimgscalewidth, $forceimgscaleheight;
98
99 $imgscalewidth = (empty($forceimgscalewidth) ? 0.3 : $forceimgscalewidth); // Scale of image for width (1=Full width of sticker)
100 $imgscaleheight = (empty($forceimgscalewidth) ? 0.5 : $forceimgscalewidth); // Scale of image for height (1=Full height of sticker)
101
102 // We are in a new page, then we must add a page
103 if (($this->_COUNTX == 0) && ($this->_COUNTY == 0) and (!$this->_First == 1)) {
104 $pdf->AddPage();
105 }
106 $this->_First = 0;
107 $_PosX = $this->_Margin_Left + ($this->_COUNTX * ($this->_Width + $this->_X_Space));
108 $_PosY = $this->_Margin_Top + ($this->_COUNTY * ($this->_Height + $this->_Y_Space));
109
110 // Define logo
111 $logo = $conf->mycompany->dir_output.'/logos/'.$mysoc->logo;
112 if (!is_readable($logo)) {
113 $logo = '';
114 if (!empty($mysoc->logo_small) && is_readable($conf->mycompany->dir_output.'/logos/thumbs/'.$mysoc->logo_small)) {
115 $logo = $conf->mycompany->dir_output.'/logos/thumbs/'.$mysoc->logo_small;
116 } elseif (!empty($mysoc->logo) && is_readable($conf->mycompany->dir_output.'/logos/'.$mysoc->logo)) {
117 $logo = $conf->mycompany->dir_output.'/logos/'.$mysoc->logo;
118 }
119 }
120
121 $member = new Adherent($db);
122 $member->id = $idmember;
123 $member->ref = (string) $idmember;
124
125 // Define photo
126 $dir = $conf->adherent->dir_output;
127 if (!empty($photo)) {
128 $file = get_exdir(0, 0, 0, 0, $member, 'member').'photos/'.$photo;
129 $photo = $dir.'/'.$file;
130 if (!is_readable($photo)) {
131 $photo = '';
132 }
133 }
134
135 // Define background image
136 $backgroundimage = '';
137 if (getDolGlobalString('ADHERENT_CARD_BACKGROUND') && file_exists($conf->adherent->dir_output.'/' . getDolGlobalString('ADHERENT_CARD_BACKGROUND'))) {
138 $backgroundimage = $conf->adherent->dir_output.'/' . getDolGlobalString('ADHERENT_CARD_BACKGROUND');
139 }
140
141 // Print lines
142 if ($this->code == "CARD") {
143 $this->Tformat = $this->_Avery_Labels["CARD"];
144 //$this->_Pointille($pdf,$_PosX,$_PosY,$_PosX+$this->_Width,$_PosY+$this->_Height,0.3,25);
145 $this->_Croix($pdf, $_PosX, $_PosY, $_PosX + $this->_Width, $_PosY + $this->_Height, 0.1, 10);
146 }
147
148 // Background
149 if ($backgroundimage) {
150 $pdf->image($backgroundimage, $_PosX, $_PosY, $this->_Width, $this->_Height);
151 }
152
153 $xleft = 2;
154 $ytop = 2;
155
156 // Top
157 if ($header != '') {
158 if ($this->code == "CARD") {
159 $pdf->SetDrawColor(128, 128, 128);
160 $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
161 $pdf->SetDrawColor(0, 0, 0);
162 }
163 $pdf->SetXY($_PosX + $xleft, $_PosY + 1); // Only 1 mm and not ytop for top text
164 $pdf->Cell($this->_Width - 2 * $xleft, $this->_Line_Height, $outputlangs->convToOutputCharset($header), 0, 1, 'C');
165 }
166
167
168 $ytop += (empty($header) ? 0 : (1 + $this->_Line_Height));
169
170 // Define widthtouse and heighttouse
171 $maxwidthtouse = round(($this->_Width - 2 * $xleft) * $imgscalewidth);
172 $maxheighttouse = round(($this->_Height - 2 * $ytop) * $imgscaleheight);
173 $defaultratio = ($maxwidthtouse / $maxheighttouse);
174 $widthtouse = $maxwidthtouse;
175 $heighttouse = 0; // old value for image
176 $tmp = dol_getImageSize($photo, false);
177 if (isset($tmp['height'])) {
178 $imgratio = $tmp['width'] / $tmp['height'];
179 if ($imgratio >= $defaultratio) {
180 $widthtouse = $maxwidthtouse;
181 $heighttouse = round($widthtouse / $imgratio);
182 } else {
183 $heighttouse = $maxheighttouse;
184 $widthtouse = round($heighttouse * $imgratio);
185 }
186 }
187 //var_dump($this->_Width.'x'.$this->_Height.' with border and scale '.$imgscale.' => max '.$maxwidthtouse.'x'.$maxheighttouse.' => We use '.$widthtouse.'x'.$heighttouse);exit;
188
189 // Center
190 if ($textright == '') { // Only a left part
191 // Output left area
192 if ($textleft == '__LOGO__' && $logo) {
193 $pdf->Image($logo, $_PosX + $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
194 } elseif ($textleft == '__PHOTO__' && $photo) {
195 $pdf->Image($photo, $_PosX + $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
196 } else {
197 $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
198 $pdf->MultiCell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft), 0, 'L');
199 }
200 } elseif ($textleft != '' && $textright != '') { //
201 if ($textleft == '__LOGO__' || $textleft == '__PHOTO__') {
202 if ($textleft == '__LOGO__' && $logo) {
203 $pdf->Image($logo, $_PosX + $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
204 } elseif ($textleft == '__PHOTO__' && $photo) {
205 $pdf->Image($photo, $_PosX + $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
206 }
207 $pdf->SetXY($_PosX + $xleft + $widthtouse + 1, $_PosY + $ytop);
208 $pdf->MultiCell($this->_Width - $xleft - $xleft - $widthtouse - 1, $this->_Line_Height, $outputlangs->convToOutputCharset($textright), 0, 'R');
209 } elseif ($textright == '__LOGO__' || $textright == '__PHOTO__') {
210 if ($textright == '__LOGO__' && $logo) {
211 $pdf->Image($logo, $_PosX + $this->_Width - $widthtouse - $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
212 } elseif ($textright == '__PHOTO__' && $photo) {
213 $pdf->Image($photo, $_PosX + $this->_Width - $widthtouse - $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
214 }
215 $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
216 $pdf->MultiCell($this->_Width - $widthtouse - $xleft - $xleft - 1, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft), 0, 'L');
217 } else { // text on halft left and text on half right
218 $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
219 $pdf->MultiCell(round($this->_Width / 2), $this->_Line_Height, $outputlangs->convToOutputCharset($textleft), 0, 'L');
220 $pdf->SetXY($_PosX + round($this->_Width / 2), $_PosY + $ytop);
221 $pdf->MultiCell(round($this->_Width / 2) - 2, $this->_Line_Height, $outputlangs->convToOutputCharset($textright), 0, 'R');
222 }
223 } else { // Only a right part
224 // Output right area
225 if ($textright == '__LOGO__' && $logo) {
226 $pdf->Image($logo, $_PosX + $this->_Width - $widthtouse - $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
227 } elseif ($textright == '__PHOTO__' && $photo) {
228 $pdf->Image($photo, $_PosX + $this->_Width - $widthtouse - $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
229 } else {
230 $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
231 $pdf->MultiCell($this->_Width - $xleft, $this->_Line_Height, $outputlangs->convToOutputCharset($textright), 0, 'R');
232 }
233 }
234
235 // Bottom
236 if ($footer != '') {
237 if ($this->code == "CARD") {
238 $pdf->SetDrawColor(128, 128, 128);
239 $pdf->Line($_PosX, $_PosY + $this->_Height - $this->_Line_Height - 2, $_PosX + $this->_Width, $_PosY + $this->_Height - $this->_Line_Height - 2);
240 $pdf->SetDrawColor(0, 0, 0);
241 }
242 $pdf->SetXY($_PosX, $_PosY + $this->_Height - $this->_Line_Height - 1);
243 $pdf->Cell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($footer), 0, 1, 'C');
244 }
245 //print "$_PosY+$this->_Height-$this->_Line_Height-1<br>\n";
246
247 $this->_COUNTY++;
248
249 if ($this->_COUNTY == $this->_Y_Number) {
250 // Si on est en bas de page, on remonte le 'curseur' de position
251 $this->_COUNTX++;
252 $this->_COUNTY = 0;
253 }
254
255 if ($this->_COUNTX == $this->_X_Number) {
256 // Si on est en bout de page, alors on repart sur une nouvelle page
257 $this->_COUNTX = 0;
258 $this->_COUNTY = 0;
259 }
260 }
261
262 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
274 public function write_file($object, $outputlangs, $srctemplatepath, $mode = 'member', $nooutput = 0, $filename = 'tmp_cards')
275 {
276 // phpcs:enable
277 global $user, $conf, $langs, $mysoc, $_Avery_Labels;
278
279 $this->code = $srctemplatepath;
280
281 if (is_object($object)) {
282 if ($object->country == '-') {
283 $object->country = '';
284 }
285
286 $now = dol_now();
287 $year = dol_print_date($now, '%Y');
288 $month = dol_print_date($now, '%m');
289 $day = dol_print_date($now, '%d');
290
291 // List of values to scan for a replacement
292 $substitutionarray = array(
293 '__ID__' => $object->id,
294 '__REF__' => $object->ref,
295 '__LOGIN__' => empty($object->login) ? '' : $object->login,
296 '__FIRSTNAME__' => empty($object->firstname) ? '' : $object->firstname,
297 '__LASTNAME__' => empty($object->lastname) ? '' : $object->lastname,
298 '__FULLNAME__' => $object->getFullName($langs),
299 '__COMPANY__' => empty($object->company) ? '' : $object->company,
300 '__ADDRESS__' => empty($object->address) ? '' : $object->address,
301 '__ZIP__' => empty($object->zip) ? '' : $object->zip,
302 '__TOWN__' => empty($object->town) ? '' : $object->town,
303 '__COUNTRY__' => empty($object->country) ? '' : $object->country,
304 '__COUNTRY_CODE__' => empty($object->country_code) ? '' : $object->country_code,
305 '__EMAIL__' => empty($object->email) ? '' : $object->email,
306 '__BIRTH__' => dol_print_date($object->birth, 'day'),
307 '__TYPE__' => empty($object->type) ? '' : $object->type,
308 '__YEAR__' => $year,
309 '__MONTH__' => $month,
310 '__DAY__' => $day,
311 '__DOL_MAIN_URL_ROOT__' => DOL_MAIN_URL_ROOT,
312 '__SERVER__' => "https://".$_SERVER["SERVER_NAME"]."/"
313 );
314 complete_substitutions_array($substitutionarray, $langs);
315
316 // For business cards
317 $textleft = make_substitutions(getDolGlobalString("ADHERENT_CARD_TEXT"), $substitutionarray);
318 $textheader = make_substitutions(getDolGlobalString("ADHERENT_CARD_HEADER_TEXT"), $substitutionarray);
319 $textfooter = make_substitutions(getDolGlobalString("ADHERENT_CARD_FOOTER_TEXT"), $substitutionarray);
320 $textright = make_substitutions(getDolGlobalString("ADHERENT_CARD_TEXT_RIGHT"), $substitutionarray);
321
322 $nb = $_Avery_Labels[$this->code]['NX'] * $_Avery_Labels[$this->code]['NY'];
323 if ($nb <= 0) {
324 $nb = 1; // Protection to avoid empty page
325 }
326
327 $arrayofmembers = array();
328 for ($j = 0; $j < $nb; $j++) {
329 $arrayofmembers[] = array(
330 'textleft' => $textleft,
331 'textheader' => $textheader,
332 'textfooter' => $textfooter,
333 'textright' => $textright,
334 'id' => (isset($object->id) ? $object->id : ""),
335 'photo' => (isset($object->photo) ? $object->photo : "")
336 );
337 }
338
339 $arrayofrecords = $arrayofmembers;
340 } else {
341 // Old usage
342 $arrayofrecords = $object;
343 }
344
345 $this->Tformat = $_Avery_Labels[$this->code];
346 if (empty($this->Tformat)) {
347 dol_print_error(null, 'ErrorBadTypeForCard'.$this->code);
348 exit;
349 }
350
351 $this->type = 'pdf';
352 $filename .= '.pdf';
353
354 // standard format or custom
355 if ($this->Tformat['paper-size'] != 'custom') {
356 $this->format = $this->Tformat['paper-size'];
357 } else {
358 //custom
359 $resolution = array($this->Tformat['custom_x'], $this->Tformat['custom_y']);
360 $this->format = $resolution;
361 }
362
363 if (!is_object($outputlangs)) {
364 $outputlangs = $langs;
365 }
366 // For backward compatibility with FPDF, force output charset to ISO, because FPDF expect text to be encoded in ISO
367 if (getDolGlobalString('MAIN_USE_FPDF')) {
368 $outputlangs->charset_output = 'ISO-8859-1';
369 }
370
371 // Load traductions files required by page
372 $outputlangs->loadLangs(array("main", "dict", "companies", "admin", "members"));
373
374 if (empty($mode) || $mode == 'member') {
375 $title = $outputlangs->transnoentities('MembersCards');
376 $keywords = $outputlangs->transnoentities('MembersCards')." ".$outputlangs->transnoentities("Foundation")." ".$outputlangs->convToOutputCharset($mysoc->name);
377 } else {
378 dol_print_error(null, 'Bad value for $mode');
379 return -1;
380 }
381
382
383 if (is_object($object)) {
384 $outputdir = $conf->adherent->dir_output;
385 $dir = $outputdir."/".get_exdir(0, 0, 0, 0, $object, 'member');
386 $file = $dir.'/'.$filename;
387 } else {
388 $outputdir = $conf->adherent->dir_temp;
389 $dir = $outputdir;
390 $file = $dir.'/'.$filename;
391 }
392
393 //var_dump($file);exit;
394
395 if (!file_exists($dir)) {
396 if (dol_mkdir($dir) < 0) {
397 $this->error = $langs->trans("ErrorCanNotCreateDir", $dir);
398 return 0;
399 }
400 }
401
402 $pdf = pdf_getInstance($this->format, $this->Tformat['metric'], $this->Tformat['orientation']);
403
404 if (class_exists('TCPDF')) {
405 $pdf->setPrintHeader(false);
406 $pdf->setPrintFooter(false);
407 }
408 $pdf->SetFont(pdf_getPDFFont($outputlangs));
409
410 $pdf->SetTitle($title);
411 $pdf->SetSubject($title);
412 $pdf->SetCreator("Dolibarr ".DOL_VERSION);
413 $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
414 $pdf->SetKeyWords($keywords);
415 if (getDolGlobalString('MAIN_DISABLE_PDF_COMPRESSION')) {
416 $pdf->SetCompression(false);
417 }
418
419 $pdf->SetMargins(0, 0);
420 $pdf->SetAutoPageBreak(false);
421
422 $this->_Metric_Doc = $this->Tformat['metric'];
423 // Permet de commencer l'impression de l'etiquette desiree dans le cas ou la page a deja service
424 $posX = 1;
425 $posY = 1;
426 if ($posX > 0) {
427 $posX--;
428 } else {
429 $posX = 0;
430 }
431 if ($posY > 0) {
432 $posY--;
433 } else {
434 $posY = 0;
435 }
436 $this->_COUNTX = $posX;
437 $this->_COUNTY = $posY;
438 $this->_Set_Format($pdf, $this->Tformat);
439
440
441 $pdf->Open();
442 $pdf->AddPage();
443
444
445 // Add each record
446 foreach ($arrayofrecords as $val) {
447 // imprime le texte specifique sur la carte
448 $this->Add_PDF_card($pdf, $val['textleft'], $val['textheader'], $val['textfooter'], $langs, $val['textright'], $val['id'], $val['photo']);
449 }
450
451 //$pdf->SetXY(10, 295);
452 //$pdf->Cell($this->_Width, $this->_Line_Height, 'XXX',0,1,'C');
453
454
455 // Output to file
456 $pdf->Output($file, 'F');
457
458 dolChmod($file);
459
460 $this->result = array('fullpath' => $file);
461
462 // Output to http stream
463 if (empty($nooutput)) {
464 clearstatcache();
465
466 $attachment = true;
467 if (getDolGlobalString('MAIN_DISABLE_FORCE_SAVEAS')) {
468 $attachment = false;
469 }
470 $type = dol_mimetype($filename);
471
472 //if ($encoding) header('Content-Encoding: '.$encoding);
473 if ($type) {
474 header('Content-Type: '.$type);
475 }
476 if ($attachment) {
477 header('Content-Disposition: attachment; filename="'.$filename.'"');
478 } else {
479 header('Content-Disposition: inline; filename="'.$filename.'"');
480 }
481
482 // Ajout directives pour resoudre bug IE
483 header('Cache-Control: Public, must-revalidate');
484 header('Pragma: public');
485
486 readfile($file);
487 }
488
489 return 1;
490 }
491}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
Class to manage members of a foundation.
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 expense report based on standard model.
Add_PDF_card(&$pdf, $textleft, $header, $footer, $outputlangs, $textright='', $idmember=0, $photo='')
Output a sticker on page at position _COUNTX, _COUNTY (_COUNTX and _COUNTY start from 0)
__construct($db)
Constructor.
addSticker(&$pdf, $outputlangs, $param)
Output a sticker on page at position _COUNTX, _COUNTY (_COUNTX and _COUNTY start from 0)
write_file($object, $outputlangs, $srctemplatepath, $mode='member', $nooutput=0, $filename='tmp_cards')
Function to build PDF on disk, then output on HTTP stream.
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
dol_mimetype($file, $default='application/octet-stream', $mode=0)
Return MIME type of a file from its name with extension.
dolChmod($filepath, $newmask='')
Change mod of a file.
dol_now($mode='auto')
Return date for now.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
complete_substitutions_array(&$substitutionarray, $outputlangs, $object=null, $parameters=null, $callfunc="completesubstitutionarray")
Complete the $substitutionarray with more entries coming from external module that had set the "subst...
make_substitutions($text, $substitutionarray, $outputlangs=null, $converttextinhtmlifnecessary=0)
Make substitution into a text string, replacing keys with vals from $substitutionarray (oldval=>newva...
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.
get_exdir($num, $level, $alpha, $withoutslash, $object, $modulepart='')
Return a path to have a the directory according to object where files are stored.
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
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition repair.php:142