File: class_pdfExporter.inc

package info (click to toggle)
fusiondirectory 1.0.8.2-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 28,984 kB
  • sloc: php: 74,645; xml: 3,645; perl: 1,555; pascal: 705; sh: 135; sql: 45; makefile: 19
file content (204 lines) | stat: -rw-r--r-- 5,193 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
/*
  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
  Copyright (C) 2003-2010  Cajus Pollmeier
  Copyright (C) 2011-2013  FusionDirectory

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/

/*!
 * \file class_pdfExporter.inc
 * Source code for class pdfExporter
 */

// Try to load PDF library
@include_once(FPDF);

// Load supporter class only if FPDF is loaded
$classes = get_declared_classes();
if (in_array('FPDF', $classes)) {
  include('class_PDF.php');
}

/*!
 * \brief This class contains all the functions needed for pdf export
 */
class pdfExporter
{
  var $result;

  /*!
   * \brief Export PDF
   *
   * \param array $headline
   *
   * \param array $header
   *
   * \param array $entries
   *
   * \param array $columns
   */
  function __construct($headline, $header, $entries, $columns = array())
  {
    // Bail out if no FPDF available
    if (!class_exists('FPDF')) {
      die(_("No PDF export possible: there is no FPDF library installed."));
    }

    // If no preset, render all columns
    if (!count($columns)) {
      $columns = array_keys($header);
    }

    // Create new PDF
    $this->result = new PDF('L', 'mm', 'A4');
    $this->result->AliasNbPages();
    $this->result->SetFont('Helvetica', '', 10);
    $this->result->setHeadline(utf8_decode($headline));
    $this->result->AddPage();

    // Analyze for width
    $width = $this->calcWidth($header, $entries, $columns);

    // Render head
    $this->result->SetFont('', 'B');
    $this->result->SetTextColor(0);
    $this->result->SetDrawColor(0, 0, 0);
    $this->result->SetLineWidth(.3);

    // Height calculator
    $height = 0;

    $fill = FALSE;
    foreach ($entries as $row) {
      // Render header
      if ($height == 0) {
        // Generate header
        $this->result->SetFillColor(230, 230, 230);
        $this->result->SetFont('', 'B');

        foreach ($columns as $order => $index) {
          if (isset($header[$index])) {
            $this->result->Cell($width[$order], 7, utf8_decode($header[$index]), 1, 0, 'C', 1);
          } else {
            $this->result->Cell($width[$order], 7, '', 1, 0, 'C', 1);
          }
        }
        $this->result->Ln();
        $height = 7;

        // Set entry collors
        $this->result->SetFillColor(240, 240, 240);
        $this->result->SetFont('');
      }

      foreach ($columns as $order => $index) {

        if (isset($row["_sort$index"])) {
          $this->result->Cell($width[$order], 6, utf8_decode($row["_sort$index"]), 'LR', 0, 'L', $fill);
        } else {
          $this->result->Cell($width[$order], 6, '', 'LR', 0, 'L', $fill);
        }
      }

      $this->result->Ln();

      // Increase height to eventually create new page
      $height += 8;
      if ($height > 220) {
        $height = 0;
        $this->result->Cell(array_sum($width), 0, '', 'T');
        $this->result->AddPage();
        $fill = FALSE;
      } else {
        $fill = !$fill;
      }
    }
    $this->result->Cell(array_sum($width), 0, '', 'T');
  }

  /*!
   * \brief Calculate the width page
   *
   * \param array $header
   *
   * \param array $entries
   *
   * \param array $columns
   */
  function calcWidth($header, $entries, $columns)
  {
    $width = array();

    // Locate longest value for each column
    foreach ($columns as $index) {
      $max = 0;

      if (isset($header[$index])) {
        $len = $this->result->GetStringWidth($header[$index]);
        if ($len > $max) {
          $max = $len;
        }
      }

      foreach ($entries as $row) {
        if (isset($row["_sort$index"])) {
          $len = $this->result->GetStringWidth($row["_sort$index"]);
          if ($len > $max) {
            $max = $len;
          }
        }
      }

      $width[] = $max;
    }

    // Scale to page width
    $printWidth = 280;
    $scale      = $printWidth / array_sum($width);
    foreach ($width as &$w) {
      $w *= $scale;
    }
    unset($w);

    return $width;
  }

  /*!
   * \brief Get the result
   */
  function query()
  {
     return $this->result->Output("", "S");
  }

  /*!
   * \brief Get informations
   */
  static function getInfo()
  {
    // Check if class defined
    $classes = get_declared_classes();
    if (in_array('FPDF', $classes)) {
      return array("exportPDF" => array( "label" => _("PDF"), "image" => "geticon.php?context=mimetypes&icon=application-pdf&size=16", "class" => "pdfExporter", "mime" => "application/pdf", "filename" => "export.pdf" ));
    } else {
      return NULL;
    }
  }

}

?>