FusionDirectory
 All Data Structures Files Functions Variables
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 = array())
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 
110  if (isset($row["_sort$index"])) {
111  $this->result->Cell($width[$order], 6, utf8_decode($row["_sort$index"]), 'LR', 0, 'L', $fill);
112  } else {
113  $this->result->Cell($width[$order], 6, '', 'LR', 0, 'L', $fill);
114  }
115  }
116 
117  $this->result->Ln();
118 
119  // Increase height to eventually create new page
120  $height += 8;
121  if ($height > 220) {
122  $height = 0;
123  $this->result->Cell(array_sum($width), 0, '', 'T');
124  $this->result->AddPage();
125  $fill = FALSE;
126  } else {
127  $fill = !$fill;
128  }
129  }
130  $this->result->Cell(array_sum($width), 0, '', 'T');
131  }
132 
142  function calcWidth($header, $entries, $columns)
143  {
144  $width = array();
145 
146  // Locate longest value for each column
147  foreach ($columns as $index) {
148  $max = 0;
149 
150  if (isset($header[$index])) {
151  $len = $this->result->GetStringWidth($header[$index]);
152  if ($len > $max) {
153  $max = $len;
154  }
155  }
156 
157  foreach ($entries as $row) {
158  if (isset($row["_sort$index"])) {
159  $len = $this->result->GetStringWidth($row["_sort$index"]);
160  if ($len > $max) {
161  $max = $len;
162  }
163  }
164  }
165 
166  $width[] = $max;
167  }
168 
169  // Scale to page width
170  $printWidth = 280;
171  $scale = $printWidth / array_sum($width);
172  foreach ($width as &$w) {
173  $w *= $scale;
174  }
175  unset($w);
176 
177  return $width;
178  }
179 
183  function query()
184  {
185  return $this->result->Output("", "S");
186  }
187 
191  static function getInfo()
192  {
193  // Check if class defined
194  $classes = get_declared_classes();
195  if (in_array('FPDF', $classes)) {
196  return array("exportPDF" => array( "label" => _("PDF"), "image" => "geticon.php?context=mimetypes&icon=application-pdf&size=16", "class" => "pdfExporter", "mime" => "application/pdf", "filename" => "export.pdf" ));
197  } else {
198  return NULL;
199  }
200  }
201 
202 }
203 
204 ?>
This class contains all the functions to manage pdf.
Definition: class_PDF.php:30
query()
Get the result.
__construct($headline, $header, $entries, $columns=array())
Export PDF.
calcWidth($header, $entries, $columns)
Calculate the width page.
static getInfo()
Get informations.
This class contains all the functions needed for pdf export.