source: trunk/spip/esqueleto-redcta/plugins/article_pdf/pdf/lib_pdf_global.php @ 30

Last change on this file since 30 was 30, checked in by sebas, 17 years ago

nueva importacion del codigo del esqueleto de redcta con los plugins

File size: 23.8 KB
Line 
1<?php
2/**
3 * class PDF extends FPDF : FPDF/tutoriel/tuto6.htm
4 *
5 * Février-Août 2003 : Jérôme Fenal (jerome.fenal@logicacmg.com)
6 * Ajout de la prise en compte des tableaux, tag <code>, et diverses autres choses de SPIP
7 */ 
8
9 //  Fichier de dump pour debug
10define (DUMP_FILE_FULL_PATH_NAME,"Dump.txt");
11
12 
13class PDF extends FPDF
14{
15var $HREF;
16var $texteAddSpace;
17var $SRC;
18var $columnProp=array();                # propriétés de la ligne
19var $lineProp=array();          # propriétés de la ligne
20var $inFirstRow;                # flag si première ligne en cours
21var $TableX;                    # abscisse du tableau
22var $HeaderColor;
23var $RowColors;
24var $tableProp=array();
25
26var $ProcessingBloc=0;
27var $BlocContent=array();
28var $BlocTags=array();
29
30var $ProcessingTable=false;     # =1 : en cours lecture table
31var $ProcessingCadre=false;     # =1 : en cours lecture contenu d'un cadre SPIP (TEXTAREA HTML)
32var $tableCurrentCol;   # numéro de cellule courante
33var $tableCurrentRow;   # Numero de ligne courante pendant la lecture d'un tableau
34var $tableContent=array();              # Contenu de la table courante pendant son absorption. Non réentrant car SPIP ne permet pas de faire
35                                                # de table dans une autre table.
36var $listDepth=0;               # profondeur courante de liste à puce
37var $listParm = array();        # paramètres des listes à puces en fonction du niveau
38
39var $TopLinkIDArray = array(); #Sauve les IDs des liens internes (notes dans le texte)
40var $TopLinkIDArrayIt = 0; #Itérateur dans le tableau des IDs des liens internes
41
42var $BottomLinkIDArray = array(); #Sauve les IDs des liens internes (notes en fin de document)
43var $BottomLinkIDArrayIt = 0; #Itérateur dans le tableau des IDs des liens internes
44
45var $FirstIteration = TRUE;  # booleen pour la génération des liens
46var $CurrentTag=array();
47
48
49function Build($OutputFileFullPathName)
50{
51        $this->Open();
52       
53        $this->FirstIteration=TRUE;
54        $this->BuildDocument() ;
55       
56        $this->ResetBuffer();
57        $this->tag=0;
58        $this->SetFont('','');
59       
60        $this->FirstIteration=FALSE;
61        $this->BuildDocument() ;
62
63        $this->Output($OutputFileFullPathName);
64       
65        $this->Close();
66}
67
68
69function AddCol($field=-1,$width=-1,$align='L')
70{
71        //Ajoute une colonne au tableau
72        if($field==-1)
73        {
74                $field=count($this->columnProp);
75        }
76       
77        $this->columnProp[$field]=array('f'=>$field,'w'=>$width,'a'=>$align);
78        #$this->Write(5, "Ajout de colonne : ".$field."/".$width."/".$align); $this->Ln();
79}
80
81
82function PDF($orientation='P', $unit='mm', $format='A4')
83{
84        //Appel au constructeur parent
85        $this->FPDF($orientation, $unit, $format);
86        $this->SetCompression(1);
87       
88        //$this->InitDumpFile();
89       
90        $this->HREF='';
91}
92
93function unhtmlentities($string)
94{
95        $trans_tbl = get_html_translation_table (HTML_ENTITIES);
96        $trans_tbl = array_flip ($trans_tbl);
97        $ret = strtr ($string, $trans_tbl);
98        return preg_replace('/&#(\d+);/me', "chr('\\1')",$ret);
99}
100
101function WriteHTML($html,$LineFeedHeight)
102{
103        $this->texteAddSpace=false;
104        //Parseur HTML, enlevé pour une meilleure récupération des tag.
105  //Il faut détecter les vraies balises "<" HTML et pas les < de texte "&lt;" HTML
106  //Parseur remis + loin pour l'édition du texte
107        //$html=$this->unhtmlentities($html);
108       
109        $a=preg_split(',(<[/a-zA-Z].*>),Ums', $html, -1, PREG_SPLIT_DELIM_CAPTURE);
110
111        // $a = le tableau de tags
112        // $i = index de l'élément courant
113        // $e = valeur de l'élément courant
114        foreach($a as $i=>$e) 
115  {
116                //Balise
117        $Balise= preg_match(',<(?=[/a-zA-Z0-9])(/)?([/a-zA-Z0-9]+)((\s.*|/)?)>,',$e,$match);
118        if ($Balise){
119      $tag=strtoupper($match[2]);
120                        $closing = $match[1]=="/";
121                       
122                        if (($this->ProcessingBloc) AND (!in_array($tag,$this->BlocTags[$this->ProcessingBloc-1])))
123                                $this->BlocContent[$this->ProcessingBloc-1] .= $e;
124                        else {
125                                if ($closing)
126                                // C'est une balise fermante
127                                        $this->CloseTag($tag,$LineFeedHeight);
128                                else
129                                        $this->OpenTag($tag,$e,$LineFeedHeight);
130                        }
131                }
132                // Contenu
133                else {
134                        if (!in_array('TEXTAREA',$this->CurrentTag))
135                                $e=str_replace("\n",' ',$e);
136                        if (strlen($e)){
137                                $te = trim($e);
138                                if (!$this->texteAddSpace)
139                                        $this->texteAddSpace = (strlen($te)==0) | ($e{0}!=$te{0});
140                                $next_add_space = (strlen($te)==0) | (substr($e,-1)!=substr($te,-1));
141                                $e = $te;
142                        }
143                        if(strlen($e)){
144                                $e = $this->texteAddSpace?" $e":$e;
145                                $this->texteAddSpace = $next_add_space;
146                                # Attention, ce mécanisme ne permet pas de traiter les liens dans les tableaux...
147                                # ni les tableaux dans les tableaux, d'ailleurs...
148                                if (($this->ProcessingBloc))
149                                        $this->BlocContent[$this->ProcessingBloc-1] .= $e;
150                                else 
151                                {
152                                        // C'est un lien. Il faut faire la distinction entre lien externe, lien interne et note de bas de page (couples ancre + lien interne)
153                                        if($this->HREF) 
154                                        {
155                                                $Link=$this->HREF;
156                                                $Text=$e;
157                                                if ( strstr($Link,"http:") || strstr($Link,"mailto:") || strstr($Link,"ftp:") )
158                                                {
159                                                        // C'est un lien  externe
160                                                        $this->PutLink($Link, $Text);
161                                                }
162                                                else
163                                                {
164                                                        // C'est une note (référence dans le texte)
165                                                        if ( strstr($Link,"#nb") )
166                                                        {
167                                                                if ($this->FirstIteration)
168                                                                {
169                                                                        $LinkID=$this->AddLink();
170                                                                        $this->SetLink($LinkID,-1,-1);
171                                                                        $this->TopLinkIDArray[]=$LinkID;
172                                                                        $this->PutLink($Link,$Text);  // Lien bidon (première itération)
173                                                                }
174                                                                else
175                                                                {
176                                                                        $LinkID=$this->BottomLinkIDArray[$this->BottomLinkIDArrayIt++];
177                                                                        $this->PutLink($LinkID,$Text);   // Bon lien  (deuxième itération)
178                                                                }
179                                                        }
180                                                        // C'est une note (détail de bas de texte)
181                                                        else if ( strstr($Link,"#nh") )
182                                                        {
183                                                       
184                                                                // C'est le lien "#nh1" (le premier) : on met un trait séparateur
185                                                                if ( strlen($Link)==4 && $Link[3]=="1" )
186                                                                {
187                                                                        $this->SetLineWidth(0.3);
188                                                                        $this->Line($this->lMargin, $this->GetY()-5, $this->w - $this->rMargin, $this->GetY()-5);
189                                                                }
190                                                               
191                                                                if ($this->FirstIteration)
192                                                                {
193                                                                        $LinkID=$this->AddLink();
194                                                                        $this->SetLink($LinkID,-1,-1);
195                                                                        $this->BottomLinkIDArray[]=$LinkID;
196                                                                        $this->PutLink($Link,$Text);  // Lien bidon (première itération)
197                                                                }
198                                                                else
199                                                                {
200                                                                        $LinkID=$this->TopLinkIDArray[$this->TopLinkIDArrayIt++];
201                                                                        $this->PutLink($LinkID,$Text);          // Bon lien  (deuxième itération)
202                                                                }
203                                                               
204                                                        }
205                                                        // C'est un lien interne
206                                                        else
207                                                        {
208                                                                $WebSiteURL=entites_html(lire_meta("adresse_site"));
209                                                                // Bug d'interprétation du point d'interrogation remplacé par 3 points. Correctif ici
210                                                                $Link=str_replace("...","?",$Link);
211                                                               
212                                                                $this->PutLink($WebSiteURL . "/" . $Link, $Text);
213                                                        }
214                                                }
215                                        } else 
216                                        {
217                                        //Parseur remis ici
218                                                $e=$this->unhtmlentities($e);
219            $this->Write(5,$e);
220                                        }
221                                }
222                        }
223                }
224        }
225 
226}
227
228function OpenTag($tag,$e,$LineFeedHeight)
229{
230        $needclosing = true;   
231        //Balise ouvrante
232        if ($tag=='B' || $tag=='U' || $tag=='I')
233        {
234                $this->SetStyle($tag,true);
235        }
236       
237        if ($tag=='STRONG')
238        {
239                $this->SetStyle('B',true);
240        }
241       
242        if ($tag=='EM')
243        {
244                $this->SetStyle('I',true);
245        }
246
247        if($tag=='A')
248        {
249                $this->HREF=extraire_attribut($e,'href');
250                $this->texteHREF="";
251                if ($this->texteAddSpace) {
252                        $this->Write(5," ");
253                        $this->texteAddSpace = false;
254                }
255        }
256        if($tag=='BR') {
257                $this->maxLineWidth = max($this->maxLineWidth,$this->x);
258                $this->Ln($LineFeedHeight);
259                $needclosing = false;
260                $this->texteAddSpace = false;
261        }
262
263        if($tag=='P') {
264                $this->maxLineWidth = max($this->maxLineWidth,$this->x);
265                $this->Ln(1.5*$LineFeedHeight);
266                $this->texteAddSpace = false;
267        }
268        if($tag=='DIV') {
269                $this->maxLineWidth = max($this->maxLineWidth,$this->x);
270                $this->Ln(1*$LineFeedHeight);
271                $this->texteAddSpace = false;
272        }
273
274        if($tag=='CODE') {
275//              $this->Write(5,"<code>\n");
276                $this->SetFont('courier','', 8);
277                $this->SetTextColor(0, 0, 255);
278                $this->ProcessingCadre=true;
279                $this->ProcessingBloc++;
280                $this->BlocTags[$this->ProcessingBloc-1]=array("CODE");
281                $this->BlocContent[$this->ProcessingBloc-1]="";
282        }
283       
284        if($tag=='H2')
285        {
286                $this->maxLineWidth = max($this->maxLineWidth,$this->x);
287                $this->Ln($LineFeedHeight*3);
288                $this->SetStyle($tag='B',true,24);
289                $this->texteAddSpace = false;
290        }
291
292        if($tag=='H3')
293        {
294                $this->maxLineWidth = max($this->maxLineWidth,$this->x);
295                $this->Ln($LineFeedHeight*3);
296                $this->SetStyle($tag='B',true,20);
297                $this->texteAddSpace = false;
298        }
299
300        if($tag=='H4')
301        {
302                $this->maxLineWidth = max($this->maxLineWidth,$this->x);
303                $this->Ln($LineFeedHeight*3);
304                $this->SetStyle($tag='B',true,18);
305                $this->texteAddSpace = false;
306        }
307
308        if($tag=='H5')
309        {
310                $this->maxLineWidth = max($this->maxLineWidth,$this->x);
311                $this->Ln($LineFeedHeight*3);
312                $this->SetStyle($tag='B',true,14);
313                $this->texteAddSpace = false;
314        }
315
316        if($tag=='H6')
317        {
318                $this->maxLineWidth = max($this->maxLineWidth,$this->x);
319                $this->Ln($LineFeedHeight*3);
320                $this->SetStyle($tag='B',true,12);
321                $this->texteAddSpace = false;
322        }
323
324        if($tag=='UL' or $tag=='OL') {
325                $this->SetLeftMargin($this->lMargin+7);
326                $this->listDepth++;
327                $this->listParm[$this->listDepth]['type']=$tag;
328                $this->listParm[$this->listDepth]['curr']=0;            # numéro si OL
329        }
330
331        if($tag=='LI'){ 
332                $this->maxLineWidth = max($this->maxLineWidth,$this->x);
333                $this->Ln();
334                $this->listParm[$this->listDepth]['curr']++;
335                $this->SetX($this->GetX()-7);
336                if ($this->listParm[$this->listDepth]['type']=='OL')
337                        $this->Cell(7,5,$this->listParm[$this->listDepth]['curr'].'.',0,0,'C'); 
338                else
339                        $this->Cell(7,5,chr(149),0,0,'C'); 
340        }
341
342        if ($tag=='IMG') {
343    $this->SRC=extraire_attribut($e,'src');
344
345                // si l'image est manquante mettre un lien avec le texte alt
346                if (!@is_readable($this->SRC)){
347                        $alt = extraire_attribut($e,'alt');
348                        if ($alt==NULL) $alt = $this->SRC;
349                        //var_dump("img:href=".$this->HREF.':');
350                        if ($this->HREF=="")
351                                $this->Write(5,"[$alt]");
352                        else 
353                                $this->PutLink($this->HREF,"[$alt]");
354                }
355                else
356                {
357                        $size=getimagesize($this->SRC);         # Attention, utilisation de GD !!! FPDF ne sait pas lire les images à moitié... et je n'ai pas envie de surcharger la méthode Image...
358                        if ($size[0] < 30 && $size[1] < 30) {
359                                # pixel / 3 pour avoir des cm. Petite cuisine...
360                                $imgX=$size[0]/3;
361                                $imgY=$size[1]/3;
362                                $yoffset=$imgY/4;
363                                if ($this->GetY() + $imgY > $this->h - $this->bMargin)
364                                        $this->AddPage();
365                                $this->Image($this->SRC, $this->GetX(), $this->GetY()-$yoffset, $imgX, $imgY,'',$this->HREF);
366                                $this->SetX($this->GetX()+$size[0]/2);
367                        } else if ($size[0] < 600 && $size[1] < 600) {
368                                $pwidth=$this->w-$this->lMargin-$this->rMargin;
369                                $ratio = 0.24;  # ce qui fait environ 600 pixels sur 16cm d'espace utile (160/600) - 2 pouillièmes
370                                $imgX=$size[0]*$ratio;
371                                $imgY=$size[1]*$ratio;
372                                if ($this->GetY() + $imgY > $this->h - $this->bMargin) {
373                                        if ($this->GetY() + $imgY*0.8 > $this->h - $this->bMargin) {
374                                                $this->AddPage();
375                                        } else {
376                                                $imgX=$imgX*0.8;
377                                                $imgY=$imgY*0.8;
378                                        }
379                                }
380                                $this->Image($this->SRC, $this->GetX()+($pwidth-$imgX)/2, $this->GetY(), $imgX, $imgY,'',$this->HREF);
381                                $this->SetY($this->GetY()+$imgY);
382                        } else {
383                                // les deux dimensions sont supérieurs à 600 pixels
384                                $pwidth=$this->w-$this->lMargin-$this->rMargin;
385                                $ratioX = $pwidth / $size[0];
386                                $plen=$this->h-$this->GetY()-$this->bMargin-20;         // on retire 20mm pour placer le cartouche de l'image
387                                $ratioY = $plen / $size[1];
388                                $ratio = 0.24;  # ce qui fait environ 600 pixels sur 16cm d'espace utile (160/600) - 2 pouillièmes
389                                $imgX=$size[0]*$ratio;
390                                $imgY=$size[1]*$ratio;
391       
392                                if ($size[1] > 900 || ($plen - ($size[1]*$ratio)  < 0)) {
393                                        if ($plen - ($size[1]*$ratio*0.8)  < 0) {
394                                                $this->AddPage();
395                                                $plen=$this->h-$this->GetY()-$this->bMargin-20; // toujours la marge du cartouche
396                                                $ratioY = $plen / $size[1];
397                                        } else {
398                                                $ratioX *= 0.8;
399                                                $ratioY *= 0.8;
400                                        }
401                                }
402       
403                                $ratio=min(0.24, $ratioX, $ratioY);
404       
405                                $imgX=$size[0]*$ratio;
406                                $imgY=$size[1]*$ratio;
407       
408                                $this->Image($this->SRC, $this->GetX()+($pwidth-$imgX)/2, $this->GetY(), $imgX, $imgY,'',$this->HREF);
409                                $this->SetY($this->GetY()+$imgY);
410                        }
411                }
412        }
413
414        if($tag=='TT' or $tag=='TEXTAREA') {
415                $this->SetFont('courier','', 8);
416                $this->SetTextColor(255, 0, 0);
417                if ($tag=='TEXTAREA'){
418                        $this->ProcessingCadre=true;
419                        }
420        }
421
422        if($tag=='TABLE') {
423                $this->ProcessingTable=true;
424                $this->inFirstRow=1;
425                # on commence une table
426                if(!isset($this->tableProp['width']))
427                        $this->tableProp['width']=0;
428
429                if($this->tableProp['width']==0)
430                        $this->tableProp['width']=$this->w-$this->lMargin-$this->rMargin;
431                if(!isset($this->tableProp['align']))
432                        $this->tableProp['align']='C';
433                if(!isset($this->tableProp['padding']))
434                        $this->tableProp['padding']=$this->cMargin;
435                $cMargin=$this->cMargin;
436                $this->cMargin=$this->tableProp['padding'];
437                if(!isset($this->tableProp['HeaderColor']))
438                        $this->tableProp['HeaderColor']=array(200, 200, 200);
439                $this->HeaderColor=$this->tableProp['HeaderColor'];
440                if(!isset($this->tableProp['color1']))
441                        $this->tableProp['color1']=array();
442                if(!isset($this->tableProp['color2']))
443                        $this->tableProp['color2']=array(230, 230, 230);
444                $this->RowColors=array($this->tableProp['color1'], $this->tableProp['color2']);
445                $this->tableCurrentRow=0;
446        }
447
448        if($tag=='TR') {
449                # on commence une ligne
450                $this->tableCurrentCol=0;
451                $this->tableCurrentRow++;
452                if (extraire_attribut($e,'class') == 'row_first') {
453                        $this->ProcessingTH=true;
454                }
455        }
456
457        if($tag=='TH' or $tag=='TD') {
458                # Cellule (pas titre)
459                $this->tableCurrentCol += 1;
460                $colspan = extraire_attribut($e,'colspan');
461                if ($this->inFirstRow) {
462                        if ($colspan)
463                                for($i=0;$i<$colspan;$i++){
464                                        $this->nCols=$this->tableCurrentCol;
465                                        $this->AddCol();
466                                }
467                        else {
468                                $this->nCols=$this->tableCurrentCol;
469                                $this->AddCol();
470                        }
471                }
472                $this->ProcessingBloc++;
473                $this->BlocTags[$this->ProcessingBloc-1]=array("TH","TD");
474                $this->BlocContent[$this->ProcessingBloc-1]="";
475        }
476        if($tag=='BLOCKQUOTE') {
477                $this->ProcessingBloc++;
478                $this->BlocTags[$this->ProcessingBloc-1]=array("BLOCKQUOTE");
479                $this->BlocContent[$this->ProcessingBloc-1]='';
480        }
481        if($tag=='TEXTAREA') {
482                $this->ProcessingBloc++;
483                $this->BlocTags[$this->ProcessingBloc-1]=array("TEXTAREA");
484                $this->BlocContent[$this->ProcessingBloc-1]="";
485        }
486        if($tag=='HR') 
487        {
488                # Ligne horizontale
489                $this->SetLineWidth(0.3);
490                $this->Line($this->lMargin, $this->GetY(), $this->w - $this->rMargin, $this->GetY());
491                $needclosing = false;
492                $this->texteAddSpace = false;
493        }
494        if ((substr($e,-2)!="/>") && $needclosing)
495                $this->CurrentTag[]=$tag;       
496}
497
498function CloseTag($tag,$LineFeedHeight)
499{
500        if($tag=='B' || $tag=='U' || $tag=='I')
501                $this->SetStyle($tag,false);
502       
503        if($tag=='STRONG')
504                $this->SetStyle('B',false);
505       
506        if($tag=='EM')
507                $this->SetStyle('I',false);
508               
509        if($tag=='A'){
510                $this->HREF='';
511        }
512               
513        if($tag=='P'){
514                $this->maxLineWidth = max($this->maxLineWidth,$this->x);
515                $this->Ln($LineFeedHeight);
516        }
517
518        if($tag=='CODE') {
519    $this->ProcessingCadre=false;
520                if (strlen($content=$this->BlocContent[$this->ProcessingBloc-1])){
521                        $this->ProcessingBloc--;
522                        $this->BlocShow(0,$content,1,$LineFeedHeight);
523      }
524                $this->SetTextColor(0);
525                $this->SetFont('helvetica','',10);
526//    $this->Write(5,"\n<\code>");
527        }
528
529        if(($tag=='H2') OR ($tag=='H3') OR ($tag=='H4') OR ($tag=='H5') OR ($tag=='H6')){               
530                $this->SetStyle($tag='B',false,10);
531                $this->maxLineWidth = max($this->maxLineWidth,$this->x);
532                $this->Ln($LineFeedHeight);
533        }
534       
535        if($tag=='UL' or $tag=='OL') { 
536                $this->SetLeftMargin($this->lMargin-7); 
537                $this->maxLineWidth = max($this->maxLineWidth,$this->x);
538                $this->Ln();
539                $this->listParm[$this->listDepth]=array();
540                $this->listDepth--;
541        } 
542        if($tag=='TT') { 
543                $this->SetFont('helvetica','',10);
544                $this->SetTextColor(0);
545        }
546        if($tag=='TD' or $tag=='TH') {
547                if (!strlen($this->BlocContent[$this->ProcessingBloc-1]))
548                        $this->tableContent[$this->tableCurrentRow][$this->tableCurrentCol - 1]['content']=" ";
549                else 
550                        $this->tableContent[$this->tableCurrentRow][$this->tableCurrentCol - 1]['content']=$this->BlocContent[$this->ProcessingBloc-1];
551                if ($tag=='TH')
552                        $this->tableContent[$this->tableCurrentRow][$this->tableCurrentCol - 1]['TH']=1;
553                else
554                        $this->tableContent[$this->tableCurrentRow][$this->tableCurrentCol - 1]['TH']=0;
555                $this->ProcessingBloc--;
556        }
557        if($tag=='TR') {
558                $this->inFirstRow=0;    # on a fini une ligne donc la première aussi
559        }
560        if($tag=='TABLE') {
561                $this->TableShow('C',$LineFeedHeight);
562                $this->inFirstRow=0;
563                $this->ProcessingTable=false;
564                $this->cMargin=$cMargin;
565                $this->columnProp=array();
566                $this->tableContent=array();
567        }
568        if($tag=='BLOCKQUOTE') {
569                if (strlen($content=$this->BlocContent[$this->ProcessingBloc-1])){
570                        $this->ProcessingBloc--;
571                        $this->BlocShow(10,$content,1,$LineFeedHeight);
572                }
573        }
574        if($tag=='TEXTAREA') {
575                        $this->ProcessingCadre=false;
576                if (strlen($content=$this->BlocContent[$this->ProcessingBloc-1])){
577                        $this->ProcessingBloc--;
578                        $this->BlocShow(0,$content,1,$LineFeedHeight);
579                }
580                $this->SetFont('helvetica','',10);
581                $this->SetTextColor(0);
582        }
583        if ($tag==end($this->CurrentTag))
584                array_pop($this->CurrentTag);
585}
586
587function SetStyle($tag,$enable,$size=0)
588{
589        static $currentStyle=array();
590        if (in_array($tag,array('B','I','U'))){
591                if ($enable)
592                        $currentStyle = array_merge($currentStyle,array($tag=>true));
593                else 
594                        $currentStyle = array_diff($currentStyle,array($tag=>true));
595                $family = $this->FontFamily?$this->FontFamily:'helvetica';
596                $this->SetFont($family,implode("",array_keys($currentStyle)), $size);
597        }
598}
599
600// $Link peut être un entier (ID de lien interne)  ou une chaîne (URL)
601function PutLink($Link,$Text)
602{
603        //Place un hyperlien
604        $this->SetTextColor(0,0,255);
605        $this->SetStyle('U',true);
606        $this->Write(5,$Text,$Link);
607        $this->SetStyle('U',false);
608        $this->SetTextColor(0);
609}
610
611
612function Header()
613{
614}
615
616/*
617Pas utilisé pour l'instant
618function Header()
619{
620    //Imprime l'en-tête du tableau si nécessaire
621    if($this->ProcessingTable)
622        $this->TableHeader();
623}
624
625function TableHeader()
626{
627    $this->SetFont('helvetica','B',8);
628    $this->SetX($this->TableX);
629    $fill=!empty($this->HeaderColor);
630    if($fill)
631        $this->SetFillColor($this->HeaderColor[0],$this->HeaderColor[1],$this->HeaderColor[2]);
632    foreach($this->columnProp as $col)
633        $this->Cell($column['w'],6,1,0,'C',$fill);
634    $this->Ln();
635}
636*/
637
638function CellSize($htmlContent,$fontFamily,$fontSize,$LineFeedHeight,$cellmargin=3,$max_width=0){
639        $cell_pdf=new PDF_SPIP();
640        $cell_pdf->Open();
641        $cell_pdf->FirstIteration=TRUE;
642        $cell_pdf->SetFont($fontFamily, '', $fontSize);
643       
644        $cell_pdf->ResetBuffer();
645        $cell_pdf->maxLineWidth = 0;
646        $cell_pdf->x=$cell_pdf->lMargin;
647        $cell_pdf->y=0;
648        $cell_pdf->CurrentTag = $this->CurrentTag;
649       
650        if ($max_width){
651                $cell_pdf->rMargin=$cell_pdf->w-$cell_pdf->x-$max_width-$cellmargin;
652        }
653        $cell_pdf -> WriteHTML($htmlContent,$LineFeedHeight);
654        if($cell_pdf->x>$cell_pdf->lMargin)
655                $cell_pdf->Ln($LineFeedHeight);
656       
657        $width = $cell_pdf->maxLineWidth-$cell_pdf->lMargin;
658        $height = $cell_pdf->y;
659       
660        $width += $cellmargin;
661        $height += $cellmargin;
662        return array($width,$height);
663}
664
665function OutputCell($width,$height,$htmlContent,$border=0,$LineFeedHeight=0,$align='',$fill=0,$cellmargin=3){
666                // dessiner la cellule vide
667                $this->Cell($width, $height, '', $border, 0, $align, $fill);
668                // on note la position apres la cellule
669                $x = $this->x; $y = $this->y;
670                $lmargin = $this->lMargin;
671                $rmargin = $this->rMargin;
672               
673                // on se remet en debut de cellule
674                $this->x-=$width;
675                $this->x = $this->x+$cellmargin/2;
676                $this->lMargin = $this->x; // pour que les retour ligne se fassent correctement dans la cellule
677                $this->rMargin = $this->w-$this->x-$width+$cellmargin/2; 
678               
679                $this -> WriteHTML($htmlContent,$LineFeedHeight);
680                // on se remet a la fin de la cellule
681                $this->x = $x; $this->y = $y;
682                $this->lMargin = $lmargin;
683                $this->rMargin = $rmargin;
684}
685
686function BlocShow($left_margin,$htmlContent,$border=0,$LineFeedHeight){
687        $this->Ln($LineFeedHeight);
688        $this->x=$this->lMargin+$left_margin;
689        $y = $this->y;
690        $width = $this->w-$this->rMargin-$this->x;
691        list($width2,$height) = $this->CellSize($htmlContent,$this->FontFamily,$this->FontSizePt,$LineFeedHeight,3,$width);
692        $this->OutputCell($width,$height,$htmlContent,$border,$LineFeedHeight);
693        $this->y = $y+$height;
694        $this->Ln($LineFeedHeight);
695}
696
697function TableShow($align,$LineFeedHeight)
698{
699        // Calcul de la taille de police optimale
700        // Le calcul ne l'est pas, lui ;-)
701        $oldFontSizePt=$this->FontSizePt;
702        $oldFontFamily=$this->FontFamily;
703
704        $tableFontFamily='helvetica';
705        $cellmargin=3.0;                // pifomètre : un peu de marge sur la largeur de cellule
706        $wrwi=$this->w - $this->lMargin - $this->rMargin;
707//-----------
708        $tableFontSize=10;
709        $TableWidth = 1.01*$wrwi;
710        $max_width=0;
711        $min_font_size=6.0; // mis à 6, 5 était vraiment petit
712        $maxiter = 10;
713        do {
714                $tableFontSize = $tableFontSize *min(1.0,$wrwi/$TableWidth)*0.99; // 0.99 pour converger plus vite
715               
716                $fixed_width= ($tableFontSize<$min_font_size) || ($maxiter==1) || ($TableWidth<=$wrwi);
717                if ($fixed_width)
718                        $coeff=min(1.0,$wrwi/$TableWidth);
719                               
720                $tableFontSize = max($min_font_size,$tableFontSize);
721                // on boucle sur la taille de police tant que la largeur du tableau ne rentre pas dans la page
722               
723                // remise à zéro des largeurs de colonnes
724                foreach ($this->columnProp as $i=>$cprop)
725                        if ($fixed_width)       $this->columnProp[$i]['w']=$this->columnProp[$i]['w']*$coeff;// redimenssioner la largeur de la colonne
726                        else    $this->columnProp[$i]['w']=0.0;
727                foreach($this->tableContent as $j=>$row)
728                        $this->lineProp[$j]['h']=0.0;
729               
730                // on passe toutes les cellules du tableau en revue
731                // de façon à calculer la largeur max de chaque colonne pour la taille de police courante
732                foreach($this->tableContent as $j=>$row) {
733                        foreach($row as $i=>$cell) {
734                                $htmlContent = $cell['content']."<br />";
735                                if ($this->tableContent[$j][$i]['TH']) {
736                                        $htmlContent="<B>$htmlContent</B>";
737                                }
738                                list($width,$height)=$this->CellSize($htmlContent,$tableFontFamily,$tableFontSize,$LineFeedHeight,$cellmargin,$fixed_width?$this->columnProp[$i]['w']:0);
739                               
740                                if (!$fixed_width)
741                                        $this->columnProp[$i]['w'] = max($this->columnProp[$i]['w'],$width);
742                                $this->lineProp[$j]['h'] = max($this->lineProp[$j]['h'],$height)+0.3;
743                        }
744                }
745                // Repris de CalcWidth : calcul de la largeur de la table
746                $TableWidth=0.0;
747                foreach($this->columnProp as $col) {
748                        $TableWidth += $col['w'];
749                }
750        } while (!$fixed_width && $maxiter--);
751
752        //-----------
753        //      Envoi du tableau dans le flux PDF
754        //-----------
755
756        $this->SetFont($tableFontFamily, '', $tableFontSize);
757        //Calcule l'abscisse du tableau
758        if($align=='C') 
759                $this->TableX=max(($this->w-$TableWidth)/2, 0);
760        elseif($align=='R')
761                $this->TableX=max($this->w-$this->rMargin-$TableWidth, 0);
762        else
763                $this->TableX=$this->lMargin;
764
765        $ci=0;  # flip-flop pour couleur de fond de ligne
766        foreach($this->tableContent as $j=>$row) {
767                $this->SetX($this->TableX);
768                $fill = !empty($this->RowColors[$ci]);
769                if ($fill) {
770                        $this->SetFillColor($this->RowColors[$ci][0],
771                                                                $this->RowColors[$ci][1],
772                                                                $this->RowColors[$ci][2]);
773                }
774               
775                foreach($this->tableContent[$j] as $i=>$cell) {
776                        if ($this->tableContent[$j][$i]['TH'] == true) {
777                                $this->SetFont($tableFontFamily, 'B', $tableFontSize);
778                                $this->SetFillColor(255, 255, 0);       // jaune
779                                $fill=1;
780                        }
781                        $this->OutputCell(
782                                $this->columnProp[$i]['w'],
783                                $this->lineProp[$j]['h'],
784                                $cell['content'],
785                                1,
786                                $LineFeedHeight,
787                                $this->columnProp[$i]['a'],
788                                $fill,0);
789                       
790                        if ($this->tableContent[$j][$i]['TH']) {
791                                $this->SetFont('', '', $tableFontSize);
792                                $this->SetFillColor(255);       // blanc
793                        }
794                }
795                $ci=1-$ci;
796                $this->maxLineWidth = max($this->maxLineWidth,$this->x);
797                $this->Ln($this->lineProp[$j]['h']);
798        }
799
800        $this->SetFont($oldFontFamily, '', $oldFontSizePt);
801        $this->Ln($LineFeedHeight);
802}
803       
804// Efface le fichier de dump
805function InitDumpFile()
806{
807        @unlink(DUMP_FILE_FULL_PATH_NAME);
808}
809
810
811// trace une chaîne dans un fichier
812function Dump($String)
813{
814        if ($f = @fopen(DUMP_FILE_FULL_PATH_NAME,"a"))
815    {
816                @fwrite($f,$String);
817        @fwrite($f,"\n");
818                @fclose($f);
819    }
820}
821
822// trace un tableau dans un fichier
823function DumpArray($String,$Array)
824{
825        $Result=print_r($Array,true);
826       
827        if ($f = @fopen(DUMP_FILE_FULL_PATH_NAME,"a"))
828    {
829                @fwrite($f,$String);
830                @fwrite($f,"\n\n");
831        if(@fwrite($f,$Result))
832        {
833            @fclose($f);
834        }
835    }
836        $Array.reset();
837}
838
839}       // class PDF extends FPDF
840
841?>
Note: See TracBrowser for help on using the repository browser.