source: trunk/client/inc/hpdf/_mypdf/01_fpdf_bookmark.class.php@ 2

Last change on this file since 2 was 2, checked in by root, 15 years ago

importo il progetto

File size: 4.5 KB
Line 
1<?php
2/*************************************************************************
3 * http://www.fpdf.org/en/script/script1.php
4 *
5 * @author Olivier
6 *
7 * This extension adds bookmark support. The method to add a bookmark is:
8 *
9 * function Bookmark(string txt [, int level [, float y]])
10 *
11 * txt: the bookmark title.
12 * level: the bookmark level (0 is top level, 1 is just below, and so on).
13 * y: the y position of the bookmark destination in the current page. -1 means the current position. Default value: 0.
14 *
15 * The title must be encoded in ISO Latin-1.
16 ************************************************************************/
17/*************************************************************************
18 * http://www.fpdf.org/en/script/script13.php
19 *
20 * @author Min's
21 *
22 * This class prints an index from the created bookmarks.
23 ************************************************************************/
24
25if (!defined('__CLASS_FPDF_BOOKMARK__'))
26{
27 define('__CLASS_FPDF_BOOKMARK__', true);
28
29require_once(dirname(__FILE__).'/00_fpdf_codebar.class.php');
30
31 class FPDF_BookMark extends FPDF_Codebar
32 {
33 var $outlines=array();
34 var $OutlineRoot;
35
36 function FPDF_BookMark($orientation='P',$unit='mm',$format='A4')
37 {
38 $this->FPDF_Codebar($orientation,$unit,$format);
39
40 }
41
42 function Bookmark($txt, $level=0, $y=0)
43 {
44 if($y==-1) $y=$this->GetY();
45 $this->outlines[]=array('t'=>$txt, 'l'=>$level, 'y'=>($this->h-$y)*$this->k, 'p'=>$this->PageNo());
46 }
47
48 function _putbookmarks()
49 {
50 $nb=count($this->outlines);
51 if($nb==0) return;
52 $lru=array();
53 $level=0;
54 foreach($this->outlines as $i=>$o)
55 {
56 if($o['l']>0)
57 {
58 $parent=$lru[$o['l']-1];
59 //Set parent and last pointers
60 $this->outlines[$i]['parent']=$parent;
61 $this->outlines[$parent]['last']=$i;
62 if($o['l']>$level)
63 {
64 //Level increasing: set first pointer
65 $this->outlines[$parent]['first']=$i;
66 }
67 }
68 else
69 $this->outlines[$i]['parent']=$nb;
70
71 if($o['l']<=$level and $i>0)
72 {
73 //Set prev and next pointers
74 $prev=$lru[$o['l']];
75 $this->outlines[$prev]['next']=$i;
76 $this->outlines[$i]['prev']=$prev;
77 }
78 $lru[$o['l']]=$i;
79 $level=$o['l'];
80 }
81
82 //Outline items
83 $n=$this->n+1;
84 foreach($this->outlines as $i=>$o)
85 {
86 $this->_newobj();
87 $this->_out('<</Title '.$this->_textstring($o['t']));
88 $this->_out('/Parent '.($n+$o['parent']).' 0 R');
89 if(isset($o['prev']))
90 $this->_out('/Prev '.($n+$o['prev']).' 0 R');
91 if(isset($o['next']))
92 $this->_out('/Next '.($n+$o['next']).' 0 R');
93 if(isset($o['first']))
94 $this->_out('/First '.($n+$o['first']).' 0 R');
95 if(isset($o['last']))
96 $this->_out('/Last '.($n+$o['last']).' 0 R');
97 $this->_out(sprintf('/Dest [%d 0 R /XYZ 0 %.2F null]',1+2*$o['p'],$o['y']));
98 $this->_out('/Count 0>>');
99 $this->_out('endobj');
100 }
101
102 //Outline root
103 $this->_newobj();
104 $this->OutlineRoot=$this->n;
105 $this->_out('<</Type /Outlines /First '.$n.' 0 R');
106 $this->_out('/Last '.($n+$lru[0]).' 0 R>>');
107 $this->_out('endobj');
108 }
109
110 function _putresources()
111 {
112 parent::_putresources();
113 $this->_putbookmarks();
114 }
115
116 function _putcatalog()
117 {
118 parent::_putcatalog();
119 if(count($this->outlines)>0)
120 {
121 $this->_out('/Outlines '.$this->OutlineRoot.' 0 R');
122 $this->_out('/PageMode /UseOutlines');
123 }
124 }
125
126 function CreateIndex($titre = 'Index', $size1 = 20, $size2 = 15)
127 {
128 $this->Bookmark($titre, 0, -1);
129
130 //Index title
131 $this->SetFontSize($size1);
132 $this->Cell(0,5,$titre,0,1,'C');
133 $this->SetFontSize($size2);
134 $this->Ln(10);
135
136 $size=sizeof($this->outlines);
137 $PageCellSize=$this->GetStringWidth('p. '.$this->outlines[$size-1]['p'])+2;
138 for ($i=0;$i<$size;$i++)
139 {
140 //Offset
141 $level=$this->outlines[$i]['l'];
142 if($level>0) $this->Cell($level*8);
143
144 //Caption
145 $str=$this->outlines[$i]['t'];
146 $strsize=$this->GetStringWidth($str);
147 $avail_size=$this->w-$this->lMargin-$this->rMargin-$PageCellSize-($level*8)-4;
148 while ($strsize>=$avail_size)
149 {
150 $str=substr($str,0,-1);
151 $strsize=$this->GetStringWidth($str);
152 }
153 $this->Cell($strsize+2,$this->FontSize+2,$str);
154
155 //Filling dots
156 $w=$this->w-$this->lMargin-$this->rMargin-$PageCellSize-($level*8)-($strsize+2);
157 $nb=$w/$this->GetStringWidth('.');
158 $dots=str_repeat('.',$nb);
159 $this->Cell($w,$this->FontSize+2,$dots,0,0,'R');
160
161 //Page number
162 $this->Cell($PageCellSize,$this->FontSize+2,'p. '.$this->outlines[$i]['p'],0,1,'R');
163 }
164 }
165 }
166}
167?>
Note: See TracBrowser for help on using the repository browser.