Merge branch 'QA_3_4'
[phpmyadmin-regexreplace.git] / libraries / export / pdf.php
blob431df0da9a751b943561c06c3e097ea2df6add70
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 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
15 if (isset($plugin_list)) {
16 $plugin_list['pdf'] = array(
17 'text' => __('PDF'),
18 'extension' => 'pdf',
19 'mime_type' => 'application/pdf',
20 'force_file' => true,
21 'options' => array(
22 array('type' => 'begin_group', 'name' => 'general_opts'),
23 array('type' => 'message_only', 'name' => 'explanation', 'text' => __('(Generates a report containing the data of a single table)')),
24 array('type' => 'text', 'name' => 'report_title', 'text' => __('Report title:')),
25 array('type' => 'hidden', 'name' => 'structure_or_data'),
26 array('type' => 'end_group')
28 'options_text' => __('Options'),
30 } else {
32 /**
33 * Font used in PDF.
35 * @todo Make this configuratble (at least Sans/Serif).
37 define('PMA_PDF_FONT', 'DejaVuSans');
38 require_once './libraries/tcpdf/tcpdf.php';
40 /**
41 * Adapted from a LGPL script by Philip Clarke
42 * @package phpMyAdmin-Export-PDF
44 class PMA_PDF extends TCPDF
46 var $tablewidths;
47 var $headerset;
48 var $footerset;
50 function checkPageBreak($h=0, $y='', $addpage=true) {
51 if ($this->empty_string($y)) {
52 $y = $this->y;
54 $current_page = $this->page;
55 if ((($y + $h) > $this->PageBreakTrigger) AND (!$this->InFooter) AND ($this->AcceptPageBreak())) {
56 if ($addpage) {
57 //Automatic page break
58 $x = $this->x;
59 $this->AddPage($this->CurOrientation);
60 $this->y = $this->dataY;
61 $oldpage = $this->page - 1;
62 if ($this->rtl) {
63 if ($this->pagedim[$this->page]['orm'] != $this->pagedim[$oldpage]['orm']) {
64 $this->x = $x - ($this->pagedim[$this->page]['orm'] - $this->pagedim[$oldpage]['orm']);
65 } else {
66 $this->x = $x;
68 } else {
69 if ($this->pagedim[$this->page]['olm'] != $this->pagedim[$oldpage]['olm']) {
70 $this->x = $x + ($this->pagedim[$this->page]['olm'] - $this->pagedim[$oldpage]['olm']);
71 } else {
72 $this->x = $x;
76 return true;
78 if ($current_page != $this->page) {
79 // account for columns mode
80 return true;
82 return false;
85 function Header()
87 global $maxY;
88 // Check if header for this page already exists
89 if (!isset($this->headerset[$this->page])) {
90 $fullwidth = 0;
91 foreach ($this->tablewidths as $width) {
92 $fullwidth += $width;
94 $this->SetY(($this->tMargin) - ($this->FontSizePt/$this->k)*3);
95 $this->cellFontSize = $this->FontSizePt ;
96 $this->SetFont(PMA_PDF_FONT, '', ($this->titleFontSize ? $this->titleFontSize : $this->FontSizePt));
97 $this->Cell(0, $this->FontSizePt, $this->titleText, 0, 1, 'C');
98 $this->SetFont(PMA_PDF_FONT, '', $this->cellFontSize);
99 $this->SetY(($this->tMargin) - ($this->FontSizePt/$this->k)*1.5);
100 $this->Cell(0, $this->FontSizePt, __('Database') .': ' .$this->currentDb .', ' .__('Table') .': ' .$this->currentTable, 0, 1, 'L');
101 $l = ($this->lMargin);
102 foreach ($this->colTitles as $col => $txt) {
103 $this->SetXY($l, ($this->tMargin));
104 $this->MultiCell($this->tablewidths[$col], $this->FontSizePt, $txt);
105 $l += $this->tablewidths[$col] ;
106 $maxY = ($maxY < $this->getY()) ? $this->getY() : $maxY ;
108 $this->SetXY($this->lMargin, $this->tMargin);
109 $this->setFillColor(200, 200, 200);
110 $l = ($this->lMargin);
111 foreach ($this->colTitles as $col => $txt) {
112 $this->SetXY($l, $this->tMargin);
113 $this->cell($this->tablewidths[$col], $maxY-($this->tMargin), '', 1, 0, 'L', 1);
114 $this->SetXY($l, $this->tMargin);
115 $this->MultiCell($this->tablewidths[$col], $this->FontSizePt, $txt, 0, 'C');
116 $l += $this->tablewidths[$col];
118 $this->setFillColor(255, 255, 255);
119 // set headerset
120 $this->headerset[$this->page] = 1;
123 $this->dataY = $maxY;
126 function Footer()
128 // Check if footer for this page already exists
129 if (!isset($this->footerset[$this->page])) {
130 $this->SetY(-15);
131 //Page number
132 $this->setFooterFont(PMA_PDF_FONT, '', 14);
133 $this->Cell(0, 6, __('Page number:') . ' ' . $this->getAliasNumPage() . '/' . $this->getAliasNbPages(), 'T', 0, 'C');
135 // set footerset
136 $this->footerset[$this->page] = 1;
140 function morepagestable($lineheight=8)
142 // some things to set and 'remember'
143 $l = $this->lMargin;
144 $startheight = $h = $this->dataY;
145 $startpage = $currpage = $this->page;
147 // calculate the whole width
148 $fullwidth = 0;
149 foreach ($this->tablewidths as $width) {
150 $fullwidth += $width;
153 // Now let's start to write the table
154 $row = 0;
155 $tmpheight = array();
156 $maxpage = $this->page;
158 while ($data = PMA_DBI_fetch_row($this->results)) {
159 $this->page = $currpage;
160 // write the horizontal borders
161 $this->Line($l, $h, $fullwidth+$l, $h);
162 // write the content and remember the height of the highest col
163 foreach ($data as $col => $txt) {
164 $this->page = $currpage;
165 $this->SetXY($l, $h);
166 if ($this->tablewidths[$col] > 0) {
167 $this->MultiCell($this->tablewidths[$col], $lineheight, $txt, 0, $this->colAlign[$col]);
168 $l += $this->tablewidths[$col];
171 if (!isset($tmpheight[$row.'-'.$this->page])) {
172 $tmpheight[$row.'-'.$this->page] = 0;
174 if ($tmpheight[$row.'-'.$this->page] < $this->GetY()) {
175 $tmpheight[$row.'-'.$this->page] = $this->GetY();
177 if ($this->page > $maxpage) {
178 $maxpage = $this->page;
180 unset($data[$col]);
183 // get the height we were in the last used page
184 $h = $tmpheight[$row.'-'.$maxpage];
185 // set the "pointer" to the left margin
186 $l = $this->lMargin;
187 // set the $currpage to the last page
188 $currpage = $maxpage;
189 unset($data[$row]);
190 $row++;
192 // draw the borders
193 // we start adding a horizontal line on the last page
194 $this->page = $maxpage;
195 $this->Line($l, $h, $fullwidth+$l, $h);
196 // now we start at the top of the document and walk down
197 for ($i = $startpage; $i <= $maxpage; $i++) {
198 $this->page = $i;
199 $l = $this->lMargin;
200 $t = ($i == $startpage) ? $startheight : $this->tMargin;
201 $lh = ($i == $maxpage) ? $h : $this->h-$this->bMargin;
202 $this->Line($l, $t, $l, $lh);
203 foreach ($this->tablewidths as $width) {
204 $l += $width;
205 $this->Line($l, $t, $l, $lh);
208 // set it to the last page, if not it'll cause some problems
209 $this->page = $maxpage;
212 function setAttributes($attr = array())
214 foreach ($attr as $key => $val){
215 $this->$key = $val ;
219 function setTopMargin($topMargin)
221 $this->tMargin = $topMargin;
224 function mysql_report($query)
226 unset($this->tablewidths);
227 unset($this->colTitles);
228 unset($this->titleWidth);
229 unset($this->colFits);
230 unset($this->display_column);
231 unset($this->colAlign);
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 // sColWidth = starting col width (an average size width)
241 $availableWidth = $this->w - $this->lMargin - $this->rMargin;
242 $this->sColWidth = $availableWidth / $this->numFields;
243 $totalTitleWidth = 0;
245 // loop through results header and set initial col widths/ titles/ alignment
246 // if a col title is less than the starting col width, reduce that column size
247 for ($i = 0; $i < $this->numFields; $i++){
248 $stringWidth = $this->getstringwidth($this->fields[$i]->name) + 6 ;
249 // save the real title's width
250 $titleWidth[$i] = $stringWidth;
251 $totalTitleWidth += $stringWidth;
253 // set any column titles less than the start width to the column title width
254 if ($stringWidth < $this->sColWidth){
255 $colFits[$i] = $stringWidth ;
257 $this->colTitles[$i] = $this->fields[$i]->name;
258 $this->display_column[$i] = true;
260 switch ($this->fields[$i]->type){
261 case 'int':
262 $this->colAlign[$i] = 'R';
263 break;
264 case 'blob':
265 case 'tinyblob':
266 case 'mediumblob':
267 case 'longblob':
269 * @todo do not deactivate completely the display
270 * but show the field's name and [BLOB]
272 if (stristr($this->fields[$i]->flags, 'BINARY')) {
273 $this->display_column[$i] = false;
274 unset($this->colTitles[$i]);
276 $this->colAlign[$i] = 'L';
277 break;
278 default:
279 $this->colAlign[$i] = 'L';
283 // title width verification
284 if ($totalTitleWidth > $availableWidth) {
285 $adjustingMode = true;
286 } else {
287 $adjustingMode = false;
288 // we have enough space for all the titles at their
289 // original width so use the true title's width
290 foreach ($titleWidth as $key => $val) {
291 $colFits[$key] = $val;
295 // loop through the data; any column whose contents
296 // is greater than the column size is resized
298 * @todo force here a LIMIT to avoid reading all rows
300 while ($row = PMA_DBI_fetch_row($this->results)) {
301 foreach ($colFits as $key => $val) {
302 $stringWidth = $this->getstringwidth($row[$key]) + 6 ;
303 if ($adjustingMode && ($stringWidth > $this->sColWidth)) {
304 // any column whose data's width is bigger than the start width is now discarded
305 unset($colFits[$key]);
306 } else {
307 // if data's width is bigger than the current column width,
308 // enlarge the column (but avoid enlarging it if the
309 // data's width is very big)
310 if ($stringWidth > $val && $stringWidth < ($this->sColWidth * 3)) {
311 $colFits[$key] = $stringWidth ;
317 $totAlreadyFitted = 0;
318 foreach ($colFits as $key => $val){
319 // set fitted columns to smallest size
320 $this->tablewidths[$key] = $val;
321 // to work out how much (if any) space has been freed up
322 $totAlreadyFitted += $val;
325 if ($adjustingMode) {
326 $surplus = (sizeof($colFits) * $this->sColWidth) - $totAlreadyFitted;
327 $surplusToAdd = $surplus / ($this->numFields - sizeof($colFits));
328 } else {
329 $surplusToAdd = 0;
332 for ($i=0; $i < $this->numFields; $i++) {
333 if (!in_array($i, array_keys($colFits))) {
334 $this->tablewidths[$i] = $this->sColWidth + $surplusToAdd;
336 if ($this->display_column[$i] == false) {
337 $this->tablewidths[$i] = 0;
341 ksort($this->tablewidths);
343 PMA_DBI_free_result($this->results);
345 // Pass 2
347 $this->results = PMA_DBI_query($query, null, PMA_DBI_QUERY_UNBUFFERED);
348 $this->setY($this->tMargin);
349 $this->AddPage();
350 $this->morepagestable($this->FontSizePt);
351 PMA_DBI_free_result($this->results);
353 } // end of mysql_report function
355 } // end of PMA_PDF class
357 $pdf = new PMA_PDF('L', 'pt', 'A3');
360 * Outputs comment
362 * @param string Text of comment
364 * @return bool Whether it suceeded
366 function PMA_exportComment($text)
368 return TRUE;
372 * Finalize the pdf.
374 * @return bool Whether it suceeded
376 * @access public
378 function PMA_exportFooter()
380 global $pdf;
382 // instead of $pdf->Output():
383 if (!PMA_exportOutputHandler($pdf->getPDFData())) {
384 return FALSE;
387 return TRUE;
391 * Initialize the pdf to export data.
393 * @return bool Whether it suceeded
395 * @access public
397 function PMA_exportHeader()
399 global $pdf_report_title;
400 global $pdf;
402 $pdf->AddFont('DejaVuSans', '', 'dejavusans.php');
403 $pdf->AddFont('DejaVuSans', 'B', 'dejavusansb.php');
404 $pdf->AddFont('DejaVuSerif', '', 'dejavuserif.php');
405 $pdf->AddFont('DejaVuSerif', 'B', 'dejavuserifb.php');
406 $pdf->SetFont(PMA_PDF_FONT, '', 11.5);
407 $pdf->setFooterFont(array(PMA_PDF_FONT, '', 11.5));
408 $pdf->AliasNbPages();
409 $pdf->Open();
411 $attr=array('titleFontSize' => 18, 'titleText' => $pdf_report_title);
412 $pdf->setAttributes($attr);
413 $pdf->setTopMargin(45);
415 return TRUE;
419 * Outputs database header
421 * @param string Database name
423 * @return bool Whether it suceeded
425 * @access public
427 function PMA_exportDBHeader($db)
429 return TRUE;
433 * Outputs database footer
435 * @param string Database name
437 * @return bool Whether it suceeded
439 * @access public
441 function PMA_exportDBFooter($db)
443 return TRUE;
447 * Outputs create database database
449 * @param string Database name
451 * @return bool Whether it suceeded
453 * @access public
455 function PMA_exportDBCreate($db)
457 return TRUE;
461 * Outputs the content of a table in PDF format
463 * @todo user-defined page orientation, paper size
464 * @param string the database name
465 * @param string the table name
466 * @param string the end of line sequence
467 * @param string the url to go back in case of error
468 * @param string SQL query for obtaining data
470 * @return bool Whether it suceeded
472 * @access public
474 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
476 global $pdf;
478 $attr=array('currentDb' => $db, 'currentTable' => $table);
479 $pdf->setAttributes($attr);
480 $pdf->mysql_report($sql_query);
482 return TRUE;
483 } // end of the 'PMA_exportData()' function