Subversion Repositories eFlore/Applications.cel

Rev

Blame | Last modification | View Log | RSS feed

<?php

// Include the main TCPDF library (search for installation path).
date_default_timezone_set("Europe/Paris");
require_once('tcpdf_config.php');
require_once('tcpdf/tcpdf.php');

Class GenerateurPDF {

        public $pdf;

        function GenerateurPDF($utilisateur = NULL) {
                // create new PDF document
                $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

                // set document information
                $pdf->SetCreator(PDF_CREATOR);
                $pdf->SetAuthor($utilisateur ? $utilisateur['prenom'] . ' ' . $utilisateur['nom'] : 'CEL - Tela Botanica');
                $pdf->SetTitle('Observations en étiquettes');
                $pdf->SetSubject('Étiquettes des observations');
                $pdf->SetKeywords('botaniques, observations, étiquettes, cel, tela-botanica');

                // set default header data
                // $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 005', PDF_HEADER_STRING);

                // set header and footer fonts
                // $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
                // $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

                // set default monospaced font
                $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

                // set margins
                $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
                $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
                $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

                // set auto page breaks
                // $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
                $pdf->SetAutoPageBreak(FALSE, PDF_MARGIN_BOTTOM);

                $pdf->SetFont('times', '', 12);
                $pdf->setCellPaddings(1, 1, 1, 1);
                $pdf->setCellMargins(1, 1, 1, 1);

                $this->pdf = $pdf;
        }



        function export($obs) {
                $pdf = &$this->pdf;
                $pdf->AddPage();
                $pdf->setEqualColumns(2);

                $i = 0;
                while($i < count($obs)) {
                        $pdf->selectColumn(0);
                        // Multicell test
                        $this->docell($obs[$i++]); if(!isset($obs[$i])) break;
                        $pdf->Ln();
                        $this->docell($obs[$i++]);  if(!isset($obs[$i])) break;
                        $pdf->Ln();
                        $this->docell($obs[$i++]); if(!isset($obs[$i])) break;
                        /*$pdf->MultiCell(0, 25, self::doTemplate($obs), 1, 'L', 1, 1, '', '', true);
                          $pdf->MultiCell(0, 25, self::doTemplate($obs), 1, 'L', 1, 1, '', '', true);*/

                        $pdf->selectColumn(1);
                        $this->docell($obs[$i++]); if(!isset($obs[$i])) break;
                        $pdf->Ln();
                        $this->docell($obs[$i++]); if(!isset($obs[$i])) break;
                        $pdf->Ln();
                        $this->docell($obs[$i++]); if(!isset($obs[$i])) break;
                        /*$pdf->MultiCell(0, 25, self::doTemplate($obs), 1, 'L', 0, 1, '', '', true);
                        $pdf->MultiCell(0, 25, self::doTemplate($obs), 1, 'L', 0, 1, '', '', true);
                        $pdf->MultiCell(0, 25, self::doTemplate($obs), 1, 'L', 0, 1, '', '', true);*/

                        if(isset($obs[$i])) $pdf->AddPage();
                }
        }

        // TODO: test plus grand nom d'espèce
        // ORDER BY id_observation
        // intitulé en gras
        // italique pour nom d'espèce, mais pas auteur
        // supprimer les lignes
        function docell($obs) {
                $this->pdf->setCellMargins(0,0,0,0);

                $this->pdf->Cell($w = 60, '',
                                                 $txt = 'Famille : ' . $obs['famille'],
                                                 $border = 1,
                                                 $ln = 0,
                                                 $align = 'L',
                                                 $fill = false,
                                                 $link = false,
                                                 $stretch = 1,
                                                 $ignore_min_height = false,
                                                 $calign = 'T',
                                                 $valign = 'M');

                $this->pdf->Cell($w = 20, '',
                                                 $txt = 'N° : ' . $obs['id_observation'] /*. sprintf("%04d", $obs['ordre']) */,
                                                 $border = 1,
                                                 $ln = 1,
                                                 $align = 'L',
                                                 $fill = false,
                                                 $link = false,
                                                 $stretch = 1,
                                                 $ignore_min_height = false,
                                                 $calign = 'T',
                                                 $valign = 'M');

                $this->pdf->Cell($w = 80, '',
                                                 $txt = 'Espèce : ' . $obs['nom_ret'],
                                                 $border = 1,
                                                 $ln = 1,
                                                 $align = 'L',
                                                 $fill = false,
                                                 $link = false,
                                                 $stretch = 1,
                                                 $ignore_min_height = false,
                                                 $calign = 'T',
                                                 $valign = 'M');

                $this->pdf->Cell($w = 80, '',
                                                 $txt = 'Collecteur : ' . $obs['prenom_utilisateur'] . ' ' . $obs['nom_utilisateur'],
                                                 $border = 1,
                                                 $ln = 1,
                                                 $align = 'L',
                                                 $fill = false,
                                                 $link = false,
                                                 $stretch = 1,
                                                 $ignore_min_height = false,
                                                 $calign = 'T',
                                                 $valign = 'M');

                $this->pdf->MultiCell(80, 20,
                                                          $txt = sprintf("Localité : %s (%s)\n%s, %s",
                                                                                         $obs['zone_geo'],
                                                                                         $obs['ce_zone_geo'],
                                                                                         $obs['lieudit'],
                                                                                         $obs['station']),
                                                          1,
                                                          'L',
                                                          0,
                                                          1,
                                                          '',
                                                          '',
                                                          true);

                $this->pdf->Cell($w = 80, '',
                                                 $txt = sprintf("Latitude, Longitude : %s  /  %s", $obs['latitude'], $obs['longitude']),
                                                 $border = 1,
                                                 $ln = 1,
                                                 $align = 'L',
                                                 $fill = false,
                                                 $link = false,
                                                 $stretch = 1,
                                                 $ignore_min_height = false,
                                                 $calign = 'T',
                                                 $valign = 'M');

                $this->pdf->MultiCell(80, 20,
                                                          $txt = self::coupeCommentaire("Observation : ", $obs['commentaire']),
                                                          1,
                                                          'L',
                                                          0,
                                                          1,
                                                          '',
                                                          '',
                                                          true);

                $this->pdf->Cell($w = 80, '',
                                                 $txt = 'Date : ' . strftime("%d/%m/%Y", strtotime($obs['date_observation'])),
                                                 $border = 1,
                                                 $ln = 1,
                                                 $align = 'R',
                                                 $fill = false,
                                                 $link = false,
                                                 $stretch = 1,
                                                 $ignore_min_height = false,
                                                 $calign = 'T',
                                                 $valign = 'M');
        }







        function coupeCommentaire($intitule, $commentaire) {
                // TODO: GetLineWidth, GetCharWidth()
                $limite = 3 /* lignes */ * 43 /* caractères */ - strlen($intitule);
                if(mb_strlen($commentaire) < $limite) return 'Observation : ' . $commentaire;
                return 'Observation : ' . mb_substr($commentaire, 0, $limite - 3) . '...';
        }


        function export1($observations) {
                $pdf = &$this->pdf;
                // MultiCell($w, $h, $txt, $border=0, $align='J', $fill=0, $ln=1, $x='', $y='', $reseth=true, $stretch=0, $ishtml=false, $autopadding=true, $maxh=0)

                $pdf->setEqualColumns(2);

                $i = 0;
                while($i < count($observations)) {
                        $obs = $observations[$i];

                        $pdf->selectColumn(0);
                        // Multicell test
                        $pdf->MultiCell(0, 25, self::doTemplate($obs), 1, 'L', 1, 1, '', '', true);
                        $pdf->MultiCell(0, 25, self::doTemplate($obs), 1, 'L', 1, 1, '', '', true);
                        $pdf->MultiCell(0, 25, self::doTemplate($obs), 1, 'L', 1, 1, '', '', true);
                        $pdf->Ln();

                        $pdf->selectColumn(1);
                        $pdf->MultiCell(0, 25, self::doTemplate($obs), 1, 'L', 0, 1, '', '', true);
                        $pdf->MultiCell(0, 25, self::doTemplate($obs), 1, 'L', 0, 1, '', '', true);
                        $pdf->MultiCell(0, 25, self::doTemplate($obs), 1, 'L', 0, 1, '', '', true);

                        $i += 6;
                        if(isset($observations[$i])) $pdf->AddPage();
                }
        }

        static function doTemplate($obs) {
                $pattern =
<<<EOF
Famille: %s (%d)
Espèce: %s
Collecteur: %s
Localité: %s
Observation: %s  Date: %s
EOF;
                return sprintf($pattern,

                                           $obs['famille'],
                                           $obs['ordre'],
                                           $obs['nom_ret'],
                                           $obs['prenom_utilisateur'] . ' ' . $obs['nom_utilisateur'],
                                           $obs['zone_geo'],
                                           $obs['commentaire'],
                                           strftime("%Y-%m-%d", strtotime($obs['date_observation']))
                );
                                          
        }



        function export2($observations) {
                $pdf = &$this->pdf;

                $pdf->setEqualColumns(2);

                $i = 0;
                $y = $pdf->getY();
                $x = $pdf->getX();
                while($i < count($observations)) {
                        $obs = $observations[$i++];

                        $pdf->selectColumn(0);
                        // Multicell test
                        $pdf->writeHTMLCell(0, 25, $x, $y + 25 * 0, self::doHTMLTemplate($obs), 1, 0, 0, true);
                        $pdf->writeHTMLCell(0, 25, $x, $y + 25 * 1, self::doHTMLTemplate($obs), 1, 0, 0, true);
                        $pdf->writeHTMLCell(0, 25, $x, $y + 25 * 2, self::doHTMLTemplate($obs), 1, 0, 0, true);
                        //$pdf->Ln();

                        $pdf->selectColumn(1);
                        $pdf->writeHTMLCell(0, 25, $x, $y + 25 * 0, self::doHTMLTemplate($obs), 1, 1, 1, true);
                        $pdf->writeHTMLCell(0, 25, $x, $y + 25 * 1, self::doHTMLTemplate($obs), 1, 1, 1, true);
                        $pdf->writeHTMLCell(0, 25, $x, $y + 25 * 2, self::doHTMLTemplate($obs), 1, 1, 1, true);

                        $i += 6;
                        if(isset($observations[$i])) $pdf->AddPage();
                }
        }

        static function doHTMLTemplate($obs) {
                $pattern =
<<<EOF
<p>Famille: %s <span style="text-align: right">(%d)</span><br/>
Espèce: %s<br/>
Collecteur: %s<br/>
Localité: %s<br/>
Observation: %s  Date: %s</p>
EOF;
                return sprintf($pattern,

                                           $obs['famille'],
                                           $obs['ordre'],
                                           $obs['nom_ret'],
                                           $obs['prenom_utilisateur'] . ' ' . $obs['nom_utilisateur'],
                                           $obs['zone_geo'],
                                           $obs['commentaire'],
                                           strftime("%Y-%m-%d", strtotime($obs['date_observation']))
                );
                                          
        }

}