Merge branch 'master' of ssh://repo.or.cz/srv/git/phpmyadmin/madhuracj into OpenGIS
[phpmyadmin/madhuracj.git] / libraries / export / pdf.php
blob90f0a3094910760464b0ac7b8fb44a943a5079b5
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Produce a PDF report (export) from a query
6 * @package PhpMyAdmin-Export
7 * @subpackage PDF
8 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /**
16 if (isset($plugin_list)) {
17 $plugin_list['pdf'] = array(
18 'text' => __('PDF'),
19 'extension' => 'pdf',
20 'mime_type' => 'application/pdf',
21 'force_file' => true,
22 'options' => array(
23 array('type' => 'begin_group', 'name' => 'general_opts'),
24 array('type' => 'message_only', 'name' => 'explanation', 'text' => __('(Generates a report containing the data of a single table)')),
25 array('type' => 'text', 'name' => 'report_title', 'text' => __('Report title:')),
26 array('type' => 'hidden', 'name' => 'structure_or_data'),
27 array('type' => 'end_group')
29 'options_text' => __('Options'),
31 } else {
33 include_once './libraries/PDF.class.php';
35 /**
36 * Adapted from a LGPL script by Philip Clarke
37 * @package PhpMyAdmin-Export
38 * @subpackage PDF
40 class PMA_Export_PDF extends PMA_PDF
42 var $tablewidths;
43 var $headerset;
45 function checkPageBreak($h=0, $y='', $addpage=true) {
46 if ($this->empty_string($y)) {
47 $y = $this->y;
49 $current_page = $this->page;
50 if ((($y + $h) > $this->PageBreakTrigger) AND (!$this->InFooter) AND ($this->AcceptPageBreak())) {
51 if ($addpage) {
52 //Automatic page break
53 $x = $this->x;
54 $this->AddPage($this->CurOrientation);
55 $this->y = $this->dataY;
56 $oldpage = $this->page - 1;
57 if ($this->rtl) {
58 if ($this->pagedim[$this->page]['orm'] != $this->pagedim[$oldpage]['orm']) {
59 $this->x = $x - ($this->pagedim[$this->page]['orm'] - $this->pagedim[$oldpage]['orm']);
60 } else {
61 $this->x = $x;
63 } else {
64 if ($this->pagedim[$this->page]['olm'] != $this->pagedim[$oldpage]['olm']) {
65 $this->x = $x + ($this->pagedim[$this->page]['olm'] - $this->pagedim[$oldpage]['olm']);
66 } else {
67 $this->x = $x;
71 return true;
73 if ($current_page != $this->page) {
74 // account for columns mode
75 return true;
77 return false;
80 function Header()
82 global $maxY;
83 // Check if header for this page already exists
84 if (!isset($this->headerset[$this->page])) {
85 $fullwidth = 0;
86 foreach ($this->tablewidths as $width) {
87 $fullwidth += $width;
89 $this->SetY(($this->tMargin) - ($this->FontSizePt/$this->k)*3);
90 $this->cellFontSize = $this->FontSizePt ;
91 $this->SetFont(PMA_PDF_FONT, '', ($this->titleFontSize ? $this->titleFontSize : $this->FontSizePt));
92 $this->Cell(0, $this->FontSizePt, $this->titleText, 0, 1, 'C');
93 $this->SetFont(PMA_PDF_FONT, '', $this->cellFontSize);
94 $this->SetY(($this->tMargin) - ($this->FontSizePt/$this->k)*1.5);
95 $this->Cell(0, $this->FontSizePt, __('Database') .': ' .$this->currentDb .', ' .__('Table') .': ' .$this->currentTable, 0, 1, 'L');
96 $l = ($this->lMargin);
97 foreach ($this->colTitles as $col => $txt) {
98 $this->SetXY($l, ($this->tMargin));
99 $this->MultiCell($this->tablewidths[$col], $this->FontSizePt, $txt);
100 $l += $this->tablewidths[$col] ;
101 $maxY = ($maxY < $this->getY()) ? $this->getY() : $maxY ;
103 $this->SetXY($this->lMargin, $this->tMargin);
104 $this->setFillColor(200, 200, 200);
105 $l = ($this->lMargin);
106 foreach ($this->colTitles as $col => $txt) {
107 $this->SetXY($l, $this->tMargin);
108 $this->cell($this->tablewidths[$col], $maxY-($this->tMargin), '', 1, 0, 'L', 1);
109 $this->SetXY($l, $this->tMargin);
110 $this->MultiCell($this->tablewidths[$col], $this->FontSizePt, $txt, 0, 'C');
111 $l += $this->tablewidths[$col];
113 $this->setFillColor(255, 255, 255);
114 // set headerset
115 $this->headerset[$this->page] = 1;
118 $this->dataY = $maxY;
121 function morepagestable($lineheight=8)
123 // some things to set and 'remember'
124 $l = $this->lMargin;
125 $startheight = $h = $this->dataY;
126 $startpage = $currpage = $this->page;
128 // calculate the whole width
129 $fullwidth = 0;
130 foreach ($this->tablewidths as $width) {
131 $fullwidth += $width;
134 // Now let's start to write the table
135 $row = 0;
136 $tmpheight = array();
137 $maxpage = $this->page;
139 while ($data = PMA_DBI_fetch_row($this->results)) {
140 $this->page = $currpage;
141 // write the horizontal borders
142 $this->Line($l, $h, $fullwidth+$l, $h);
143 // write the content and remember the height of the highest col
144 foreach ($data as $col => $txt) {
145 $this->page = $currpage;
146 $this->SetXY($l, $h);
147 if ($this->tablewidths[$col] > 0) {
148 $this->MultiCell($this->tablewidths[$col], $lineheight, $txt, 0, $this->colAlign[$col]);
149 $l += $this->tablewidths[$col];
152 if (!isset($tmpheight[$row.'-'.$this->page])) {
153 $tmpheight[$row.'-'.$this->page] = 0;
155 if ($tmpheight[$row.'-'.$this->page] < $this->GetY()) {
156 $tmpheight[$row.'-'.$this->page] = $this->GetY();
158 if ($this->page > $maxpage) {
159 $maxpage = $this->page;
161 unset($data[$col]);
164 // get the height we were in the last used page
165 $h = $tmpheight[$row.'-'.$maxpage];
166 // set the "pointer" to the left margin
167 $l = $this->lMargin;
168 // set the $currpage to the last page
169 $currpage = $maxpage;
170 unset($data[$row]);
171 $row++;
173 // draw the borders
174 // we start adding a horizontal line on the last page
175 $this->page = $maxpage;
176 $this->Line($l, $h, $fullwidth+$l, $h);
177 // now we start at the top of the document and walk down
178 for ($i = $startpage; $i <= $maxpage; $i++) {
179 $this->page = $i;
180 $l = $this->lMargin;
181 $t = ($i == $startpage) ? $startheight : $this->tMargin;
182 $lh = ($i == $maxpage) ? $h : $this->h-$this->bMargin;
183 $this->Line($l, $t, $l, $lh);
184 foreach ($this->tablewidths as $width) {
185 $l += $width;
186 $this->Line($l, $t, $l, $lh);
189 // set it to the last page, if not it'll cause some problems
190 $this->page = $maxpage;
193 function setAttributes($attr = array())
195 foreach ($attr as $key => $val) {
196 $this->$key = $val ;
200 function setTopMargin($topMargin)
202 $this->tMargin = $topMargin;
205 function mysql_report($query)
207 unset($this->tablewidths);
208 unset($this->colTitles);
209 unset($this->titleWidth);
210 unset($this->colFits);
211 unset($this->display_column);
212 unset($this->colAlign);
215 * Pass 1 for column widths
217 $this->results = PMA_DBI_query($query, null, PMA_DBI_QUERY_UNBUFFERED);
218 $this->numFields = PMA_DBI_num_fields($this->results);
219 $this->fields = PMA_DBI_get_fields_meta($this->results);
221 // sColWidth = starting col width (an average size width)
222 $availableWidth = $this->w - $this->lMargin - $this->rMargin;
223 $this->sColWidth = $availableWidth / $this->numFields;
224 $totalTitleWidth = 0;
226 // loop through results header and set initial col widths/ titles/ alignment
227 // if a col title is less than the starting col width, reduce that column size
228 for ($i = 0; $i < $this->numFields; $i++) {
229 $stringWidth = $this->getstringwidth($this->fields[$i]->name) + 6 ;
230 // save the real title's width
231 $titleWidth[$i] = $stringWidth;
232 $totalTitleWidth += $stringWidth;
234 // set any column titles less than the start width to the column title width
235 if ($stringWidth < $this->sColWidth) {
236 $colFits[$i] = $stringWidth ;
238 $this->colTitles[$i] = $this->fields[$i]->name;
239 $this->display_column[$i] = true;
241 switch ($this->fields[$i]->type) {
242 case 'int':
243 $this->colAlign[$i] = 'R';
244 break;
245 case 'blob':
246 case 'tinyblob':
247 case 'mediumblob':
248 case 'longblob':
250 * @todo do not deactivate completely the display
251 * but show the field's name and [BLOB]
253 if (stristr($this->fields[$i]->flags, 'BINARY')) {
254 $this->display_column[$i] = false;
255 unset($this->colTitles[$i]);
257 $this->colAlign[$i] = 'L';
258 break;
259 default:
260 $this->colAlign[$i] = 'L';
264 // title width verification
265 if ($totalTitleWidth > $availableWidth) {
266 $adjustingMode = true;
267 } else {
268 $adjustingMode = false;
269 // we have enough space for all the titles at their
270 // original width so use the true title's width
271 foreach ($titleWidth as $key => $val) {
272 $colFits[$key] = $val;
276 // loop through the data; any column whose contents
277 // is greater than the column size is resized
279 * @todo force here a LIMIT to avoid reading all rows
281 while ($row = PMA_DBI_fetch_row($this->results)) {
282 foreach ($colFits as $key => $val) {
283 $stringWidth = $this->getstringwidth($row[$key]) + 6 ;
284 if ($adjustingMode && ($stringWidth > $this->sColWidth)) {
285 // any column whose data's width is bigger than the start width is now discarded
286 unset($colFits[$key]);
287 } else {
288 // if data's width is bigger than the current column width,
289 // enlarge the column (but avoid enlarging it if the
290 // data's width is very big)
291 if ($stringWidth > $val && $stringWidth < ($this->sColWidth * 3)) {
292 $colFits[$key] = $stringWidth ;
298 $totAlreadyFitted = 0;
299 foreach ($colFits as $key => $val) {
300 // set fitted columns to smallest size
301 $this->tablewidths[$key] = $val;
302 // to work out how much (if any) space has been freed up
303 $totAlreadyFitted += $val;
306 if ($adjustingMode) {
307 $surplus = (sizeof($colFits) * $this->sColWidth) - $totAlreadyFitted;
308 $surplusToAdd = $surplus / ($this->numFields - sizeof($colFits));
309 } else {
310 $surplusToAdd = 0;
313 for ($i=0; $i < $this->numFields; $i++) {
314 if (!in_array($i, array_keys($colFits))) {
315 $this->tablewidths[$i] = $this->sColWidth + $surplusToAdd;
317 if ($this->display_column[$i] == false) {
318 $this->tablewidths[$i] = 0;
322 ksort($this->tablewidths);
324 PMA_DBI_free_result($this->results);
326 // Pass 2
328 $this->results = PMA_DBI_query($query, null, PMA_DBI_QUERY_UNBUFFERED);
329 $this->setY($this->tMargin);
330 $this->AddPage();
331 $this->morepagestable($this->FontSizePt);
332 PMA_DBI_free_result($this->results);
334 } // end of mysql_report function
336 } // end of PMA_Export_PDF class
338 $pdf = new PMA_Export_PDF('L', 'pt', 'A3');
341 * Finalize the pdf.
343 * @return bool Whether it suceeded
345 * @access public
347 function PMA_exportFooter()
349 global $pdf;
351 // instead of $pdf->Output():
352 if (!PMA_exportOutputHandler($pdf->getPDFData())) {
353 return false;
356 return true;
360 * Initialize the pdf to export data.
362 * @return bool Whether it suceeded
364 * @access public
366 function PMA_exportHeader()
368 global $pdf_report_title;
369 global $pdf;
371 $pdf->Open();
373 $attr=array('titleFontSize' => 18, 'titleText' => $pdf_report_title);
374 $pdf->setAttributes($attr);
375 $pdf->setTopMargin(45);
377 return true;
381 * Outputs database header
383 * @param string $db Database name
384 * @return bool Whether it suceeded
386 * @access public
388 function PMA_exportDBHeader($db)
390 return true;
394 * Outputs database footer
396 * @param string $db Database name
397 * @return bool Whether it suceeded
399 * @access public
401 function PMA_exportDBFooter($db)
403 return true;
407 * Outputs CREATE DATABASE statement
409 * @param string $db Database name
410 * @return bool Whether it suceeded
412 * @access public
414 function PMA_exportDBCreate($db)
416 return true;
420 * Outputs the content of a table in PDF format
422 * @param string $db database name
423 * @param string $table table name
424 * @param string $crlf the end of line sequence
425 * @param string $error_url the url to go back in case of error
426 * @param string $sql_query SQL query for obtaining data
427 * @return bool Whether it suceeded
429 * @access public
431 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
433 global $pdf;
435 $attr=array('currentDb' => $db, 'currentTable' => $table);
436 $pdf->setAttributes($attr);
437 $pdf->mysql_report($sql_query);
439 return true;
440 } // end of the 'PMA_exportData()' function