This should be checked in common, not in dbi library
[phpmyadmin.git] / libraries / export / pdf.php
blob01ffc88be150b8ff50f40ce5742e0547d54c85de
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 /**
34 * Font used in PDF.
36 * @todo Make this configuratble (at least Sans/Serif).
38 define('PMA_PDF_FONT', 'DejaVuSans');
39 require_once './libraries/tcpdf/tcpdf.php';
41 /**
42 * Adapted from a LGPL script by Philip Clarke
43 * @package phpMyAdmin-Export
44 * @subpackage PDF
46 class PMA_PDF extends TCPDF
48 var $tablewidths;
49 var $headerset;
50 var $footerset;
52 function checkPageBreak($h=0, $y='', $addpage=true) {
53 if ($this->empty_string($y)) {
54 $y = $this->y;
56 $current_page = $this->page;
57 if ((($y + $h) > $this->PageBreakTrigger) AND (!$this->InFooter) AND ($this->AcceptPageBreak())) {
58 if ($addpage) {
59 //Automatic page break
60 $x = $this->x;
61 $this->AddPage($this->CurOrientation);
62 $this->y = $this->dataY;
63 $oldpage = $this->page - 1;
64 if ($this->rtl) {
65 if ($this->pagedim[$this->page]['orm'] != $this->pagedim[$oldpage]['orm']) {
66 $this->x = $x - ($this->pagedim[$this->page]['orm'] - $this->pagedim[$oldpage]['orm']);
67 } else {
68 $this->x = $x;
70 } else {
71 if ($this->pagedim[$this->page]['olm'] != $this->pagedim[$oldpage]['olm']) {
72 $this->x = $x + ($this->pagedim[$this->page]['olm'] - $this->pagedim[$oldpage]['olm']);
73 } else {
74 $this->x = $x;
78 return true;
80 if ($current_page != $this->page) {
81 // account for columns mode
82 return true;
84 return false;
87 function Header()
89 global $maxY;
90 // Check if header for this page already exists
91 if (!isset($this->headerset[$this->page])) {
92 $fullwidth = 0;
93 foreach ($this->tablewidths as $width) {
94 $fullwidth += $width;
96 $this->SetY(($this->tMargin) - ($this->FontSizePt/$this->k)*3);
97 $this->cellFontSize = $this->FontSizePt ;
98 $this->SetFont(PMA_PDF_FONT, '', ($this->titleFontSize ? $this->titleFontSize : $this->FontSizePt));
99 $this->Cell(0, $this->FontSizePt, $this->titleText, 0, 1, 'C');
100 $this->SetFont(PMA_PDF_FONT, '', $this->cellFontSize);
101 $this->SetY(($this->tMargin) - ($this->FontSizePt/$this->k)*1.5);
102 $this->Cell(0, $this->FontSizePt, __('Database') .': ' .$this->currentDb .', ' .__('Table') .': ' .$this->currentTable, 0, 1, 'L');
103 $l = ($this->lMargin);
104 foreach ($this->colTitles as $col => $txt) {
105 $this->SetXY($l, ($this->tMargin));
106 $this->MultiCell($this->tablewidths[$col], $this->FontSizePt, $txt);
107 $l += $this->tablewidths[$col] ;
108 $maxY = ($maxY < $this->getY()) ? $this->getY() : $maxY ;
110 $this->SetXY($this->lMargin, $this->tMargin);
111 $this->setFillColor(200, 200, 200);
112 $l = ($this->lMargin);
113 foreach ($this->colTitles as $col => $txt) {
114 $this->SetXY($l, $this->tMargin);
115 $this->cell($this->tablewidths[$col], $maxY-($this->tMargin), '', 1, 0, 'L', 1);
116 $this->SetXY($l, $this->tMargin);
117 $this->MultiCell($this->tablewidths[$col], $this->FontSizePt, $txt, 0, 'C');
118 $l += $this->tablewidths[$col];
120 $this->setFillColor(255, 255, 255);
121 // set headerset
122 $this->headerset[$this->page] = 1;
125 $this->dataY = $maxY;
128 function Footer()
130 // Check if footer for this page already exists
131 if (!isset($this->footerset[$this->page])) {
132 $this->SetY(-15);
133 //Page number
134 $this->setFooterFont(PMA_PDF_FONT, '', 14);
135 $this->Cell(0, 6, __('Page number:') . ' ' . $this->getAliasNumPage() . '/' . $this->getAliasNbPages(), 'T', 0, 'C');
137 // set footerset
138 $this->footerset[$this->page] = 1;
142 function morepagestable($lineheight=8)
144 // some things to set and 'remember'
145 $l = $this->lMargin;
146 $startheight = $h = $this->dataY;
147 $startpage = $currpage = $this->page;
149 // calculate the whole width
150 $fullwidth = 0;
151 foreach ($this->tablewidths as $width) {
152 $fullwidth += $width;
155 // Now let's start to write the table
156 $row = 0;
157 $tmpheight = array();
158 $maxpage = $this->page;
160 while ($data = PMA_DBI_fetch_row($this->results)) {
161 $this->page = $currpage;
162 // write the horizontal borders
163 $this->Line($l, $h, $fullwidth+$l, $h);
164 // write the content and remember the height of the highest col
165 foreach ($data as $col => $txt) {
166 $this->page = $currpage;
167 $this->SetXY($l, $h);
168 if ($this->tablewidths[$col] > 0) {
169 $this->MultiCell($this->tablewidths[$col], $lineheight, $txt, 0, $this->colAlign[$col]);
170 $l += $this->tablewidths[$col];
173 if (!isset($tmpheight[$row.'-'.$this->page])) {
174 $tmpheight[$row.'-'.$this->page] = 0;
176 if ($tmpheight[$row.'-'.$this->page] < $this->GetY()) {
177 $tmpheight[$row.'-'.$this->page] = $this->GetY();
179 if ($this->page > $maxpage) {
180 $maxpage = $this->page;
182 unset($data[$col]);
185 // get the height we were in the last used page
186 $h = $tmpheight[$row.'-'.$maxpage];
187 // set the "pointer" to the left margin
188 $l = $this->lMargin;
189 // set the $currpage to the last page
190 $currpage = $maxpage;
191 unset($data[$row]);
192 $row++;
194 // draw the borders
195 // we start adding a horizontal line on the last page
196 $this->page = $maxpage;
197 $this->Line($l, $h, $fullwidth+$l, $h);
198 // now we start at the top of the document and walk down
199 for ($i = $startpage; $i <= $maxpage; $i++) {
200 $this->page = $i;
201 $l = $this->lMargin;
202 $t = ($i == $startpage) ? $startheight : $this->tMargin;
203 $lh = ($i == $maxpage) ? $h : $this->h-$this->bMargin;
204 $this->Line($l, $t, $l, $lh);
205 foreach ($this->tablewidths as $width) {
206 $l += $width;
207 $this->Line($l, $t, $l, $lh);
210 // set it to the last page, if not it'll cause some problems
211 $this->page = $maxpage;
214 function setAttributes($attr = array())
216 foreach ($attr as $key => $val) {
217 $this->$key = $val ;
221 function setTopMargin($topMargin)
223 $this->tMargin = $topMargin;
226 function mysql_report($query)
228 unset($this->tablewidths);
229 unset($this->colTitles);
230 unset($this->titleWidth);
231 unset($this->colFits);
232 unset($this->display_column);
233 unset($this->colAlign);
236 * Pass 1 for column widths
238 $this->results = PMA_DBI_query($query, null, PMA_DBI_QUERY_UNBUFFERED);
239 $this->numFields = PMA_DBI_num_fields($this->results);
240 $this->fields = PMA_DBI_get_fields_meta($this->results);
242 // sColWidth = starting col width (an average size width)
243 $availableWidth = $this->w - $this->lMargin - $this->rMargin;
244 $this->sColWidth = $availableWidth / $this->numFields;
245 $totalTitleWidth = 0;
247 // loop through results header and set initial col widths/ titles/ alignment
248 // if a col title is less than the starting col width, reduce that column size
249 for ($i = 0; $i < $this->numFields; $i++) {
250 $stringWidth = $this->getstringwidth($this->fields[$i]->name) + 6 ;
251 // save the real title's width
252 $titleWidth[$i] = $stringWidth;
253 $totalTitleWidth += $stringWidth;
255 // set any column titles less than the start width to the column title width
256 if ($stringWidth < $this->sColWidth) {
257 $colFits[$i] = $stringWidth ;
259 $this->colTitles[$i] = $this->fields[$i]->name;
260 $this->display_column[$i] = true;
262 switch ($this->fields[$i]->type) {
263 case 'int':
264 $this->colAlign[$i] = 'R';
265 break;
266 case 'blob':
267 case 'tinyblob':
268 case 'mediumblob':
269 case 'longblob':
271 * @todo do not deactivate completely the display
272 * but show the field's name and [BLOB]
274 if (stristr($this->fields[$i]->flags, 'BINARY')) {
275 $this->display_column[$i] = false;
276 unset($this->colTitles[$i]);
278 $this->colAlign[$i] = 'L';
279 break;
280 default:
281 $this->colAlign[$i] = 'L';
285 // title width verification
286 if ($totalTitleWidth > $availableWidth) {
287 $adjustingMode = true;
288 } else {
289 $adjustingMode = false;
290 // we have enough space for all the titles at their
291 // original width so use the true title's width
292 foreach ($titleWidth as $key => $val) {
293 $colFits[$key] = $val;
297 // loop through the data; any column whose contents
298 // is greater than the column size is resized
300 * @todo force here a LIMIT to avoid reading all rows
302 while ($row = PMA_DBI_fetch_row($this->results)) {
303 foreach ($colFits as $key => $val) {
304 $stringWidth = $this->getstringwidth($row[$key]) + 6 ;
305 if ($adjustingMode && ($stringWidth > $this->sColWidth)) {
306 // any column whose data's width is bigger than the start width is now discarded
307 unset($colFits[$key]);
308 } else {
309 // if data's width is bigger than the current column width,
310 // enlarge the column (but avoid enlarging it if the
311 // data's width is very big)
312 if ($stringWidth > $val && $stringWidth < ($this->sColWidth * 3)) {
313 $colFits[$key] = $stringWidth ;
319 $totAlreadyFitted = 0;
320 foreach ($colFits as $key => $val) {
321 // set fitted columns to smallest size
322 $this->tablewidths[$key] = $val;
323 // to work out how much (if any) space has been freed up
324 $totAlreadyFitted += $val;
327 if ($adjustingMode) {
328 $surplus = (sizeof($colFits) * $this->sColWidth) - $totAlreadyFitted;
329 $surplusToAdd = $surplus / ($this->numFields - sizeof($colFits));
330 } else {
331 $surplusToAdd = 0;
334 for ($i=0; $i < $this->numFields; $i++) {
335 if (!in_array($i, array_keys($colFits))) {
336 $this->tablewidths[$i] = $this->sColWidth + $surplusToAdd;
338 if ($this->display_column[$i] == false) {
339 $this->tablewidths[$i] = 0;
343 ksort($this->tablewidths);
345 PMA_DBI_free_result($this->results);
347 // Pass 2
349 $this->results = PMA_DBI_query($query, null, PMA_DBI_QUERY_UNBUFFERED);
350 $this->setY($this->tMargin);
351 $this->AddPage();
352 $this->morepagestable($this->FontSizePt);
353 PMA_DBI_free_result($this->results);
355 } // end of mysql_report function
357 } // end of PMA_PDF class
359 $pdf = new PMA_PDF('L', 'pt', 'A3');
362 * Finalize the pdf.
364 * @return bool Whether it suceeded
366 * @access public
368 function PMA_exportFooter()
370 global $pdf;
372 // instead of $pdf->Output():
373 if (!PMA_exportOutputHandler($pdf->getPDFData())) {
374 return false;
377 return true;
381 * Initialize the pdf to export data.
383 * @return bool Whether it suceeded
385 * @access public
387 function PMA_exportHeader()
389 global $pdf_report_title;
390 global $pdf;
392 $pdf->AddFont('DejaVuSans', '', 'dejavusans.php');
393 $pdf->AddFont('DejaVuSans', 'B', 'dejavusansb.php');
394 $pdf->AddFont('DejaVuSerif', '', 'dejavuserif.php');
395 $pdf->AddFont('DejaVuSerif', 'B', 'dejavuserifb.php');
396 $pdf->SetFont(PMA_PDF_FONT, '', 11.5);
397 $pdf->setFooterFont(array(PMA_PDF_FONT, '', 11.5));
398 $pdf->AliasNbPages();
399 $pdf->Open();
401 $attr=array('titleFontSize' => 18, 'titleText' => $pdf_report_title);
402 $pdf->setAttributes($attr);
403 $pdf->setTopMargin(45);
405 return true;
409 * Outputs database header
411 * @param string $db Database name
412 * @return bool Whether it suceeded
414 * @access public
416 function PMA_exportDBHeader($db)
418 return true;
422 * Outputs database footer
424 * @param string $db Database name
425 * @return bool Whether it suceeded
427 * @access public
429 function PMA_exportDBFooter($db)
431 return true;
435 * Outputs CREATE DATABASE statement
437 * @param string $db Database name
438 * @return bool Whether it suceeded
440 * @access public
442 function PMA_exportDBCreate($db)
444 return true;
448 * Outputs the content of a table in PDF format
450 * @param string $db database name
451 * @param string $table table name
452 * @param string $crlf the end of line sequence
453 * @param string $error_url the url to go back in case of error
454 * @param string $sql_query SQL query for obtaining data
455 * @return bool Whether it suceeded
457 * @access public
459 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
461 global $pdf;
463 $attr=array('currentDb' => $db, 'currentTable' => $table);
464 $pdf->setAttributes($attr);
465 $pdf->mysql_report($sql_query);
467 return true;
468 } // end of the 'PMA_exportData()' function