Translation update done using Pootle.
[phpmyadmin-themes.git] / libraries / export / pdf.php
blob8e2bd53f6459712d9198702e25106c7b370fcba3
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->Cell(0, 10, __('Page number:') .' '.$this->PageNo() .'/{nb}', 'T', 0, 'C');
134 // set footerset
135 $this->footerset[$this->page] = 1;
139 function morepagestable($lineheight=8)
141 // some things to set and 'remember'
142 $l = $this->lMargin;
143 $startheight = $h = $this->dataY;
144 $startpage = $currpage = $this->page;
146 // calculate the whole width
147 $fullwidth = 0;
148 foreach ($this->tablewidths as $width) {
149 $fullwidth += $width;
152 // Now let's start to write the table
153 $row = 0;
154 $tmpheight = array();
155 $maxpage = $this->page;
157 while ($data = PMA_DBI_fetch_row($this->results)) {
158 $this->page = $currpage;
159 // write the horizontal borders
160 $this->Line($l, $h, $fullwidth+$l, $h);
161 // write the content and remember the height of the highest col
162 foreach ($data as $col => $txt) {
163 $this->page = $currpage;
164 $this->SetXY($l, $h);
165 if ($this->tablewidths[$col] > 0) {
166 $this->MultiCell($this->tablewidths[$col], $lineheight, $txt, 0, $this->colAlign[$col]);
167 $l += $this->tablewidths[$col];
170 if (!isset($tmpheight[$row.'-'.$this->page])) {
171 $tmpheight[$row.'-'.$this->page] = 0;
173 if ($tmpheight[$row.'-'.$this->page] < $this->GetY()) {
174 $tmpheight[$row.'-'.$this->page] = $this->GetY();
176 if ($this->page > $maxpage) {
177 $maxpage = $this->page;
179 unset($data[$col]);
182 // get the height we were in the last used page
183 $h = $tmpheight[$row.'-'.$maxpage];
184 // set the "pointer" to the left margin
185 $l = $this->lMargin;
186 // set the $currpage to the last page
187 $currpage = $maxpage;
188 unset($data[$row]);
189 $row++;
191 // draw the borders
192 // we start adding a horizontal line on the last page
193 $this->page = $maxpage;
194 $this->Line($l, $h, $fullwidth+$l, $h);
195 // now we start at the top of the document and walk down
196 for ($i = $startpage; $i <= $maxpage; $i++) {
197 $this->page = $i;
198 $l = $this->lMargin;
199 $t = ($i == $startpage) ? $startheight : $this->tMargin;
200 $lh = ($i == $maxpage) ? $h : $this->h-$this->bMargin;
201 $this->Line($l, $t, $l, $lh);
202 foreach ($this->tablewidths as $width) {
203 $l += $width;
204 $this->Line($l, $t, $l, $lh);
207 // set it to the last page, if not it'll cause some problems
208 $this->page = $maxpage;
211 function setAttributes($attr = array())
213 foreach ($attr as $key => $val){
214 $this->$key = $val ;
218 function setTopMargin($topMargin)
220 $this->tMargin = $topMargin;
223 function mysql_report($query)
225 unset($this->tablewidths);
226 unset($this->colTitles);
227 unset($this->titleWidth);
228 unset($this->colFits);
229 unset($this->display_column);
230 unset($this->colAlign);
233 * Pass 1 for column widths
235 $this->results = PMA_DBI_query($query, null, PMA_DBI_QUERY_UNBUFFERED);
236 $this->numFields = PMA_DBI_num_fields($this->results);
237 $this->fields = PMA_DBI_get_fields_meta($this->results);
239 // sColWidth = starting col width (an average size width)
240 $availableWidth = $this->w - $this->lMargin - $this->rMargin;
241 $this->sColWidth = $availableWidth / $this->numFields;
242 $totalTitleWidth = 0;
244 // loop through results header and set initial col widths/ titles/ alignment
245 // if a col title is less than the starting col width, reduce that column size
246 for ($i = 0; $i < $this->numFields; $i++){
247 $stringWidth = $this->getstringwidth($this->fields[$i]->name) + 6 ;
248 // save the real title's width
249 $titleWidth[$i] = $stringWidth;
250 $totalTitleWidth += $stringWidth;
252 // set any column titles less than the start width to the column title width
253 if ($stringWidth < $this->sColWidth){
254 $colFits[$i] = $stringWidth ;
256 $this->colTitles[$i] = $this->fields[$i]->name;
257 $this->display_column[$i] = true;
259 switch ($this->fields[$i]->type){
260 case 'int':
261 $this->colAlign[$i] = 'R';
262 break;
263 case 'blob':
264 case 'tinyblob':
265 case 'mediumblob':
266 case 'longblob':
268 * @todo do not deactivate completely the display
269 * but show the field's name and [BLOB]
271 if (stristr($this->fields[$i]->flags, 'BINARY')) {
272 $this->display_column[$i] = false;
273 unset($this->colTitles[$i]);
275 $this->colAlign[$i] = 'L';
276 break;
277 default:
278 $this->colAlign[$i] = 'L';
282 // title width verification
283 if ($totalTitleWidth > $availableWidth) {
284 $adjustingMode = true;
285 } else {
286 $adjustingMode = false;
287 // we have enough space for all the titles at their
288 // original width so use the true title's width
289 foreach ($titleWidth as $key => $val) {
290 $colFits[$key] = $val;
294 // loop through the data; any column whose contents
295 // is greater than the column size is resized
297 * @todo force here a LIMIT to avoid reading all rows
299 while ($row = PMA_DBI_fetch_row($this->results)) {
300 foreach ($colFits as $key => $val) {
301 $stringWidth = $this->getstringwidth($row[$key]) + 6 ;
302 if ($adjustingMode && ($stringWidth > $this->sColWidth)) {
303 // any column whose data's width is bigger than the start width is now discarded
304 unset($colFits[$key]);
305 } else {
306 // if data's width is bigger than the current column width,
307 // enlarge the column (but avoid enlarging it if the
308 // data's width is very big)
309 if ($stringWidth > $val && $stringWidth < ($this->sColWidth * 3)) {
310 $colFits[$key] = $stringWidth ;
316 $totAlreadyFitted = 0;
317 foreach ($colFits as $key => $val){
318 // set fitted columns to smallest size
319 $this->tablewidths[$key] = $val;
320 // to work out how much (if any) space has been freed up
321 $totAlreadyFitted += $val;
324 if ($adjustingMode) {
325 $surplus = (sizeof($colFits) * $this->sColWidth) - $totAlreadyFitted;
326 $surplusToAdd = $surplus / ($this->numFields - sizeof($colFits));
327 } else {
328 $surplusToAdd = 0;
331 for ($i=0; $i < $this->numFields; $i++) {
332 if (!in_array($i, array_keys($colFits))) {
333 $this->tablewidths[$i] = $this->sColWidth + $surplusToAdd;
335 if ($this->display_column[$i] == false) {
336 $this->tablewidths[$i] = 0;
340 ksort($this->tablewidths);
342 PMA_DBI_free_result($this->results);
344 // Pass 2
346 $this->results = PMA_DBI_query($query, null, PMA_DBI_QUERY_UNBUFFERED);
347 $this->setY($this->tMargin);
348 $this->AddPage();
349 $this->morepagestable($this->FontSizePt);
350 PMA_DBI_free_result($this->results);
352 } // end of mysql_report function
354 } // end of PMA_PDF class
356 $pdf = new PMA_PDF('L', 'pt', 'A3');
359 * Outputs comment
361 * @param string Text of comment
363 * @return bool Whether it suceeded
365 function PMA_exportComment($text)
367 return TRUE;
371 * Finalize the pdf.
373 * @return bool Whether it suceeded
375 * @access public
377 function PMA_exportFooter()
379 global $pdf;
381 // instead of $pdf->Output():
382 if (!PMA_exportOutputHandler($pdf->getPDFData())) {
383 return FALSE;
386 return TRUE;
390 * Initialize the pdf to export data.
392 * @return bool Whether it suceeded
394 * @access public
396 function PMA_exportHeader()
398 global $pdf_report_title;
399 global $pdf;
401 $pdf->AddFont('DejaVuSans', '', 'dejavusans.php');
402 $pdf->AddFont('DejaVuSans', 'B', 'dejavusansb.php');
403 $pdf->AddFont('DejaVuSerif', '', 'dejavuserif.php');
404 $pdf->AddFont('DejaVuSerif', 'B', 'dejavuserifb.php');
405 $pdf->SetFont(PMA_PDF_FONT, '', 11.5);
406 $pdf->AliasNbPages();
407 $pdf->Open();
409 $attr=array('titleFontSize' => 18, 'titleText' => $pdf_report_title);
410 $pdf->setAttributes($attr);
411 $pdf->setTopMargin(45);
413 return TRUE;
417 * Outputs database header
419 * @param string Database name
421 * @return bool Whether it suceeded
423 * @access public
425 function PMA_exportDBHeader($db)
427 return TRUE;
431 * Outputs database footer
433 * @param string Database name
435 * @return bool Whether it suceeded
437 * @access public
439 function PMA_exportDBFooter($db)
441 return TRUE;
445 * Outputs create database database
447 * @param string Database name
449 * @return bool Whether it suceeded
451 * @access public
453 function PMA_exportDBCreate($db)
455 return TRUE;
459 * Outputs the content of a table in PDF format
461 * @todo user-defined page orientation, paper size
462 * @param string the database name
463 * @param string the table name
464 * @param string the end of line sequence
465 * @param string the url to go back in case of error
466 * @param string SQL query for obtaining data
468 * @return bool Whether it suceeded
470 * @access public
472 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
474 global $pdf;
476 $attr=array('currentDb' => $db, 'currentTable' => $table);
477 $pdf->setAttributes($attr);
478 $pdf->mysql_report($sql_query);
480 return TRUE;
481 } // end of the 'PMA_exportData()' function