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