1 <?php
3 // Load supporter class only if FPDF is loaded
4 $classes= get_declared_classes();
5 if(in_array('FPDF', $classes)) {
6 include('class_PDF.inc');
7 }
9 class pdfExporter
10 {
11 var $result;
13 function pdfExporter($headline, $header, $entries, $columns= array()) {
14 // Bail out if no FPDF available
15 if(!class_exists('FPDF')) {
16 die(_("No PDF export possible: there is no FPDF library installed."));
17 }
19 // If no preset, render all columns
20 if (!count($columns)) {
21 foreach ($header as $index => $dummy) {
22 $columns[]= $index;
23 }
24 }
26 // Create new PDF
27 $this->result=new PDF('L', 'mm', 'A4');
28 $this->result->AliasNbPages();
29 $this->result->SetFont('Helvetica', '', 10);
30 $this->result->setHeadline(utf8_decode($headline));
31 $this->result->AddPage();
33 // Analyze for width
34 $width= $this->calcWidth($header, $entries, $columns);
36 // Render head
37 $this->result->SetFont('','B');
38 $this->result->SetTextColor(0);
39 $this->result->SetDrawColor(0,0,0);
40 $this->result->SetLineWidth(.3);
42 // Height calculator
43 $height= 0;
45 $fill= false;
46 foreach ($entries as $row) {
47 // Render header
48 if ($height == 0) {
49 // Generate header
50 $this->result->SetFillColor(230,230,230);
51 $this->result->SetFont('','B');
53 foreach ($columns as $order => $index) {
54 if (isset($header[$index])){
55 $this->result->Cell($width[$order], 7, utf8_decode($header[$index]), 1, 0, 'C', 1);
56 } else {
57 $this->result->Cell($width[$order], 7, '', 1, 0, 'C', 1);
58 }
59 }
60 $this->result->Ln();
61 $height= 7;
63 // Set entry collors
64 $this->result->SetFillColor(240,240,240);
65 $this->result->SetFont('');
66 }
68 foreach ($columns as $order => $index) {
70 if (isset($row["_sort$index"])){
71 $this->result->Cell($width[$order], 6, utf8_decode($row["_sort$index"]), 'LR', 0, 'L', $fill);
72 } else {
73 $this->result->Cell($width[$order], 6, '', 'LR', 0, 'L', $fill);
74 }
75 }
77 $this->result->Ln();
79 // Increase height to eventually create new page
80 $height+= 8;
81 if ($height > 220) {
82 $height= 0;
83 $this->result->Cell(array_sum($width), 0, '', 'T');
84 $this->result->AddPage();
85 $fill= false;
86 } else {
87 $fill= !$fill;
88 }
89 }
90 $this->result->Cell(array_sum($width), 0, '', 'T');
91 }
94 function calcWidth($header, $entries, $columns)
95 {
96 $width= array();
98 // Locate longest value for each column
99 foreach ($columns as $order => $index) {
100 $max= 0;
102 if (isset($header[$index])){
103 $len= $this->result->GetStringWidth($header[$index]);
104 if ($len > $max) {
105 $max= $len;
106 }
107 }
109 foreach ($entries as $row) {
110 if (isset($row["_sort$index"])){
111 $len= $this->result->GetStringWidth($row["_sort$index"]);
112 if ($len > $max) {
113 $max= $len;
114 }
115 }
116 }
118 $width[]= $max;
119 }
121 // Scale to page width
122 $printWidth= 280;
123 $scale= $printWidth / array_sum($width);
124 foreach ($width as $index => $w) {
125 $width[$index]= (int)($width[$index] * $scale);
126 }
128 return $width;
129 }
132 function query()
133 {
134 return $this->result->Output("", "S");
135 }
138 static function getInfo()
139 {
140 // Check if class defined
141 $classes= get_declared_classes();
142 if(in_array('FPDF', $classes)) {
143 return array("exportPDF" => array( "label" => _("PDF"), "image" => "images/lists/pdf.png", "class"=> "pdfExporter", "mime" => "application/pdf", "filename" => "export.pdf" ));
144 } else {
145 return null;
146 }
148 }
150 }
152 ?>