PageRenderTime 167ms CodeModel.GetById 81ms app.highlight 46ms RepoModel.GetById 34ms app.codeStats 0ms

/htdocs/core/modules/member/labels/pdf_standardlabel.class.php

https://github.com/asterix14/dolibarr
PHP | 453 lines | 283 code | 61 blank | 109 comment | 89 complexity | a14175d6ac2d95a8162607acfca6678a MD5 | raw file
  1<?php
  2/* Copyright (C) 2001-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3 * Copyright (C) 2002-2003 Jean-Louis Bergamo   <jlb@j1b.org>
  4 * Copyright (C) 2006-2010 Laurent Destailleur  <eldy@users.sourceforge.net>
  5 *
  6 * This program is free software; you can redistribute it and/or modify
  7 * it under the terms of the GNU General Public License as published by
  8 * the Free Software Foundation; either version 2 of the License, or
  9 * (at your option) any later version.
 10 *
 11 * This program is distributed in the hope that it will be useful,
 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14 * GNU General Public License for more details.
 15 *
 16 * You should have received a copy of the GNU General Public License
 17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 18 */
 19
 20/* Inspire de PDF_Label
 21 * PDF_Label - PDF label editing
 22 * @package PDF_Label
 23 * @author Laurent PASSEBECQ <lpasseb@numericable.fr>
 24 * @copyright 2003 Laurent PASSEBECQ
 25 * disponible ici : http://www.fpdf.org/fr/script/script29.php
 26 */
 27
 28////////////////////////////////////////////////////
 29// PDF_Label
 30//
 31// Classe afin d'editer au format PDF des etiquettes
 32// au format Avery ou personnalise
 33//
 34//
 35// Copyright (C) 2003 Laurent PASSEBECQ (LPA)
 36// Base sur les fonctions de Steve Dillon : steved@mad.scientist.com
 37//
 38//-------------------------------------------------------------------
 39// VERSIONS :
 40// 1.0  : Initial release
 41// 1.1  : +	: Added unit in the constructor
 42//	  + : Now Positions start @ (1,1).. then the first image @top-left of a page is (1,1)
 43//	  + : Added in the description of a label :
 44//		font-size	: defaut char size (can be changed by calling Set_Char_Size(xx);
 45//		paper-size	: Size of the paper for this sheet (thanx to Al Canton)
 46//		metric		: type of unit used in this description
 47//				  You can define your label properties in inches by setting metric to 'in'
 48//				  and printing in millimiter by setting unit to 'mm' in constructor.
 49//	  Added some labels :
 50//	        5160, 5161, 5162, 5163,5164 : thanx to Al Canton : acanton@adams-blake.com
 51//		8600 						: thanx to Kunal Walia : kunal@u.washington.edu
 52//	  + : Added 3mm to the position of labels to avoid errors
 53////////////////////////////////////////////////////
 54
 55/**
 56 *	\file       htdocs/core/modules/member/labels/pdf_standardlabel.class.php
 57 *	\ingroup    member
 58 *	\brief      Fichier de la classe permettant d'editer au format PDF des etiquettes au format Avery ou personnalise
 59 *	\author     Steve Dillon
 60 *	\author	    Laurent Passebecq
 61 *	\author	    Rodolphe Quiedville
 62 *	\author	    Jean Louis Bergamo.
 63 */
 64
 65require_once(DOL_DOCUMENT_ROOT.'/core/lib/pdf.lib.php');
 66require_once(DOL_DOCUMENT_ROOT.'/core/lib/format_cards.lib.php');
 67
 68
 69/**
 70 *	\class      pdf_standardlabel
 71 *	\brief      Classe afin d'editer au format PDF des pages d'etiquette adresse au format Avery ou personnalise
 72 */
 73class pdf_standardlabel {
 74
 75	var $code;		// Code of format
 76	var $format;	// Array with informations
 77
 78	// Proprietes privees
 79	var $_Avery_Name	= '';	// Nom du format de l'etiquette
 80	var $_Margin_Left	= 0;	// Marge de gauche de l'etiquette
 81	var $_Margin_Top	= 0;	// marge en haut de la page avant la premiere etiquette
 82	var $_X_Space 	= 0;	// Espace horizontal entre 2 bandes d'etiquettes
 83	var $_Y_Space 	= 0;	// Espace vertical entre 2 bandes d'etiquettes
 84	var $_X_Number 	= 0;	// NX Nombre d'etiquettes sur la largeur de la page
 85	var $_Y_Number 	= 0;	// NY Nombre d'etiquettes sur la hauteur de la page
 86	var $_Width 		= 0;	// Largeur de chaque etiquette
 87	var $_Height 		= 0;	// Hauteur de chaque etiquette
 88	var $_Char_Size	= 10;	// Hauteur des caracteres
 89	var $_Line_Height	= 10;	// Hauteur par defaut d'une ligne
 90	var $_Metric 		= 'mm';	// Type of metric.. Will help to calculate good values
 91	var $_Metric_Doc 	= 'mm';	// Type of metric for the doc..
 92
 93	var $_COUNTX = 1;
 94	var $_COUNTY = 1;
 95	var $_First = 1;
 96
 97
 98
 99	/**
100	 *	Constructor
101	 *
102	 *  @param		DoliDB		$DB      Database handler
103	 */
104	function pdf_standard($db)
105	{
106		$this->db = $db;
107	}
108
109	//Methode qui permet de modifier la taille des caracteres
110	// Cela modiera aussi l'espace entre chaque ligne
111	function Set_Char_Size(&$pdf,$pt) {
112		if ($pt > 3) {
113			$this->_Char_Size = $pt;
114			$this->_Line_Height = $this->_Get_Height_Chars($pt);
115			$pdf->SetFont('','',$pt);
116		}
117	}
118
119
120	// On imprime une etiquette
121	function Add_PDF_card(&$pdf,$textleft,$header='',$footer='',$outputlangs,$textright='')
122	{
123		global $mysoc,$conf,$langs;
124
125		// We are in a new page, then we must add a page
126		if (($this->_COUNTX ==0) and ($this->_COUNTY==0) and (!$this->_First==1)) {
127			$pdf->AddPage();
128		}
129		$this->_First=0;
130		$_PosX = $this->_Margin_Left+($this->_COUNTX*($this->_Width+$this->_X_Space));
131		$_PosY = $this->_Margin_Top+($this->_COUNTY*($this->_Height+$this->_Y_Space));
132
133		// Define logo
134		$logo=$conf->mycompany->dir_output.'/logos/'.$mysoc->logo;
135		if (! is_readable($logo))
136		{
137			$logo='';
138			if (! empty($mysoc->logo_small) && is_readable($conf->mycompany->dir_output.'/logos/thumbs/'.$mysoc->logo_small))
139			{
140				$logo=$conf->mycompany->dir_output.'/logos/thumbs/'.$mysoc->logo_small;
141			}
142			elseif (! empty($mysoc->logo) && is_readable($conf->mycompany->dir_output.'/logos/'.$mysoc->logo))
143			{
144				$logo=$conf->mycompany->dir_output.'/logos/'.$mysoc->logo;
145			}
146		}
147
148		// Print lines
149		if ($this->code == "CARD")
150		{
151			$this->Tformat=$this->_Avery_Labels["CARD"];
152			//$this->_Pointille($pdf,$_PosX,$_PosY,$_PosX+$this->_Width,$_PosY+$this->_Height,0.3,25);
153			$this->_Croix($pdf,$_PosX,$_PosY,$_PosX+$this->_Width,$_PosY+$this->_Height,0.1,10);
154		}
155
156		// Top
157		if ($header!='')
158		{
159			if ($this->code == "CARD")
160			{
161				$pdf->SetDrawColor(128,128,128);
162				$pdf->Line($_PosX, $_PosY+$this->_Line_Height+1, $_PosX+$this->_Width, $_PosY+$this->_Line_Height+1);
163				$pdf->SetDrawColor(0,0,0);
164			}
165			$pdf->SetXY($_PosX, $_PosY+1);
166			$pdf->Cell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($header),0,1,'C');
167		}
168
169		// Center
170		if ($textright=='')	// Only a left part
171		{
172			if ($textleft == '%LOGO%' && $logo) $this->Image($logo,$_PosX+2,$_PosY+3+$this->_Line_Height,20);
173			else if ($textleft == '%PHOTO%' && $photo) $this->Image($photo,$_PosX+2,$_PosY+3+$this->_Line_Height,20);
174			else
175			{
176				$pdf->SetXY($_PosX+3, $_PosY+3+$this->_Line_Height);
177				$pdf->MultiCell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft));
178			}
179		}
180		else if ($textleft!='' && $textright!='')	//
181		{
182			if ($textleft == '%LOGO%' || $textleft == '%PHOTO%')
183			{
184				if ($textleft == '%LOGO%' && $logo) $pdf->Image($logo,$_PosX+2,$_PosY+3+$this->_Line_Height,20);
185				else if ($textleft == '%PHOTO%' && $photo) $pdf->Image($photo,$_PosX+2,$_PosY+3+$this->_Line_Height,20);
186				$pdf->SetXY($_PosX+21, $_PosY+3+$this->_Line_Height);
187				$pdf->MultiCell($this->_Width-22, $this->_Line_Height, $outputlangs->convToOutputCharset($textright),0,'R');
188			}
189			else if ($textright == '%LOGO%' || $textright == '%PHOTO%')
190			{
191				if ($textright == '%LOGO%' && $logo) $pdf->Image($logo,$_PosX+$this->_Width-21,$_PosY+3+$this->_Line_Height,20);
192				else if ($textright == '%PHOTO%' && $photo) $pdf->Image($photo,$_PosX+$this->_Width-21,$_PosY+3+$this->_Line_Height,20);
193				$pdf->SetXY($_PosX+2, $_PosY+3+$this->_Line_Height);
194				$pdf->MultiCell($this->_Width-22, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft));
195			}
196			else
197			{
198				$pdf->SetXY($_PosX+2, $_PosY+3+$this->_Line_Height);
199				$pdf->MultiCell(round($this->_Width/2), $this->_Line_Height, $outputlangs->convToOutputCharset($textleft));
200				$pdf->SetXY($_PosX+round($this->_Width/2), $_PosY+3+$this->_Line_Height);
201				$pdf->MultiCell(round($this->_Width/2)-2, $this->_Line_Height, $outputlangs->convToOutputCharset($textright),0,'R');
202			}
203		}
204		else	// Only a right part
205		{
206			if ($textright == '%LOGO%' && $logo) $this->Image($logo,$_PosX+$this->_Width-21,$_PosY+1,20);
207			else if ($textright == '%PHOTO%' && $photo) $this->Image($photo,$_PosX+$this->_Width-21,$_PosY+1,20);
208			else
209			{
210				$pdf->SetXY($_PosX+2, $_PosY+3+$this->_Line_Height);
211				$pdf->MultiCell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($textright),0,'R');
212			}
213		}
214
215		// Bottom
216		if ($footer!='')
217		{
218			if ($this->code == "CARD")
219			{
220				$pdf->SetDrawColor(128,128,128);
221				$pdf->Line($_PosX, $_PosY+$this->_Height-$this->_Line_Height-2, $_PosX+$this->_Width, $_PosY+$this->_Height-$this->_Line_Height-2);
222				$pdf->SetDrawColor(0,0,0);
223			}
224			$pdf->SetXY($_PosX, $_PosY+$this->_Height-$this->_Line_Height-1);
225			$pdf->Cell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($footer),0,1,'C');
226		}
227		//print "$_PosY+$this->_Height-$this->_Line_Height-1<br>\n";
228
229		$this->_COUNTY++;
230
231		if ($this->_COUNTY == $this->_Y_Number) {
232			// Si on est en bas de page, on remonte le 'curseur' de position
233			$this->_COUNTX++;
234			$this->_COUNTY=0;
235		}
236
237		if ($this->_COUNTX == $this->_X_Number) {
238			// Si on est en bout de page, alors on repart sur une nouvelle page
239			$this->_COUNTX=0;
240			$this->_COUNTY=0;
241		}
242	}
243
244
245	function _Pointille(&$pdf,$x1=0,$y1=0,$x2=210,$y2=297,$epaisseur=1,$nbPointilles=15)
246	{
247		$pdf->SetLineWidth($epaisseur);
248		$length=abs($x1-$x2);
249		$hauteur=abs($y1-$y2);
250		if($length>$hauteur) {
251			$Pointilles=($length/$nbPointilles)/2; // taille des pointilles
252		}
253		else {
254			$Pointilles=($hauteur/$nbPointilles)/2;
255		}
256		for($i=$x1;$i<=$x2;$i+=$Pointilles+$Pointilles) {
257			for($j=$i;$j<=($i+$Pointilles);$j++) {
258				if($j<=($x2-1)) {
259		$pdf->Line($j,$y1,$j+1,$y1); // on trace le pointill? du haut, point par point
260		$pdf->Line($j,$y2,$j+1,$y2); // on trace le pointill? du bas, point par point
261				}
262			}
263		}
264		for($i=$y1;$i<=$y2;$i+=$Pointilles+$Pointilles) {
265			for($j=$i;$j<=($i+$Pointilles);$j++) {
266				if($j<=($y2-1)) {
267		$pdf->Line($x1,$j,$x1,$j+1); // on trace le pointill? du haut, point par point
268		$pdf->Line($x2,$j,$x2,$j+1); // on trace le pointill? du bas, point par point
269				}
270			}
271		}
272	}
273
274	/*
275	 * Fonction realisant une croix aux 4 coins des cartes
276	 */
277	function _Croix(&$pdf,$x1=0,$y1=0,$x2=210,$y2=297,$epaisseur=1,$taille=4)
278	{
279		$pdf->SetDrawColor(192,192,192);
280
281		$pdf->SetLineWidth($epaisseur);
282		$lg=$taille/2;
283		// croix haut gauche
284		$pdf->Line($x1,$y1-$lg,$x1,$y1+$lg);
285		$pdf->Line($x1-$lg,$y1,$x1+$lg,$y1);
286		// croix bas gauche
287		$pdf->Line($x1,$y2-$lg,$x1,$y2+$lg);
288		$pdf->Line($x1-$lg,$y2,$x1+$lg,$y2);
289		// croix haut droit
290		$pdf->Line($x2,$y1-$lg,$x2,$y1+$lg);
291		$pdf->Line($x2-$lg,$y1,$x2+$lg,$y1);
292		// croix bas droit
293		$pdf->Line($x2,$y2-$lg,$x2,$y2+$lg);
294		$pdf->Line($x2-$lg,$y2,$x2+$lg,$y2);
295
296		$pdf->SetDrawColor(0,0,0);
297	}
298
299	// convert units (in to mm, mm to in)
300	// $src and $dest must be 'in' or 'mm'
301	function _Convert_Metric ($value, $src, $dest) {
302		if ($src != $dest) {
303			$tab['in'] = 39.37008;
304			$tab['mm'] = 1000;
305			return $value * $tab[$dest] / $tab[$src];
306		} else {
307			return $value;
308		}
309	}
310
311	// Give the height for a char size given.
312	function _Get_Height_Chars($pt) {
313		// Tableau de concordance entre la hauteur des caracteres et de l'espacement entre les lignes
314		$_Table_Hauteur_Chars = array(6=>2, 7=>2.5, 8=>3, 9=>3.5, 10=>4, 11=>6, 12=>7, 13=>8, 14=>9, 15=>10);
315		if (in_array($pt, array_keys($_Table_Hauteur_Chars))) {
316			return $_Table_Hauteur_Chars[$pt];
317		} else {
318			return 100; // There is a prob..
319		}
320	}
321
322	function _Set_Format(&$pdf, $format) {
323		$this->_Metric 	= $format['metric'];
324		$this->_Avery_Name 	= $format['name'];
325		$this->_Avery_Code	= $format['code'];
326		$this->_Margin_Left	= $this->_Convert_Metric($format['marginLeft'], $this->_Metric, $this->_Metric_Doc);
327		$this->_Margin_Top	= $this->_Convert_Metric($format['marginTop'], $this->_Metric, $this->_Metric_Doc);
328		$this->_X_Space 	= $this->_Convert_Metric($format['SpaceX'], $this->_Metric, $this->_Metric_Doc);
329		$this->_Y_Space 	= $this->_Convert_Metric($format['SpaceY'], $this->_Metric, $this->_Metric_Doc);
330		$this->_X_Number 	= $format['NX'];
331		$this->_Y_Number 	= $format['NY'];
332		$this->_Width 	= $this->_Convert_Metric($format['width'], $this->_Metric, $this->_Metric_Doc);
333		$this->_Height	= $this->_Convert_Metric($format['height'], $this->_Metric, $this->_Metric_Doc);
334		$this->Set_Char_Size($pdf, $format['font-size']);
335	}
336
337
338    /**
339     *      \brief      Function to build PDF on disk, then output on HTTP strem.
340     *      \param      arrayofmembers  Array of members informations
341     *      \param      outputlangs     Lang object for output language
342     *      \return     int             1=ok, 0=ko
343     */
344    function write_file($arrayofmembers,$outputlangs)
345    {
346        global $user,$conf,$langs,$mysoc,$_Avery_Labels;
347
348        // Choose type (L7163 by default)
349        $this->code=empty($conf->global->ADHERENT_ETIQUETTE_TYPE)?'L7163':$conf->global->ADHERENT_ETIQUETTE_TYPE;
350        $this->Tformat = $_Avery_Labels[$this->code];
351        if (empty($this->Tformat)) { dol_print_error('','ErrorBadTypeForCard'.$this->code); exit; }
352        $this->type = 'pdf';
353        $this->format = $this->Tformat['paper-size'];
354
355        if (! is_object($outputlangs)) $outputlangs=$langs;
356		// For backward compatibility with FPDF, force output charset to ISO, because FPDF expect text to be encoded in ISO
357		if (! empty($conf->global->MAIN_USE_FPDF)) $outputlangs->charset_output='ISO-8859-1';
358
359        $outputlangs->load("main");
360        $outputlangs->load("dict");
361        $outputlangs->load("companies");
362        $outputlangs->load("members");
363        $outputlangs->load("admin");
364
365
366        $dir = $conf->adherent->dir_temp;
367        $file = $dir . "/tmplabels.pdf";
368
369        if (! file_exists($dir))
370        {
371            if (create_exdir($dir) < 0)
372            {
373                $this->error=$langs->trans("ErrorCanNotCreateDir",$dir);
374                return 0;
375            }
376        }
377
378        $pdf=pdf_getInstance($this->format,$this->Tformat['metric']);
379
380        if (class_exists('TCPDF'))
381        {
382            $pdf->setPrintHeader(false);
383            $pdf->setPrintFooter(false);
384        }
385        $pdf->SetFont(pdf_getPDFFont($outputlangs));
386
387        $pdf->SetTitle($outputlangs->transnoentities('MembersLabels'));
388        $pdf->SetSubject($outputlangs->transnoentities("MembersLabels"));
389        $pdf->SetCreator("Dolibarr ".DOL_VERSION);
390        $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
391        $pdf->SetKeyWords($outputlangs->transnoentities('MembersLabels')." ".$outputlangs->transnoentities("Foundation")." ".$outputlangs->convToOutputCharset($mysoc->name));
392        if ($conf->global->MAIN_DISABLE_PDF_COMPRESSION) $pdf->SetCompression(false);
393
394        $pdf->SetMargins(0,0);
395        $pdf->SetAutoPageBreak(false);
396
397        $this->_Metric_Doc = $this->Tformat['metric'];
398        // Permet de commencer l'impression de l'etiquette desiree dans le cas ou la page a deja servie
399        $posX=1;
400        $posY=1;
401        if ($posX > 0) $posX--; else $posX=0;
402        if ($posY > 0) $posY--; else $posY=0;
403        $this->_COUNTX = $posX;
404        $this->_COUNTY = $posY;
405        $this->_Set_Format($pdf, $this->Tformat);
406
407
408        $pdf->Open();
409        $pdf->AddPage();
410
411
412        // Add each record
413        foreach($arrayofmembers as $val)
414        {
415            // imprime le texte specifique sur la carte
416            $this->Add_PDF_card($pdf,$val['textleft'],$val['textheader'],$val['textfooter'],$langs,$val['textright'],$val['id'],$val['photo']);
417        }
418
419        //$pdf->SetXY(10, 295);
420        //$pdf->Cell($this->_Width, $this->_Line_Height, 'XXX',0,1,'C');
421
422
423        // Output to file
424        $pdf->Output($file,'F');
425
426        if (! empty($conf->global->MAIN_UMASK))
427            @chmod($file, octdec($conf->global->MAIN_UMASK));
428
429
430
431        // Output to http stream
432        clearstatcache();
433
434        $attachment=true;
435        if (! empty($conf->global->MAIN_DISABLE_FORCE_SAVEAS)) $attachment=false;
436        $filename='tmplabels.pdf';
437        $type=dol_mimetype($filename);
438
439        if ($encoding)   header('Content-Encoding: '.$encoding);
440        if ($type)       header('Content-Type: '.$type);
441        if ($attachment) header('Content-Disposition: attachment; filename="'.$filename.'"');
442        else header('Content-Disposition: inline; filename="'.$filename.'"');
443
444        // Ajout directives pour resoudre bug IE
445        header('Cache-Control: Public, must-revalidate');
446        header('Pragma: public');
447
448        readfile($file);
449
450        return 1;
451    }
452}
453?>