PageRenderTime 127ms CodeModel.GetById 60ms app.highlight 28ms RepoModel.GetById 33ms app.codeStats 0ms

/htdocs/core/modules/printsheet/doc/pdf_standardlabel.class.php

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