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