Code

Updated in
[gosa.git] / gosa-core / include / exporter / class_pdfExporter.inc
1 <?php
2 // Try to load PDF library
3 @include_once('fpdf/fpdf.php');
5 // Load supporter class only if FPDF is loaded
6 $classes= get_declared_classes();
7 if(in_array_strict('FPDF', $classes)) {
8   include('class_PDF.inc');
9 }
11 class pdfExporter
12 {
13   var $result;
15   function pdfExporter($headline, $header, $entries, $columns= array()) {
16     // Bail out if no FPDF available
17     if(!class_exists('FPDF')) {
18       die(_("No PDF export possible: there is no FPDF library installed."));
19     }
21     // If no preset, render all columns
22     if (!count($columns)) {
23       foreach ($header as $index => $dummy) {
24         $columns[]= $index;
25       }
26     }
28     // Create new PDF
29     $this->result=new PDF('L', 'mm', 'A4');
30     $this->result->AliasNbPages();
31     $this->result->SetFont('Helvetica', '', 10);
32     $this->result->setHeadline(utf8_decode($headline));
33     $this->result->AddPage();
35     // Analyze for width
36     $width= $this->calcWidth($header, $entries, $columns);
37     
38     // Render head
39     $this->result->SetFont('','B');
40     $this->result->SetTextColor(0);
41     $this->result->SetDrawColor(0,0,0);
42     $this->result->SetLineWidth(.3);
44     // Height calculator
45     $height= 0;
47     $fill= false;
48     foreach ($entries as $row) {
49       // Render header
50       if ($height == 0) {
51         // Generate header
52         $this->result->SetFillColor(230,230,230);
53         $this->result->SetFont('','B');
55         foreach ($columns as $order => $index) {
56           if (isset($header[$index])){
57             $this->result->Cell($width[$order], 7, utf8_decode($header[$index]), 1, 0, 'C', 1);
58           } else {
59             $this->result->Cell($width[$order], 7, '', 1, 0, 'C', 1);
60           }
61         }
62         $this->result->Ln();
63         $height= 7;
65         // Set entry collors
66         $this->result->SetFillColor(240,240,240);
67         $this->result->SetFont('');
68       }
70       foreach ($columns as $order => $index) {
72         if (isset($row["_sort$index"])){
73           $this->result->Cell($width[$order], 6, utf8_decode($row["_sort$index"]), 'LR', 0, 'L', $fill);
74         } else {
75           $this->result->Cell($width[$order], 6, '', 'LR', 0, 'L', $fill);
76         }
77       }
79       $this->result->Ln();
81       // Increase height to eventually create new page
82       $height+= 8;
83       if ($height > 220) {
84         $height= 0;
85         $this->result->Cell(array_sum($width), 0, '', 'T');
86         $this->result->AddPage();
87         $fill= false;
88       } else {
89         $fill= !$fill;
90       }
91     }
92     $this->result->Cell(array_sum($width), 0, '', 'T');
93   }
96   function calcWidth($header, $entries, $columns)
97   {
98     $width= array();
100     // Locate longest value for each column
101     foreach ($columns as $order => $index) {
102       $max= 0;
104       if (isset($header[$index])){
105         $len= $this->result->GetStringWidth($header[$index]);
106         if ($len > $max) {
107           $max= $len;
108         }
109       }
111       foreach ($entries as $row) {
112         if (isset($row["_sort$index"])){
113           $len= $this->result->GetStringWidth($row["_sort$index"]);
114           if ($len > $max) {
115             $max= $len;
116           }
117         }
118       }
120       $width[]= $max;
121     }
123     // Scale to page width
124     $printWidth= 280;
125     $scale= $printWidth / array_sum($width);
126     foreach ($width as $index => $w) {
127       $width[$index]= (int)($width[$index] * $scale);
128     }
130     return $width;
131   }
134   function query()
135   {
136      return $this->result->Output("", "S");
137   }
140   static function getInfo()
141   {
142     // Check if class defined
143     $classes= get_declared_classes();
144     if(in_array_strict('FPDF', $classes)) {
145       return array("exportPDF" => array( "label" => _("PDF"), "image" => "images/lists/pdf.png", "class"=> "pdfExporter", "mime" => "application/pdf", "filename" => "export.pdf" ));
146     } else {
147       return null;
148     }
150   }
154 ?>