Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 56
CRAP
0.00% covered (danger)
0.00%
0 / 384
TPDF
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 56
24492
0.00% covered (danger)
0.00%
0 / 384
 __construct
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 6
 show
0.00% covered (danger)
0.00%
0 / 1
182
0.00% covered (danger)
0.00%
0 / 28
 getBaseDir
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 4
 AddPage
0.00% covered (danger)
0.00%
0 / 1
30
0.00% covered (danger)
0.00%
0 / 19
 setPageBorder
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 getPageBorder
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 1
 addColumn
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 3
 getColumns
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 getColumn
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 setData
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 getData
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 prepare
0.00% covered (danger)
0.00%
0 / 1
20
0.00% covered (danger)
0.00%
0 / 22
 printRows
0.00% covered (danger)
0.00%
0 / 1
90
0.00% covered (danger)
0.00%
0 / 19
 Header
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 4
 Footer
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 3
 setRowLineHeight
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 getRowLineHeight
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 1
 setWaterMark
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 getWaterMark
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 7
 clearColumns
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 row
0.00% covered (danger)
0.00%
0 / 1
182
0.00% covered (danger)
0.00%
0 / 62
 CheckPageBreak
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 2
 NbLines
0.00% covered (danger)
0.00%
0 / 1
110
0.00% covered (danger)
0.00%
0 / 32
 setRowWidths
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 getRowWidths
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 3
 setRowFillColors
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 getRowFillColors
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 setRowFontColors
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 getRowFontColors
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 setRowFontStyles
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 getRowFontStyles
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 setRowAligns
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 getRowAligns
0.00% covered (danger)
0.00%
0 / 1
20
0.00% covered (danger)
0.00%
0 / 5
 setRowFieldNames
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 getRowFieldNames
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 setOnDrawCell
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 getOnDrawCell
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 RotatedText
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 4
 Rotate
0.00% covered (danger)
0.00%
0 / 1
30
0.00% covered (danger)
0.00%
0 / 13
 printGridHeader
0.00% covered (danger)
0.00%
0 / 1
42
0.00% covered (danger)
0.00%
0 / 15
 getRowMaxWidth
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 6
 setVar
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 3
 getVar
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 3
 setTopMargin
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 4
 getTopMargin
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 HexToRGB
0.00% covered (danger)
0.00%
0 / 1
90
0.00% covered (danger)
0.00%
0 / 18
 translateColor
0.00% covered (danger)
0.00%
0 / 1
30
0.00% covered (danger)
0.00%
0 / 22
 setHeaderFillColors
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 getHeaderFillColors
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 3
 setHeaderFontColors
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 getHeaderFontColors
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 3
 getFontSize
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 getCurrentDrawColor
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 6
 getCurrentFillColor
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 6
 getCurrentTextColor
0.00% covered (danger)
0.00%
0 / 1
20
0.00% covered (danger)
0.00%
0 / 8
 getMixColor
