noalyss Version-9
pdf_core.class.php
Go to the documentation of this file.
1<?php
2/*
3 * This file is part of NOALYSS.
4 *
5 * NOALYSS is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * NOALYSS is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with NOALYSS; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18*/
19
20// Copyright Author Dany De Bontridder danydb@aevalys.eu
21
22/*!
23 * \file
24 * \brief API for creating PDF, unicode, based on tfpdf
25 *@see TFPDF
26 */
27/*!
28 * \class Cellule
29 * \brief A Cellule is a cell to print
30 *@see TFPDF
31 */
32require_once NOALYSS_INCLUDE.'/tfpdf/tfpdf.php';
33class Cellule {
34 var $width;
36 var $text;
39 var $align;
40 var $fill;
41 var $link;
42 var $type;
44 {
45 $this->width=$w ;
46 $this->height=$h ;
47 $this->text=$txt;
48 $this->border=$border;
49 $this->new_line=$ln;
50 $this->align=$align;
51 $this->fill=$fill;
52 $this->link=$link;
53 $this->type=$type;
54 return $this;
55 }
56}
57/*!
58 * \class PDF_Core
59 * \brief API for creating PDF, unicode, based on tfpdf
60 *@see TFPDF
61 */
62class PDF_Core extends TFPDF
63{
64
65
66
67 private $cells=array();
68
69 public function __construct ( $orientation = 'P', $unit = 'mm', $format = 'A4')
70 {
71 $this->bigger=0;
72
73 parent::__construct($orientation, $unit, $format);
74 $this->AddFont('DejaVu','','DejaVuSans.ttf',true);
75 $this->AddFont('DejaVu','I','DejaVuSans-Oblique.ttf',true);
76 $this->AddFont('DejaVu','B','DejaVuSans-Bold.ttf',true);
77 $this->AddFont('DejaVu','BI','DejaVuSans-BoldOblique.ttf',true);
78 $this->AddFont('DejaVuCond','','DejaVuSansCondensed.ttf',true);
79 $this->AddFont('DejaVuCond','B','DejaVuSansCondensed-Bold.ttf',true);
80 $this->AddFont('DejaVuCond','I','DejaVuSansCondensed-Oblique.ttf',true);
81 $this->AddFont('DejaVuCond','BI','DejaVuSansCondensed-BoldOblique.ttf',true);
82
83
84
85 $this->cells=array();
86 }
87 function get_margin_left()
88 {
89 return $this->lMargin;
90 }
92 {
93 return $this->bMargin;
94
95 }
96 function get_margin_top()
97 {
98 return $this->tMargin;
99 }
101 {
102 return $this->rMargin;
103 }
105 {
106 return $this->DefOrientation;
107 }
108 function get_unit()
109 {
110 return $this->k;
111 }
112 function get_page_size()
113 {
114 return $this->DefPageSize;
115 }
116 /**
117 * Count the number of rows a p_text will take for a multicell
118 * @param $p_text String
119 * @param $p_colSize size of the column in User Unit
120 */
121 private function count_nb_row($p_text,$p_colSize)
122 {
123 // If colSize is bigger than the size of the string then it takes 1 line
124 if ( $this->GetStringWidth($p_text) <= $p_colSize) return 1;
125 $nRow=0;
126 $aWords=explode(' ',$p_text);
127 $nb_words=count($aWords);
128 $string="";
129
130 for ($i=0;$i < $nb_words;$i++){
131 // Concatenate String with current word + a space
132 $string.=$aWords[$i];
133
134 // if there is a word after add a space
135 if ( $i+1 < $nb_words) $string.=" ";
136
137 // Compute new size and compare to the colSize
138 if ( $this->GetStringWidth($string) >= $p_colSize) {
139 // If the size of the string if bigger than we add a row, the current
140 // word is the first word of the next line
141 $nRow++;
142 $string=$aWords[$i];
143 }
144 }
145 $nRow++;
146 return $nRow;
147
148
149
150 }
151 /**
152 * Check if a page must be added due a MultiCell
153 * @return boolean
154 */
155 private function check_page_add()
156 {
157 // break on page
158 $size=count($this->cells);
159 for ($i=0;$i < $size ; $i++)
160 {
161 if ($this->cells[$i]->type == 'M' )
162 {
163 /**
164 * On doit calculer si le texte dépasse la texte et compter le
165 * nombre de lignes que le texte prendrait. Ensuite il faut
166 * faire un saut de page (renvoit true) si dépasse
167 */
168
169 $sizetext=$this->GetStringWidth($this->cells[$i]->text);
170
171 // if text bigger than column width then check
172
173 $y=$this->GetY();
174 $nb_row=$this->count_nb_row($this->cells[$i]->text, $this->cells[$i]->width);
175 $height=$this->cells[$i]->height*$nb_row;
176
177 // If the text is bigger than a sheet of paper then return false
178 if ($height >= $this->h) return false;
179
180 if ( $y + $height > ($this->h - $this->bMargin -7 ))
181 return true;
182
183 }
184 }
185 return false;
186 }
187
188 /**
189 * @brief print the current array of cell and reset it , if different colors are set on the same row
190 * you have to print it before changing
191 *@code
192 * // fill red , text white
193 * $this->SetFillColor(255,0,0);
194 * $this->SetTextColor(255,255,255);
195 * $this->write_cell(15,5,"PRICE",0,0,'R',fill:true);
196 *
197 * // print the cell without a linefeed
198 * $this->print_row();
199 *
200 * // text in black on green
201 *
202 * $this->SetTextColor(0,0,0);
203 * $this->SetFillColor(0,255,0);
204 *
205 * $this->write_cell(15,5,nbm($other['price']),0,0,'R');
206 *@endcode
207 * @see TFPDF::SetTextColor()
208 * @see TFPDF::SetFillColor()
209 * @see TFPDF::SetFontSize()
210 * @return void
211 */
212 protected function print_row()
213 {
214 static $e=0;
215 $e++;
216 if ( $this->check_page_add() == true ) $this->AddPage ();
217 $this->bigger=0;
218 $size=count($this->cells);
219 $cell=$this->cells;
220 if ($size == 0 )return;
221 for ($i=0;$i < $size ; $i++)
222 {
223 $a=$cell[$i];
224 $a->text= noalyss_str_replace("\\", "", $a->text);
225 switch ($a->type)
226 {
227 case "M":
228 $x_m=$this->GetX();
229 $y_m=$this->GetY();
230 parent::MultiCell(
231 $a->width,
232 $a->height,
233 $a->text,
234 $a->border,
235 $a->align,
236 $a->fill
237 );
238 $x_m=$x_m+$a->width;
239 $tmp=$this->GetY()-$y_m;
240 if ( $tmp > $this->bigger) $this->bigger=$tmp;
241 $this->SetXY($x_m,$y_m);
242 break;
243
244 case "C":
245
246 parent::Cell( $a->width,
247 $a->height,
248 $a->text,
249 $a->border,
250 $a->new_line,
251 $a->align,
252 $a->fill,
253 $a->link);
254 break;
255
256 default:
257 break;
258 }
259 }
260 $this->cells=array();
261 }
262 private function add_cell(Cellule $Ce)
263 {
264 $size=count($this->cells);
265 $this->cells[$size]=$Ce;
266
267 }
268 function write_cell ($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='')
269 {
270 $this->add_cell(new Cellule($w,$h,$txt,$border,$ln,$align,$fill,$link,'C'));
271
272 }
273 function LongLine($w,$h,$txt,$border=0,$align='',$fill=false)
274 {
275 $this->add_cell(new Cellule($w,$h,$txt,$border,0,$align,$fill,'','M'));
276
277 }
278 /**
279 * Print all the cell stored and call Ln (new line)
280 * @param int $p_step
281 */
282
283 function line_new($p_step=null){
284 $this->print_row();
285 if ( $this->bigger==0)
286 parent::Ln($p_step);
287 else
288 parent::Ln($this->bigger);
289 $this->bigger=0;
290 }
291 /**
292 * @brief If the step is even then return 1 and set the backgroup color to blue , otherwise
293 * returns 0, and set the background color to white
294 * It is use to compute alternated colored row , it the parameter fill in write_cell and
295 * cell
296 * @see PDF:write_cell
297 * @see TPDF:cell
298 *
299 */
300 function is_fill($p_step)
301 {
302 if ($p_step % 2 == 0) {
303 $this->SetFillColor(239, 239, 255);
304 $fill = 1;
305 } else {
306 $this->SetFillColor(255, 255, 255);
307 $fill = 0;
308 }
309 return $fill;
310 }
311
312
313
314}
315
noalyss_str_replace($search, $replace, $string)
Definition: ac_common.php:1553
return false Description text align
h( $row[ 'oa_description'])
foreach($array as $idx=> $m) $w
$input_from type
Definition: balance.inc.php:65
A Cellule is a cell to print.
__construct($w, $h, $txt, $border, $ln, $align, $fill, $link, $type)
API for creating PDF, unicode, based on tfpdf.
line_new($p_step=null)
Print all the cell stored and call Ln (new line)
is_fill($p_step)
If the step is even then return 1 and set the backgroup color to blue , otherwise returns 0,...
check_page_add()
Check if a page must be added due a MultiCell.
LongLine($w, $h, $txt, $border=0, $align='', $fill=false)
write_cell($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='')
count_nb_row($p_text, $p_colSize)
Count the number of rows a p_text will take for a multicell.
print_row()
print the current array of cell and reset it , if different colors are set on the same row you have t...
__construct( $orientation='P', $unit='mm', $format='A4')
add_cell(Cellule $Ce)
if(count($array)==0) $size
$desc width