FusionDirectory
class_pdfExporter.inc
Go to the documentation of this file.
1 <?php
2 /*
3  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
4  Copyright (C) 2003-2010 Cajus Pollmeier
5  Copyright (C) 2011-2016 FusionDirectory
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
20 */
21 
27 // Try to load PDF library
28 @include_once(FPDF);
29 
30 // Load supporter class only if FPDF is loaded
31 $classes = get_declared_classes();
32 if (in_array('FPDF', $classes)) {
33  include('class_PDF.php');
34 }
35 
40 {
41  var $result;
42 
54  function __construct ($headline, $header, $entries, $columns = [])
55  {
56  // Bail out if no FPDF available
57  if (!class_exists('FPDF')) {
58  die(_("No PDF export possible: there is no FPDF library installed."));
59  }
60 
61  // If no preset, render all columns
62  if (!count($columns)) {
63  $columns = array_keys($header);
64  }
65 
66  // Create new PDF
67  $this->result = new PDF('L', 'mm', 'A4');
68  $this->result->AliasNbPages();
69  $this->result->SetFont('Helvetica', '', 10);
70  $this->result->setHeadline(utf8_decode($headline));
71  $this->result->AddPage();
72 
73  // Analyze for width
74  $width = $this->calcWidth($header, $entries, $columns);
75 
76  // Render head
77  $this->result->SetFont('', 'B');
78  $this->result->SetTextColor(0);
79  $this->result->SetDrawColor(0, 0, 0);
80  $this->result->SetLineWidth(.3);
81 
82  // Height calculator
83  $height = 0;
84 
85  $fill = FALSE;
86  foreach ($entries as $row) {
87  // Render header
88  if ($height == 0) {
89  // Generate header
90  $this->result->SetFillColor(230, 230, 230);
91  $this->result->SetFont('', 'B');
92 
93  foreach ($columns as $order => $index) {
94  if (isset($header[$index])) {
95  $this->result->Cell($width[$order], 7, utf8_decode($header[$index]), 1, 0, 'C', 1);
96  } else {
97  $this->result->Cell($width[$order], 7, '', 1, 0, 'C', 1);
98  }
99  }
100  $this->result->Ln();
101  $height = 7;
102 
103  // Set entry collors
104  $this->result->SetFillColor(240, 240, 240);
105  $this->result->SetFont('');
106  }
107 
108  foreach ($columns as $order => $index) {
109  if (isset($row["_sort$index"])) {
110  $this->result->Cell($width[$order], 6, utf8_decode($row["_sort$index"]), 'LR', 0, 'L', $fill);
111  } else {
112  $this->result->Cell($width[$order], 6, '', 'LR', 0, 'L', $fill);
113  }
114  }
115 
116  $this->result->Ln();
117 
118  // Increase height to eventually create new page
119  $height += 8;
120  if ($height > 220) {
121  $height = 0;
122  $this->result->Cell(array_sum($width), 0, '', 'T');
123  $this->result->AddPage();
124  $fill = FALSE;
125  } else {
126  $fill = !$fill;
127  }
128  }
129  $this->result->Cell(array_sum($width), 0, '', 'T');
130  }
131 
141  function calcWidth ($header, $entries, $columns)
142  {
143  $width = [];
144 
145  // Locate longest value for each column
146  foreach ($columns as $index) {
147  $max = 0;
148 
149  if (isset($header[$index])) {
150  $len = $this->result->GetStringWidth($header[$index]);
151  if ($len > $max) {
152  $max = $len;
153  }
154  }
155 
156  foreach ($entries as $row) {
157  if (isset($row["_sort$index"])) {
158  $len = $this->result->GetStringWidth($row["_sort$index"]);
159  if ($len > $max) {
160  $max = $len;
161  }
162  }
163  }
164 
165  $width[] = $max;
166  }
167 
168  // Scale to page width
169  $printWidth = 280;
170  $scale = $printWidth / array_sum($width);
171  foreach ($width as &$w) {
172  $w *= $scale;
173  }
174  unset($w);
175 
176  return $width;
177  }
178 
182  static function calcColumnsWidth ($result, $columns, $iterator)
183  {
184  $width = [];
185 
186  // Locate longest value for each column
187  foreach ($columns as $index => $column) {
188  if (!$column->isExportable()) {
189  continue;
190  }
191  $max = 0;
192 
193  $len = $result->GetStringWidth($column->getLabel());
194  if ($len > $max) {
195  $max = $len;
196  }
197 
198  foreach ($iterator as $entry) {
199  $len = $result->GetStringWidth(implode(',', $column->getRawExportValues($entry)));
200  if ($len > $max) {
201  $max = $len;
202  }
203  }
204 
205  $width[$index] = $max;
206  }
207 
208  // Scale to page width
209  $printWidth = 280;
210  $scale = $printWidth / array_sum($width);
211  foreach ($width as &$w) {
212  $w *= $scale;
213  }
214  unset($w);
215 
216  return $width;
217  }
218 
222  function query ()
223  {
224  return $this->result->Output("", "S");
225  }
226 
227  static function export (managementListing $listing)
228  {
229  global $ui;
230 
231  // Bail out if no FPDF available
232  if (!class_exists('FPDF')) {
233  die(_('No PDF export possible: there is no FPDF library installed.'));
234  }
235 
236  $columns = $listing->getColumns();
237  $iterator = $listing->getIterator();
238 
239  // Create new PDF
240  $result = new PDF('L', 'mm', 'A4');
241  $result->AliasNbPages();
242  $result->SetFont('Helvetica', '', 10);
243  $headline = $listing->parent->headline;
244  $headline .= ', '._('created by').' '.$ui->cn.' - '.strftime('%A, %d. %B %Y, %H:%M:%S');
245  $result->setHeadline(utf8_decode($headline));
246  $result->AddPage();
247 
248  // Analyze for width
249  $width = static::calcColumnsWidth($result, $columns, $iterator);
250 
251  // Render head
252  $result->SetTextColor(0);
253  $result->SetDrawColor(0, 0, 0);
254  $result->SetLineWidth(.3);
255 
256  // Render header
257  $result->SetFillColor(230, 230, 230);
258  $result->SetFont('', 'B');
259 
260  foreach ($columns as $index => $column) {
261  if ($column->isExportable()) {
262  $result->Cell($width[$index], 7, utf8_decode($column->getLabel()), 1, 0, 'C', 1);
263  }
264  }
265  $result->Ln();
266  $height = 7;
267 
268  // Set entry colors
269  $result->SetFillColor(240, 240, 240);
270  $result->SetFont('');
271 
272  $fill = TRUE;
273  foreach ($iterator as $entry) {
274  if ($height > 220) {
275  $height = 0;
276  $result->Cell(array_sum($width), 0, '', 'T');
277  $result->AddPage();
278  $fill = FALSE;
279  } else {
280  $fill = !$fill;
281  }
282 
283  foreach ($columns as $index => $column) {
284  if ($column->isExportable()) {
285  $result->Cell(
286  $width[$index],
287  6,
288  implode(
289  ',',
290  array_map(
291  'utf8_decode',
292  $column->getRawExportValues($entry)
293  )
294  ),
295  'LR',
296  0,
297  'L',
298  $fill
299  );
300  }
301  }
302 
303  $result->Ln();
304 
305  // Increase height to eventually create new page
306  $height += 8;
307  }
308  $result->Cell(array_sum($width), 0, '', 'T');
309 
310  return $result->Output('', 'S');
311  }
312 
316  static function getInfo ()
317  {
318  // Check if class defined
319  $classes = get_declared_classes();
320  if (in_array('FPDF', $classes)) {
321  return ["exportPDF" => [ "label" => _("PDF"), "image" => "geticon.php?context=mimetypes&icon=application-pdf&size=16", "class" => "pdfExporter", "mime" => "application/pdf", "filename" => "export.pdf" ]];
322  } else {
323  return NULL;
324  }
325  }
326 }
This class contains all the functions to manage pdf.
Definition: class_PDF.php:30
query()
Get the result.
This class handles the entries list for a management instance.
__construct($headline, $header, $entries, $columns=[])
Export PDF.
calcWidth($header, $entries, $columns)
Calculate the width page.
static calcColumnsWidth($result, $columns, $iterator)
Calculate the width page.
static getInfo()
Get informations.
This class contains all the functions needed for pdf export.