0.00% covered (danger)
0.00%
0 / 1
20
0.00% covered (danger)
0.00%
0 / 11
1<?php
2/*
3 * Formdin Framework
4 * Copyright (C) 2012 Ministério do Planejamento
5 * Criado por Luís Eugênio Barbosa
6 * Essa versão é um Fork https://github.com/bjverde/formDin
7 *
8 * ----------------------------------------------------------------------------
9 * This file is part of Formdin Framework.
10 *
11 * Formdin Framework is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License version 3
13 * as published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License version 3
21 * along with this program; if not,  see <http://www.gnu.org/licenses/>
22 * or write to the Free Software Foundation, Inc., 51 Franklin Street,
23 * Fifth Floor, Boston, MA  02110-1301, USA.
24 * ----------------------------------------------------------------------------
25 * Este arquivo é parte do Framework Formdin.
26 *
27 * O Framework Formdin é um software livre; você pode redistribuí-lo e/ou
28 * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação
29 * do Software Livre (FSF).
30 *
31 * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA
32 * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou
33 * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português
34 * para maiores detalhes.
35 *
36 * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título
37 * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/>
38 * ou escreva para a Fundação do Software Livre (FSF) Inc.,
39 * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
40 */
41
42/**
43 * extensão da classe FPDF v1.52 original adicionado o metodo row() e watermark
44 *
45 */
46// artificio para encontrar a pasta base
47//$e = new TElement();
48//define( 'FPDF_FONTPATH', $e->getBase() . 'lib_fpdf181/font/' );
49//require_once( $e->getBase() . 'lib_fpdf181/fpdf.php' );
50
51class TPDF extends FPDF
52{
53
54    private $baseDir = null;
55    private $pageBorder = false; // adicionar borda na página
56    private $colums = null;
57    private $data = null;
58    private $waterMark = null;
59    private $angle = 0;
60    private $vars = null;
61    //--- Parametros para o metodo Row()
62    private $rowWidths;
63    private $rowFillColors;
64    private $rowFontColors;
65    private $rowFontStyles;
66    private $rowAligns;
67    private $rowFieldNames;
68    private $rowLineHeight;
69    private $onDrawRow;
70    private $onDrawCell;
71    private $flagPrintHeader;
72
73    private $headerFillColors;
74    private $headerFontColors;
75    private $headerFontStyles;
76
77    /**
78     * Classe para criação de relatórios no formato PDF
79     *
80     * @param string $strOrientation 01 : P or L
81     * @param string $strUnit        02 :sunidade 
82     * @param string $strFormat
83     * @param string $strFontFamily
84     * @param integer $intFontSize
85     */
86    public function __construct( $strOrientation = 'P'
87                               , $strUnit = 'mm'
88                               , $strFormat = 'A4'
89                               , $strFontFamily = 'arial'
90                               , $intFontSize = 8 )
91    {
92        parent::__construct( $strOrientation, $strUnit, $strFormat );
93        $this->SetFont( ( is_null( $strFontFamily ) ? 'Arial' : $strFontFamily ), '', ( is_null( $intFontSize ) ? 8 : $intFontSize ) );
94        $this->SetFillColor( 255 ); // fundo branco
95        $this->AliasNbPages();
96        $this->flagPrintHeader=true;
97    }
98
99    /**
100     * Gerar o pdf e enviar para o browser ou retornar o arquivo gerado
101     *
102     * @param string$fileName
103     * @param boolean $boolPrint
104     * @return mixed
105     */
106    public function show( $strOutputFileName = null, $boolPrint = true )
107    {
108        // excluir os arquivos com mais de 5 minutos de vida
109        $t = time();
110        $tmpDir = str_replace( '//', '/', $this->getBaseDir() . "/tmp/" );
111
112        if ( !file_exists( $tmpDir ) )
113        {
114            @mkdir( $tmpDir, '0775', true );
115        }
116
117        if ( !file_exists( $tmpDir ) )
118        {
119            die( 'Diretório para arquivos temporário ' . $tmpDir
120                . ' deve ser criado com permissão de leitura/escrita.' );
121        }
122
123        if ( !is_writable( $tmpDir ) )
124        {
125            die( 'Diretório ' . $tmpDir . ' não está com permissão de leitura/escrita' );
126        }
127
128        if ( $h = opendir( $tmpDir ) )
129        {
130            while( $file = readdir( $h ) )
131            {
132                if ( substr( $file, 0, 3 ) == 'tmp' && substr( $file, -4 ) == '.pdf' )
133                {
134                    $path = $tmpDir . $file;
135
136                    if ( $t - filemtime( $path ) > 300 )@unlink( $path );
137                }
138            }
139            closedir( $h );
140        }
141
142        if ( is_null( $strOutputFileName ) )
143        {
144            //$file = tempnam(getcwd() . "/" . $this->getBaseDir() . "tmp", 'tmp');
145            $file = $tmpDir . 'tmp_' . date( 'ymdhis' ) . '.pdf';
146        //rename($file, $file . ".pdf");
147        //$file.=".pdf";
148        //print '2)'.$file.'<br>';
149        }
150        else
151        {
152            //$file = getcwd() . "/" . $this->getBaseDir() . '/tmp/' . $strOutputFileName;
153            $file = $tmpDir . $strOutputFileName;
154        //print '3)'.$file.'<br>';
155
156        }
157
158        if ($boolPrint){
159            if ($boolPrint==true){
160                $dest = 'F';
161            }else{
162                $dest = 'D';
163            }
164        }else{
165            $dest = 'F';
166        }
167        
168        $this->Output( $dest, $file );
169        $file = $tmpDir . basename( $file );
170
171        if ( $boolPrint ) {
172            header( "Location: $file" );
173        }
174        return $file;
175    }
176
177    /**
178     * Retorna o diretório da pasta base/ da aplicação
179     *
180     */
181    public function getBaseDir()
182    {
183        if ( is_null( $this->baseDir ) )
184        {
185            $objTemp = new TElement();
186            $this->baseDir = $objTemp->getBase();
187        }
188        return $this->baseDir;
189    }
190
191    /**
192     * Sobrescrita da função addPage para possibilitar desenhar borda na página
193     *
194     * @param string $strOrientation P/L
195     * @param boolean $boolBorder
196     */
197    function AddPage( $strOrientation='', $size='', $rotation=0 ) 
198    {
199        $strOrientation = ( is_null($strOrientation) ? '' : $strOrientation );
200        parent::AddPage( $strOrientation );
201
202        if ( $this->getPageBorder()) {
203            $yy = $this->GetY();
204            $h = ( $this->h - $yy - $this->bMargin );
205            $this->cell( 0, $h, '', 1, 0, 'L', 0 );
206            $this->SetY( $yy );
207        }
208
209           if ($str = $this->getWaterMark())
210        {
211            $currentFontSize  = $this->FontSizePt;
212            $currentTextColor = $this->getCurrentTextColor();  ;
213            $this->SetFont('Arial', 'B', 50);
214            $this->SetTextColor(255, 192, 203);
215            if ($this->w > $this->h) {
216                $angle = 35;
217            } else {
218                $angle = 55;
219            }
220            $this->RotatedText($this->lMargin, $this->h, $str, $angle);
221            $this->SetFont(null,null,$currentFontSize);
222            $this->SetTextColor($currentTextColor);
223        }
224    }
225
226    /**
227     * Habilitar/desabilitar a impressão da borda na página
228     *
229     * @param mixed $boolNewValue
230     */
231    function setPageBorder( $boolNewValue = null )
232    {
233        $this->pageBorder = $boolNewValue;
234    }
235
236    /**
237     * Retorna o valor da propriedade pageBorder para impressão da borda na página
238     *
239     */
240    function getPageBorder()
241    {
242        return $this->pageBorder===true ? true : false;
243    }
244
245    /****
246     * Add New Column in Grid. Need to use method printRows for show column in pdf.
247     * 
248     * Adicionar nova coluna na grade. Precisa usar o método printRows para mostrar coluna em pdf
249     * 
250     * @param string $strHeader      - 1: ID column
251     * @param string $intWidth       - 2: Size column
252     * @param string $strAlign       - 3: Align column, Values L = Left ,C= Center,R= ,J=justified
253     * @param string $strFieldName   - 4: Label column head
254     * @param string $hexFillColor   - 5: Cor do fundo, hexadecimal
255     * @param string $strFontStyle   - 6: Style da font: I, B
256     * @param string $intFontSize    - 7: Tamanho da fonte
257     * @param string $hexFontColor   - 8: Cor em HexaDecimal
258     * @param string $strFontFamily  - 9: 
259     * @return TPDFColumn
260     */
261    public function addColumn( $strHeader = null
262                             , $intWidth = null
263                             , $strAlign = null
264                             , $strFieldName = null
265                             , $hexFillColor = null
266                             , $strFontStyle = null
267                             , $intFontSize = null
268                             , $hexFontColor = null
269                             , $strFontFamily = null 
270                             )
271    {
272        $column = new TPDFColumn( $strHeader
273                                , $intWidth
274                                , $strAlign
275                                , $strFieldName
276                                , $hexFillColor
277                                , $strFontStyle
278                                , $intFontSize
279                                , $hexFontColor
280                                , $strFontFamily );
281        $this->colums[] = $column;
282        return $column;
283    }
284
285    function getColumns()
286    {
287        return $this->colums;
288    }
289
290    function getColumn( $intColumn = null )
291    {
292        return $this->colums[ $intColumn ];
293    }
294
295    function setData( $arrDados = null )
296    {
297        $this->data = $arrDados;
298        return $this;
299    }
300
301    function getData()
302    {
303        return $this->data;
304    }
305
306    //----------------------------------------------------------------------------------------------------
307    protected function prepare()
308    {
309        if ( !is_array( $this->getData() ) )
310        {
311            return;
312        }
313
314        if ( !$this->getColumns() )
315        {
316            die( 'Nenhuma coluna adicionada!' );
317        }
318
319        $aWidths = null;
320        $aAligns = null;
321        $aFontColors = null;
322        $aFillColors = null;
323        $aFontStyles = null;
324        $aFieldNames = null;
325
326        foreach( $this->getColumns() as $k => $oCol )
327        {
328            $aWidths[] = $oCol->getWidth();
329            $aAligns[] = $oCol->getAlign();
330            $aFillColors[] = $oCol->getFillColor();
331            $aFontStyles[] = $oCol->getFontStyle();
332            $aFieldNames[] = $oCol->getFieldName();
333        }
334        $this->setRowWidths( $aWidths );
335        $this->setRowFillColors( $aFillColors );
336        $this->setRowFontColors( $aFontColors );
337        $this->setRowAligns( $aAligns );
338        $this->setRowFieldNames( $aFieldNames );
339    }
340
341    /**
342     * criar a grid do pdf
343     *
344     * @param [type] $newPage
345     * @param [type] $strPageOrientation
346     * @return void
347     */
348    public function printRows( $newPage = null, $strPageOrientation = null )
349    {
350        //$this->clearColumns();
351        
352        if ( !$this->getData() )
353        {
354            return;
355        }
356        $cloneColumns = null;
357
358        // clonar as colunas para poder repetir a impressão do grid e exibir o mesmo
359        // resultado, caso o usuario altera alguma propriedade original no evento onDrawCell
360        if ( $this->getOnDrawCell() )
361        {
362            foreach( $this->getColumns() as $k => $objColumn )
363            {
364                $cloneColumns[] = clone $objColumn;
365            }
366        }
367        $this->prepare();
368        $res = $this->getData();
369
370        if ( $newPage || $this->PageNo() == 0 )
371        {
372            $this->AddPage( $strPageOrientation, $this->getPageBorder() );
373        }
374        $this->printGridHeader();
375
376        foreach( $res[ key( $res )] as $k => $v )
377        {
378            $aDados = array();
379
380            foreach( $this->getRowFieldNames() as $k1 => $v1 )
381            {
382                $aDados[] = $res[ $v1 ][ $k ];
383            }
384            $this->row( $aDados );
385        }
386
387        if ( !is_null( $cloneColumns ) )
388        {
389            $this->colums = $cloneColumns;
390        }
391    }
392
393    //----------------------------------------------------------------------------------------------------
394    public function Header()
395    {
396        if ( function_exists( 'cabecalho' ) )
397        {
398            call_user_func( 'cabecalho', $this );
399        }
400        // imprimir o cabeçalho do gride
401        $this->printGridHeader();
402    /*
403    if ($str = $this->getWaterMark())
404    {
405        $this->SetFont('Arial', 'B', 50);
406        $this->SetTextColor(255, 192, 203);
407        if ($this->w > $this->h) {
408            $angle = 35;
409        } else {
410            $angle = 55;
411        }
412        $this->RotatedText($this->lMargin, $this->h, $str, $angle);
413    }
414    */
415    }
416    public function Footer()
417    {
418        if ( function_exists( 'rodape' ) )
419        {
420            call_user_func( 'rodape', $this );
421        }
422    }
423    //----------------------------------------------------------------------------------------------------
424    public function setRowLineHeight( $intNewValue = null )
425    {
426        $this->rowLineHeight = $intNewValue;
427    }
428    public function getRowLineHeight()
429    {
430        return is_null( $this->rowLineHeight ) ? 4 : $this->rowLineHeight;
431    }
432    //----------------------------------------------------------------------------------------------------
433    public function setWaterMark( $strNewValue = null )
434    {
435        $this->waterMark = $strNewValue;
436    }
437    //----------------------------------------------------------------------------------------------------
438    public function getWaterMark()
439    {
440        $str = $this->waterMark;
441
442        if ( $this->w < $this->h )
443        {
444            $max = 57.085088888889;
445        }
446        else
447        {
448            $max = 53.156555555556;
449        }
450
451        while( $this->GetStringWidth( $str ) < $max )
452        {
453            $str = " " . $str . " ";
454        }
455        return $str;
456    }
457    //----------------------------------------------------------------------------------------------------
458    /**
459     * Clean coluns to new grid
460     * 
461     * Limpa as colunas para gerar novo gride
462     */    
463    public function clearColumns()
464    {
465        $this->colums = null;
466    }
467    //----------------------------------------------------------------------------------------------------
468    //----------------------------------------------------------------------------------------------------
469    /**
470     * Imprimir array de dados em colunas
471     *
472     * O parametro $margemInferior indica o limite inferior maximo que a função
473     * poderá utilizar para impressão da linha,  jogando o texto para a outra página se não couber.
474     *
475     * @param array $data
476     * @param int   $intBottomMarginSize
477     * @param mixed $mixFillColor
478     * @param mixed $mixFontColor
479     * @return void
480     */
481    function row( $data, $intBottomMarginSize = null, $mixFillColor = null, $mixFontColor = null )
482    {
483        $this->flagPrintHeader=true; // imprimir o cabecalho do grid
484        //Calculate the height of the row
485        $nb = 0;
486        $aNb = null;
487        $currentFontFamily= $this->FontFamily;
488        $currentFontStyle = $this->FontStyle;
489        $currentFontSize  = $this->FontSizePt;
490        $currentTextColor = $this->getCurrentTextColor();
491        $currentFillColor = $this->getCurrentFillColor();
492        //MessageHelper::logRecordSimple('FillColor linha 492: '.$currentFillColor);
493        $currentDrawColor = $this->getCurrentDrawColor();
494
495        // calcular o espaço que a linha vai ocupar na página
496        for( $i = 0; $i < count( $data ); $i++ ){
497            $oCol = $this->getColumn( $i );
498
499            if ( $this->getOnDrawCell() && function_exists( $this->getOnDrawCell() ) ){
500                //$params = (object) array('value'=>$data[$i],'colIndex'=>$i, 'data'=>$data );
501                //call_user_func( $this->getOnDrawCell(), $oCol, $params );
502                call_user_func( $this->getOnDrawCell(), $oCol, $data[ $i ], $i, $data );
503            }
504
505            //definir de volta o estilo, tamanho e cor da fonte da celula
506            $this->SetFont( $oCol->getFontFamily( $currentFontFamily )
507                          , $oCol->getFontStyle( $currentFontStyle )
508                          , $oCol->getFontSize( $currentFontSize ) 
509                          );
510
511            $aNb[ $i ] = $this->NbLines( $oCol->getWidth(), $data[ $i ] );
512            $nb = max( $nb, $aNb[ $i ] );
513        }
514
515        $lineHeight = $this->getRowLineHeight() * $nb;
516
517        //saltar página se a linha for ocupar mais que o limite informado.
518        if ( $intBottomMarginSize ){
519            if ( ( $this->GetY() + $lineHeight ) > $intBottomMarginSize ) {
520                $this->AddPage( $this->CurOrientation, $this->getPageBorder() );
521            }
522        }
523        //Issue a page break first if needed
524        $this->CheckPageBreak( $lineHeight );
525
526        //Draw the cells of the row
527        for( $i = 0; $i < count( $data ); $i++ ) {
528            $oCol = $this->getColumn( $i );
529            //definir o estilo, tamanho e cor da fonte da celula
530            $this->SetFont( $oCol->getFontFamily( $currentFontFamily )
531                          , $oCol->getFontStyle( $currentFontStyle )
532                          , $oCol->getFontSize( $currentFontSize )
533                          );
534            $this->SetFillColor( $currentFillColor );
535            $colWidth = $this->getRowWidths($i);
536            $textAlign = strtoupper($this->getRowAligns(null,$i));
537            $borda = 1;
538
539            //alterar a cor da fonte
540            $aCor = null;
541            $color= $oCol->getFontColor( $currentTextColor );
542            $aCor = $this->getMixColor($mixFontColor, $color);
543            if ( is_array( $aCor ) ) {
544                $this->SetTextColor( $aCor[ 'r' ], $aCor[ 'g' ], $aCor[ 'b' ] );
545            } else {
546                $this->SetTextColor( $aCor ); // Escala de cinza: 0 - 255
547            }
548
549            //cor de preenchimento da celula
550            $color= $oCol->getFillColor( $currentFillColor );
551            $aCor = $this->getMixColor($mixFillColor, $color);
552            if ( is_array( $aCor ) ){
553                $this->SetFillColor( $aCor[ 'r' ], $aCor[ 'g' ], $aCor[ 'b' ] );
554            }else{
555                $this->SetFillColor( $aCor ); // escala de cinza: 0 - 255
556            }
557            //Save the current position
558            $x = $this->GetX();
559            $y = $this->GetY();
560
561            $fill=1;
562            if( $oCol->getFillColor() == '255'  && is_null( $mixFillColor ) ) {
563                $fill = 0;
564            } else {
565                //Pintar o fundo da celula
566                $this->Rect( $x, $y, $colWidth, $lineHeight, 'FD' );
567            }
568
569            // centralizar verticalmente o texto na célula
570            if ( ( $c = $nb - $aNb[ $i ] ) > 0 ){
571                $c *= ( $this->getRowLineHeight() / 2 );
572                $this->SetXY( $x, ( $y + $c ) );
573            }
574            //Print the text
575            $this->MultiCell( $colWidth, $this->getRowLineHeight(), $data[ $i ], 0, $textAlign, $fill );
576
577            //Put the position to the right of the cell
578            $this->SetXY( $x + $colWidth, $y );
579
580            //eugenio - desenhar a borda
581            if ( $borda ){
582                $this->Rect( $x, $y, $colWidth, $lineHeight, 'D' );
583            }
584            // voltar as configurações padrão
585            $this->SetFont( $currentFontFamily, $currentFontStyle, $currentFontSize );
586            $this->SetFillColor( $currentFillColor );
587            $this->SetTextColor( $currentTextColor );
588            $this->SetDrawColor( $currentDrawColor );
589        }
590        //Go to the next line
591        $this->Ln( $lineHeight );
592    }
593
594    //---------------------------------------------------------------------------------
595    function CheckPageBreak( $h )
596    {
597        //If the height h would cause an overflow, add a new page immediately
598        if ( $this->GetY() + $h > $this->PageBreakTrigger )$this->AddPage( $this->CurOrientation );
599    }
600    //-------------------------------------------------------------------------------
601    function NbLines( $w, $txt )
602    {
603        //Computes the number of lines a MultiCell of width w will take
604        $cw = &$this->CurrentFont[ 'cw' ];
605
606        if ( $w == 0 )$w = $this->w - $this->rMargin - $this->x;
607        $wmax = ( $w - 2 * $this->cMargin ) * 1000 / $this->FontSize;
608        $s = str_replace( "\r", '', $txt );
609        $nb = strlen( $s );
610
611        if ( $nb > 0 and $s[ $nb - 1 ] == "\n" )$nb--;
612        $sep = -1;
613        $i = 0;
614        $j = 0;
615        $l = 0;
616        $nl = 1;
617
618        while( $i < $nb )
619        {
620            $c = $s[ $i ];
621
622            if ( $c == "\n" )
623            {
624                $i++;
625                $sep = -1;
626                $j = $i;
627                $l = 0;
628                $nl++;
629                continue;
630            }
631
632            if ( $c == ' ' )$sep = $i;
633            $l += $cw[ $c ];
634
635            if ( $l > $wmax )
636            {
637                if ( $sep == -1 )
638                {
639                    if ( $i == $j )$i++;
640                }
641                else $i = $sep + 1;
642                $sep = -1;
643                $j = $i;
644                $l = 0;
645                $nl++;
646            }
647            else $i++;
648        }
649        return $nl;
650    }
651    public function setRowWidths( $mixNewValue = null )
652    {
653        $this->rowWidths = $mixNewValue;
654        return $this;
655    }
656    public function getRowWidths($intCol=null)
657    {
658        if( ! is_null( $intCol ) )
659        {
660            return $this->rowWidths[ $intCol ];
661        }
662        return $this->rowWidths;
663    }
664    public function setRowFillColors( $mixNewValue = null )
665    {
666        $this->rowFillColors = $mixNewValue;
667        return $this;
668    }
669    public function getRowFillColors()
670    {
671        return $this->rowFillColors;
672    }
673    public function setRowFontColors( $mixNewValue = null )
674    {
675        $this->rowFontColors = $mixNewValue;
676        return $this;
677    }
678    public function getRowFontColors()
679    {
680        return $this->rowFontColors;
681    }
682    public function setRowFontStyles( $mixNewValue = null )
683    {
684        $this->rowFontStyles = $mixNewValue;
685        return $this;
686    }
687    public function getRowFontStyles()
688    {
689        return $this->rowFontStyles;
690    }
691    public function setRowAligns( $mixNewValue = null )
692    {
693        $this->rowAligns = $mixNewValue;
694        return $this;
695    }
696    /**
697    * Retorna o array com os alinhamentos de cada coluna.
698    * Se for informado o parametro $allAs, o array retornado
699    * será preenchido todas as posições com o valor de $allAs
700    * Utilize o parametro $colIndex para tetornar o alinhamento
701    * de uma determinada coluna
702    *
703    * @param mixed $allAs
704    */
705    public function getRowAligns( $allAs = null,$colIndex=null )
706    {
707        if ( !is_null( $allAs ) && is_array( $this->rowAligns ) )
708        {
709            return array_fill( 0, count( $this->rowAligns ), $allAs );
710        }
711        if( ! is_null( $colIndex ) )
712        {
713            return $this->rowAligns[$colIndex];
714        }
715        return $this->rowAligns;
716    }
717    public function setRowFieldNames( $mixNewValue = null )
718    {
719        $this->rowFieldNames = $mixNewValue;
720        return $this;
721    }
722    public function getRowFieldNames()
723    {
724        return $this->rowFieldNames;
725    }
726    public function setOnDrawCell( $newValue = null )
727    {
728        $this->onDrawCell = $newValue;
729        return $this;
730    }
731    public function getOnDrawCell()
732    {
733        return $this->onDrawCell;
734    }
735    //------------------------------------------------------------------------------------
736    public function RotatedText( $x, $y, $txt, $angle )
737    {
738        //Text rotated around its origin
739        $this->Rotate( $angle, $x, $y );
740        $this->Text( $x, $y, $txt );
741        $this->Rotate( 0 );
742    }
743    public function Rotate( $angle, $x = -1, $y = -1 )
744    {
745        if ( $x == -1 )$x = $this->x;
746
747        if ( $y == -1 )$y = $this->y;
748
749        if ( $this->angle != 0 )$this->_out( 'Q' );
750        $this->angle = $angle;
751
752        if ( $angle != 0 )
753        {
754            $angle *= M_PI / 180;
755            $c = cos( $angle );
756            $s = sin( $angle );
757            $cx = $x * $this->k;
758            $cy = ( $this->h - $y ) * $this->k;
759            $this->_out( sprintf( 'q %.5F %.5F %.5F %.5F %.2F %.2F cm 1 0 0 1 %.2F %.2F cm', $c, $s, -$s, $c, $cx, $cy,
760                -$cx, -$cy ) );
761        }
762    }
763    /*
764    function _endpage()
765    {
766        if ( $str = $this->getWaterMark() )
767        {
768            $this->SetFont( 'Arial', 'B', 50 );
769            $this->SetTextColor( 255, 192, 203 );
770
771            if ( $this->w > $this->h )
772            {
773                $angle = 35;
774            }
775            else
776            {
777                $angle = 55;
778            }
779            $this->RotatedText( $this->lMargin, $this->h, $str, $angle );
780        }
781
782        if ( $this->angle != 0 )
783        {
784            $this->angle = 0;
785            $this->_out( 'Q' );
786        }
787        parent::_endpage();
788    }
789    /*
790    /**
791     * Método para impressão do cabeçalho do gride
792     */
793    public function printGridHeader()
794    {
795        // se exisiter gride definido, imprimir o titulo
796        if ( $cols = $this->getColumns() )
797        {
798            if( $this->flagPrintHeader===true )
799            {
800                $headers = array();
801                // alinhar os titulos no centro da celula
802                $oldAligns = $this->getRowAligns();
803                $this->setRowAligns( $this->getRowAligns('c') );
804                foreach( $cols as $k => $oCol )
805                {
806                    if ( $oCol->getHeader() )
807                    {
808                        $headers[] = $oCol->getHeader();
809                    }
810                }
811
812                if ( count( $headers ) > 0 )
813                {
814                    $headerFillColors = $this->getHeaderFillColors();
815                    $headerFontColors = $this->getHeaderFontColors();
816                    $this->row( $headers, null, $headerFillColors, $headerFontColors );
817                }
818                $this->setRowAligns($oldAligns);
819                $this->flagPrintHeader=false;
820            }
821        }
822    }
823    public function getRowMaxWidth()
824    {
825        $this->prepare();
826        $r = 0;
827
828        if ( $this->getRowWidths() )
829        {
830            foreach( $this->getRowWidths() as $k => $v )
831            {
832                $r += $v;
833            }
834        }
835        return $r;
836    }
837    public function setVar( $varName = null, $varValue = null )
838    {
839        if ( $varName )
840        {
841            $this->vars[ strtolower( $varName )] = $varValue;
842        }
843    }
844    public function getVar( $varName = null )
845    {
846        if ( $varName )
847        {
848            return $this->vars[ strtolower( $varName )];
849        }
850    }
851    public function setTopMargin($intNewValue=null)
852    {
853        if( (integer) $intNewValue > -1 )
854        {
855            $this->tMargin = $intNewValue;
856        }else{
857            $this->tMargin = 10.00125; // valor padrão.
858        }
859    }
860    public function getTopMargin($intNewValue=null)
861    {
862          return $this->tMargin;
863    }
864    //------------------------------------------------------------------------------------    
865    public function HexToRGB( $hex ){
866        $hex = $this->translateColor( $hex );
867
868        if ( is_null( $hex ) || $hex === 0 ){
869            return $hex;
870        }
871
872        if ( $hex == 255 || $hex == '0 G' || $hex == '0 g' ){
873            return $hex;
874        }
875
876        if ( !preg_match( '/^#/', $hex ) ) {
877            return $hex;
878        }
879        $hex = preg_replace( "/#/", "", $hex );
880        $color = array();
881
882        if ( strlen( $hex ) == 3 ){
883            $color[ 'r' ] = hexdec( substr( $hex, 0, 1 ) . $r );
884            $color[ 'g' ] = hexdec( substr( $hex, 1, 1 ) . $g );
885            $color[ 'b' ] = hexdec( substr( $hex, 2, 1 ) . $b );
886        }else if( strlen( $hex ) == 6 ){
887            $color[ 'r' ] = hexdec( substr( $hex, 0, 2 ) );
888            $color[ 'g' ] = hexdec( substr( $hex, 2, 2 ) );
889            $color[ 'b' ] = hexdec( substr( $hex, 4, 2 ) );
890        }
891        return $color;
892    }
893    /**
894     * Converte o nome de uma cor no valor em HexaDecimal
895     * @param string $strColor
896     * @return void
897     */
898    public function translateColor( $strColor = null )
899    {
900        if ( is_null( $strColor ) || !is_string( $strColor ) || preg_match( '/#/', $strColor ) == 1 )
901        {
902            return $strColor;
903        }
904        $strColor = strtolower( $strColor );
905        $aColors[ 'red' ] = '#ff0000';
906        $aColors[ 'green' ] = '#00ff00';
907        $aColors[ 'blue' ] = '#0000ff';
908        $aColors[ 'yellow' ] = '#FFFF00';
909        $aColors[ 'fuchsia' ] = '#FF00FF';
910        $aColors[ 'gray' ] = '#989898';
911        $aColors[ 'grey' ] = '#989898';
912        $aColors[ 'black' ] = '#000000';
913        $aColors[ 'white' ] = '#ffffff';
914        $aColors[ 'orange' ] = '#FF9900';
915        $aColors[ 'lightYellow' ] = '#FF9900';
916        $aColors[ 'lightBlue' ] = '#0066FF';
917        $aColors[ 'lightGreen' ] = '#66FF99';
918        $aColors[ 'pink' ] = '#FF99FF';
919        $aColors[ 'brown' ] = '#663300';
920        $aColors[ 'silver' ] = '#E8E8E8';
921
922        if ( isset( $aColors[ $strColor ] ) )
923        {
924            return $aColors[ $strColor ];
925        }
926        return $strColor;
927    }    
928    //------------------------------------------------------------------------------------    
929    /**
930     * Seta a cor de fundo do cabeçalho do Grid, é possíveis em HEX decimal um nome fixo.
931     * alguns exemplos
932     *   - red, green, blue, yellow, fuchsia, gray, black, white
933     *   - orange, lightYellow, lightBlue, lightGreen, pink
934     *   - brown, silver
935     *  A lista completa em veja $this->translateColor
936     *
937     * @param string $headerFontColors
938     */    
939    public function setHeaderFillColors($headerFillColors)
940    {
941        $this->headerFillColors = $headerFillColors;
942    }
943    public function getHeaderFillColors()
944    {
945        if(empty($this->headerFillColors) ) {
946            $this->setHeaderFillColors('silver');
947        }
948        return $this->headerFillColors;
949    }
950    /**
951     * Seta a cor da letro deo cabeçao do grid, é possíveis em HEX decimal um nome fixo.
952     * alguns exemplos
953     *   - red, green, blue, yellow, fuchsia, gray, black, white
954     *   - orange, lightYellow, lightBlue, lightGreen, pink
955     *   - brown, silver
956     *  A lista completa em veja $this->translateColor
957     *
958     * @param string $headerFontColors
959     */
960    public function setHeaderFontColors($headerFontColors)
961    {
962        $this->headerFontColors = $headerFontColors;
963    }
964    public function getHeaderFontColors(){
965        if(empty($this->headerFontColors) ) {
966            $this->setHeaderFontColors('black');
967        }
968        return $this->headerFontColors;
969    }
970    public function getFontSize(){
971        return $this->FontSizePt;
972    }
973    //-------------------------------------------------------------------------
974    /**
975     * POG para evitar o problema PHP Warning:  A non-numeric value encountered
976     *
977     * @return mixed
978     */
979    public function getCurrentDrawColor(){
980        $currentColor=$this->DrawColor;
981        if( $currentColor == '0.000 G'){
982            $currentColor=0;
983        }elseif( $currentColor == '0 G'){
984            $currentColor=0;
985        }
986        return $currentColor;
987    }
988    /**
989     * POG para evitar o problema PHP Warning:  A non-numeric value encountered
990     *
991     * @return mixed
992     */    
993    public function getCurrentFillColor(){
994        $currentColor=$this->FillColor;
995        if( $currentColor == '0.000 g'){
996            $currentColor=0;
997        }elseif( $currentColor == '0.412 0.412 0.412 rg'){
998            $currentColor=0;
999        }
1000        return $currentColor;
1001    }
1002    /**
1003     * POG para evitar o problema PHP Warning:  A non-numeric value encountered
1004     *
1005     * @return mixed
1006     */    
1007    public function getCurrentTextColor(){
1008        $currentColor=$this->TextColor;
1009        if( $currentColor == '0 g'){
1010            $currentColor=0;
1011        }elseif( $currentColor == '0.000 g'){
1012            $currentColor=0;
1013        }elseif( $currentColor == '0.412 0.412 0.412 rg'){
1014            $currentColor=0;
1015        }
1016        return $currentColor;
1017    }
1018
1019    /**
1020     * recupera a cor
1021     *
1022     * @param mixed $mixColor
1023     * @param string $color
1024     * @return void
1025     */
1026    public function getMixColor($mixColor, $color){
1027        $aCor = null;
1028        if ( $mixColor ) {
1029            if ( is_array( $mixColor ) ){
1030                if ( isset( $mixColor[ 'g' ] ) ) {
1031                    $aCor = $mixColor;
1032                } else {
1033                    $aCor[ 'r' ] = $mixColor[ 0 ];
1034                    $aCor[ 'g' ] = $mixColor[ 1 ];
1035                    $aCor[ 'b' ] = $mixColor[ 2 ];
1036                }
1037            } else {
1038                $aCor = $this->HexToRGB( $mixColor );
1039            }
1040        } else {
1041            $aCor = $this->HexToRGB( $color );
1042        }
1043        return $aCor;
1044    }
1045}
1046?>