[auth] Add custom port configuration in signon
[phpmyadmin/madhuracj.git] / libraries / export / pdf.php
blob8512ad7670c4cc392b06b9011decedda61a43ea1
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-PDF
7 * @version $Id$
8 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /**
16 if (isset($plugin_list)) {
17 $plugin_list['pdf'] = array(
18 'text' => 'strPDF',
19 'extension' => 'pdf',
20 'mime_type' => 'application/pdf',
21 'force_file' => true,
22 'options' => array(
23 array('type' => 'message_only', 'name' => 'explanation', 'text' => 'strPDFReportExplanation'),
24 array('type' => 'text', 'name' => 'report_title', 'text' => 'strPDFReportTitle'),
25 array('type' => 'hidden', 'name' => 'data'),
27 'options_text' => 'strOptions',
29 } else {
31 /**
32 * Font used in PDF.
34 * @todo Make this configuratble (at least Sans/Serif).
36 define('PMA_PDF_FONT', 'DejaVuSans');
37 require_once './libraries/tcpdf/tcpdf.php';
39 /**
40 * Adapted from a LGPL script by Philip Clarke
41 * @package phpMyAdmin-Export-PDF
43 class PMA_PDF extends TCPDF
45 var $tablewidths;
46 var $headerset;
47 var $footerset;
49 // added because tcpdf for PHP 5 has a protected $buffer
50 public function getBuffer()
52 return $this->buffer;
55 public function getState()
57 return $this->state;
60 // overloading of a tcpdf function:
61 function _beginpage($orientation)
63 $this->page++;
64 // solved the problem of overwriting a page, if it already exists
65 if (!isset($this->pages[$this->page])) {
66 $this->pages[$this->page] = '';
68 $this->state = 2;
69 $this->x = $this->lMargin;
70 $this->y = $this->tMargin;
71 $this->lasth = 0;
72 $this->FontFamily = '';
74 //Page orientation
75 if (!$orientation) {
76 $orientation = $this->DefOrientation;
77 } else {
78 $orientation = strtoupper($orientation{0});
79 if ($orientation != $this->DefOrientation) {
80 $this->OrientationChanges[$this->page] = true;
83 if ($orientation != $this->CurOrientation) {
84 //Change orientation
85 if ($orientation == 'P') {
86 $this->wPt = $this->fwPt;
87 $this->hPt = $this->fhPt;
88 $this->w = $this->fw;
89 $this->h = $this->fh;
90 } else {
91 $this->wPt = $this->fhPt;
92 $this->hPt = $this->fwPt;
93 $this->w = $this->fh;
94 $this->h = $this->fw;
96 $this->PageBreakTrigger = $this->h - $this->bMargin;
97 $this->CurOrientation = $orientation;
101 function Header()
103 global $maxY;
105 // Check if header for this page already exists
106 if (!isset($this->headerset[$this->page])) {
107 $fullwidth = 0;
108 foreach ($this->tablewidths as $width) {
109 $fullwidth += $width;
111 $this->SetY(($this->tMargin) - ($this->FontSizePt/$this->k)*2);
112 $this->cellFontSize = $this->FontSizePt ;
113 $this->SetFont(PMA_PDF_FONT, '', ($this->titleFontSize ? $this->titleFontSize : $this->FontSizePt));
114 $this->Cell(0, $this->FontSizePt, $this->titleText, 0, 1, 'C');
115 $l = ($this->lMargin);
116 $this->SetFont(PMA_PDF_FONT, '', $this->cellFontSize);
117 foreach ($this->colTitles as $col => $txt) {
118 $this->SetXY($l, ($this->tMargin));
119 $this->MultiCell($this->tablewidths[$col], $this->FontSizePt, $txt);
120 $l += $this->tablewidths[$col] ;
121 $maxY = ($maxY < $this->getY()) ? $this->getY() : $maxY ;
123 $this->SetXY($this->lMargin, $this->tMargin);
124 $this->setFillColor(200, 200, 200);
125 $l = ($this->lMargin);
126 foreach ($this->colTitles as $col => $txt) {
127 $this->SetXY($l, $this->tMargin);
128 $this->cell($this->tablewidths[$col], $maxY-($this->tMargin), '', 1, 0, 'L', 1);
129 $this->SetXY($l, $this->tMargin);
130 $this->MultiCell($this->tablewidths[$col], $this->FontSizePt, $txt, 0, 'C');
131 $l += $this->tablewidths[$col];
133 $this->setFillColor(255, 255, 255);
134 // set headerset
135 $this->headerset[$this->page] = 1;
138 $this->SetY($maxY);
141 function Footer()
143 // Check if footer for this page already exists
144 if (!isset($this->footerset[$this->page])) {
145 $this->SetY(-15);
146 //Page number
147 $this->Cell(0, 10, $GLOBALS['strPageNumber'] .' '.$this->PageNo() .'/{nb}', 'T', 0, 'C');
149 // set footerset
150 $this->footerset[$this->page] = 1;
154 function morepagestable($lineheight=8)
156 // some things to set and 'remember'
157 $l = $this->lMargin;
158 $startheight = $h = $this->GetY();
159 $startpage = $currpage = $this->page;
161 // calculate the whole width
162 $fullwidth = 0;
163 foreach ($this->tablewidths as $width) {
164 $fullwidth += $width;
167 // Now let's start to write the table
168 $row = 0;
169 $tmpheight = array();
170 $maxpage = 0;
172 while ($data = PMA_DBI_fetch_row($this->results)) {
173 $this->page = $currpage;
174 // write the horizontal borders
175 $this->Line($l, $h, $fullwidth+$l, $h);
176 // write the content and remember the height of the highest col
177 foreach ($data as $col => $txt) {
178 $this->page = $currpage;
179 $this->SetXY($l, $h);
180 if ($this->tablewidths[$col] > 0) {
181 $this->MultiCell($this->tablewidths[$col], $lineheight, $txt, 0, $this->colAlign[$col]);
182 $l += $this->tablewidths[$col];
185 if (!isset($tmpheight[$row.'-'.$this->page])) {
186 $tmpheight[$row.'-'.$this->page] = 0;
188 if ($tmpheight[$row.'-'.$this->page] < $this->GetY()) {
189 $tmpheight[$row.'-'.$this->page] = $this->GetY();
191 if ($this->page > $maxpage) {
192 $maxpage = $this->page;
194 unset($data[$col]);
197 // get the height we were in the last used page
198 $h = $tmpheight[$row.'-'.$maxpage];
199 // set the "pointer" to the left margin
200 $l = $this->lMargin;
201 // set the $currpage to the last page
202 $currpage = $maxpage;
203 unset($data[$row]);
204 $row++;
206 // draw the borders
207 // we start adding a horizontal line on the last page
208 $this->page = $maxpage;
209 $this->Line($l, $h, $fullwidth+$l, $h);
210 // now we start at the top of the document and walk down
211 for ($i = $startpage; $i <= $maxpage; $i++) {
212 $this->page = $i;
213 $l = $this->lMargin;
214 $t = ($i == $startpage) ? $startheight : $this->tMargin;
215 $lh = ($i == $maxpage) ? $h : $this->h-$this->bMargin;
216 $this->Line($l, $t, $l, $lh);
217 foreach ($this->tablewidths as $width) {
218 $l += $width;
219 $this->Line($l, $t, $l, $lh);
222 // set it to the last page, if not it'll cause some problems
223 $this->page = $maxpage;
227 function mysql_report($query, $attr = array())
229 foreach ($attr as $key => $val){
230 $this->$key = $val ;
234 * Pass 1 for column widths
236 $this->results = PMA_DBI_query($query, null, PMA_DBI_QUERY_UNBUFFERED);
237 $this->numFields = PMA_DBI_num_fields($this->results);
238 $this->fields = PMA_DBI_get_fields_meta($this->results);
240 // if column widths not set
241 if (!isset($this->tablewidths)){
243 // sColWidth = starting col width (an average size width)
244 $availableWidth = $this->w - $this->lMargin - $this->rMargin;
245 $this->sColWidth = $availableWidth / $this->numFields;
246 $totalTitleWidth = 0;
248 // loop through results header and set initial col widths/ titles/ alignment
249 // if a col title is less than the starting col width, reduce that column size
250 for ($i = 0; $i < $this->numFields; $i++){
251 $stringWidth = $this->getstringwidth($this->fields[$i]->name) + 6 ;
252 // save the real title's width
253 $titleWidth[$i] = $stringWidth;
254 $totalTitleWidth += $stringWidth;
256 // set any column titles less than the start width to the column title width
257 if ($stringWidth < $this->sColWidth){
258 $colFits[$i] = $stringWidth ;
260 $this->colTitles[$i] = $this->fields[$i]->name;
261 $this->display_column[$i] = true;
263 switch ($this->fields[$i]->type){
264 case 'int':
265 $this->colAlign[$i] = 'R';
266 break;
267 case 'blob':
268 case 'tinyblob':
269 case 'mediumblob':
270 case 'longblob':
272 * @todo do not deactivate completely the display
273 * but show the field's name and [BLOB]
275 if (stristr($this->fields[$i]->flags, 'BINARY')) {
276 $this->display_column[$i] = false;
277 unset($this->colTitles[$i]);
279 $this->colAlign[$i] = 'L';
280 break;
281 default:
282 $this->colAlign[$i] = 'L';
286 // title width verification
287 if ($totalTitleWidth > $availableWidth) {
288 $adjustingMode = true;
289 } else {
290 $adjustingMode = false;
291 // we have enough space for all the titles at their
292 // original width so use the true title's width
293 foreach ($titleWidth as $key => $val) {
294 $colFits[$key] = $val;
298 // loop through the data; any column whose contents
299 // is greater than the column size is resized
301 * @todo force here a LIMIT to avoid reading all rows
303 while ($row = PMA_DBI_fetch_row($this->results)) {
304 foreach ($colFits as $key => $val) {
305 $stringWidth = $this->getstringwidth($row[$key]) + 6 ;
306 if ($adjustingMode && ($stringWidth > $this->sColWidth)) {
307 // any column whose data's width is bigger than the start width is now discarded
308 unset($colFits[$key]);
309 } else {
310 // if data's width is bigger than the current column width,
311 // enlarge the column (but avoid enlarging it if the
312 // data's width is very big)
313 if ($stringWidth > $val && $stringWidth < ($this->sColWidth * 3)) {
314 $colFits[$key] = $stringWidth ;
320 $totAlreadyFitted = 0;
321 foreach ($colFits as $key => $val){
322 // set fitted columns to smallest size
323 $this->tablewidths[$key] = $val;
324 // to work out how much (if any) space has been freed up
325 $totAlreadyFitted += $val;
328 if ($adjustingMode) {
329 $surplus = (sizeof($colFits) * $this->sColWidth) - $totAlreadyFitted;
330 $surplusToAdd = $surplus / ($this->numFields - sizeof($colFits));
331 } else {
332 $surplusToAdd = 0;
335 for ($i=0; $i < $this->numFields; $i++) {
336 if (!in_array($i, array_keys($colFits))) {
337 $this->tablewidths[$i] = $this->sColWidth + $surplusToAdd;
339 if ($this->display_column[$i] == false) {
340 $this->tablewidths[$i] = 0;
344 ksort($this->tablewidths);
346 PMA_DBI_free_result($this->results);
348 // Pass 2
350 $this->results = PMA_DBI_query($query, null, PMA_DBI_QUERY_UNBUFFERED);
351 $this->Open();
352 $this->setY($this->tMargin);
353 $this->AddPage();
354 $this->morepagestable($this->FontSizePt);
355 PMA_DBI_free_result($this->results);
357 } // end of mysql_report function
359 } // end of PMA_PDF class
362 * Outputs comment
364 * @param string Text of comment
366 * @return bool Whether it suceeded
368 function PMA_exportComment($text)
370 return TRUE;
374 * Outputs export footer
376 * @return bool Whether it suceeded
378 * @access public
380 function PMA_exportFooter()
382 return TRUE;
386 * Outputs export header
388 * @return bool Whether it suceeded
390 * @access public
392 function PMA_exportHeader()
394 return TRUE;
398 * Outputs database header
400 * @param string Database name
402 * @return bool Whether it suceeded
404 * @access public
406 function PMA_exportDBHeader($db)
408 return TRUE;
412 * Outputs database footer
414 * @param string Database name
416 * @return bool Whether it suceeded
418 * @access public
420 function PMA_exportDBFooter($db)
422 return TRUE;
426 * Outputs create database database
428 * @param string Database name
430 * @return bool Whether it suceeded
432 * @access public
434 function PMA_exportDBCreate($db)
436 return TRUE;
440 * Outputs the content of a table in PDF format
442 * @todo user-defined page orientation, paper size
443 * @param string the database name
444 * @param string the table name
445 * @param string the end of line sequence
446 * @param string the url to go back in case of error
447 * @param string SQL query for obtaining data
449 * @return bool Whether it suceeded
451 * @access public
453 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
455 global $what;
456 global $pdf_report_title;
458 $pdf = new PMA_PDF('L', 'pt', 'A3');
460 $pdf->AddFont('DejaVuSans', '', 'dejavusans.php');
461 $pdf->AddFont('DejaVuSans', 'B', 'dejavusans-bold.php');
462 $pdf->AddFont('DejaVuSerif', '', 'dejavuserif.php');
463 $pdf->AddFont('DejaVuSerif', 'B', 'dejavuserif-bold.php');
464 $pdf->SetFont(PMA_PDF_FONT, '', 11.5);
465 $pdf->AliasNbPages();
466 $attr=array('titleFontSize' => 18, 'titleText' => $pdf_report_title);
467 $pdf->mysql_report($sql_query, $attr);
469 // instead of $pdf->Output():
470 if ($pdf->getState() < 3) {
471 $pdf->Close();
473 if (!PMA_exportOutputHandler($pdf->getBuffer())) {
474 return FALSE;
477 return TRUE;
478 } // end of the 'PMA_exportData()' function