jquery components should be under js/jquery
[phpmyadmin/ninadsp.git] / libraries / export / pdf.php
blob10b4ea47975367c0ad70a0a8a052d34a3281bee4
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 // overloading of a tcpdf function:
51 function _beginpage($orientation)
53 $this->page++;
54 // solved the problem of overwriting a page, if it already exists
55 if (!isset($this->pages[$this->page])) {
56 $this->pages[$this->page] = '';
58 $this->state = 2;
59 $this->x = $this->lMargin;
60 $this->y = $this->tMargin;
61 $this->lasth = 0;
62 $this->FontFamily = '';
64 //Page orientation
65 if (!$orientation) {
66 $orientation = $this->DefOrientation;
67 } else {
68 $orientation = strtoupper($orientation{0});
69 if ($orientation != $this->DefOrientation) {
70 $this->OrientationChanges[$this->page] = true;
73 if ($orientation != $this->CurOrientation) {
74 //Change orientation
75 if ($orientation == 'P') {
76 $this->wPt = $this->fwPt;
77 $this->hPt = $this->fhPt;
78 $this->w = $this->fw;
79 $this->h = $this->fh;
80 } else {
81 $this->wPt = $this->fhPt;
82 $this->hPt = $this->fwPt;
83 $this->w = $this->fh;
84 $this->h = $this->fw;
86 $this->PageBreakTrigger = $this->h - $this->bMargin;
87 $this->CurOrientation = $orientation;
91 function Header()
93 global $maxY;
95 // Check if header for this page already exists
96 if (!isset($this->headerset[$this->page])) {
97 $fullwidth = 0;
98 foreach ($this->tablewidths as $width) {
99 $fullwidth += $width;
101 $this->SetY(($this->tMargin) - ($this->FontSizePt/$this->k)*2);
102 $this->cellFontSize = $this->FontSizePt ;
103 $this->SetFont(PMA_PDF_FONT, '', ($this->titleFontSize ? $this->titleFontSize : $this->FontSizePt));
104 $this->Cell(0, $this->FontSizePt, $this->titleText, 0, 1, 'C');
105 $l = ($this->lMargin);
106 $this->SetFont(PMA_PDF_FONT, '', $this->cellFontSize);
107 foreach ($this->colTitles as $col => $txt) {
108 $this->SetXY($l, ($this->tMargin));
109 $this->MultiCell($this->tablewidths[$col], $this->FontSizePt, $txt);
110 $l += $this->tablewidths[$col] ;
111 $maxY = ($maxY < $this->getY()) ? $this->getY() : $maxY ;
113 $this->SetXY($this->lMargin, $this->tMargin);
114 $this->setFillColor(200, 200, 200);
115 $l = ($this->lMargin);
116 foreach ($this->colTitles as $col => $txt) {
117 $this->SetXY($l, $this->tMargin);
118 $this->cell($this->tablewidths[$col], $maxY-($this->tMargin), '', 1, 0, 'L', 1);
119 $this->SetXY($l, $this->tMargin);
120 $this->MultiCell($this->tablewidths[$col], $this->FontSizePt, $txt, 0, 'C');
121 $l += $this->tablewidths[$col];
123 $this->setFillColor(255, 255, 255);
124 // set headerset
125 $this->headerset[$this->page] = 1;
128 $this->SetY($maxY);
131 function Footer()
133 // Check if footer for this page already exists
134 if (!isset($this->footerset[$this->page])) {
135 $this->SetY(-15);
136 //Page number
137 $this->Cell(0, 10, __('Page number:') .' '.$this->PageNo() .'/{nb}', 'T', 0, 'C');
139 // set footerset
140 $this->footerset[$this->page] = 1;
144 function morepagestable($lineheight=8)
146 // some things to set and 'remember'
147 $l = $this->lMargin;
148 $startheight = $h = $this->GetY();
149 $startpage = $currpage = $this->page;
151 // calculate the whole width
152 $fullwidth = 0;
153 foreach ($this->tablewidths as $width) {
154 $fullwidth += $width;
157 // Now let's start to write the table
158 $row = 0;
159 $tmpheight = array();
160 $maxpage = 0;
162 while ($data = PMA_DBI_fetch_row($this->results)) {
163 $this->page = $currpage;
164 // write the horizontal borders
165 $this->Line($l, $h, $fullwidth+$l, $h);
166 // write the content and remember the height of the highest col
167 foreach ($data as $col => $txt) {
168 $this->page = $currpage;
169 $this->SetXY($l, $h);
170 if ($this->tablewidths[$col] > 0) {
171 $this->MultiCell($this->tablewidths[$col], $lineheight, $txt, 0, $this->colAlign[$col]);
172 $l += $this->tablewidths[$col];
175 if (!isset($tmpheight[$row.'-'.$this->page])) {
176 $tmpheight[$row.'-'.$this->page] = 0;
178 if ($tmpheight[$row.'-'.$this->page] < $this->GetY()) {
179 $tmpheight[$row.'-'.$this->page] = $this->GetY();
181 if ($this->page > $maxpage) {
182 $maxpage = $this->page;
184 unset($data[$col]);
187 // get the height we were in the last used page
188 $h = $tmpheight[$row.'-'.$maxpage];
189 // set the "pointer" to the left margin
190 $l = $this->lMargin;
191 // set the $currpage to the last page
192 $currpage = $maxpage;
193 unset($data[$row]);
194 $row++;
196 // draw the borders
197 // we start adding a horizontal line on the last page
198 $this->page = $maxpage;
199 $this->Line($l, $h, $fullwidth+$l, $h);
200 // now we start at the top of the document and walk down
201 for ($i = $startpage; $i <= $maxpage; $i++) {
202 $this->page = $i;
203 $l = $this->lMargin;
204 $t = ($i == $startpage) ? $startheight : $this->tMargin;
205 $lh = ($i == $maxpage) ? $h : $this->h-$this->bMargin;
206 $this->Line($l, $t, $l, $lh);
207 foreach ($this->tablewidths as $width) {
208 $l += $width;
209 $this->Line($l, $t, $l, $lh);
212 // set it to the last page, if not it'll cause some problems
213 $this->page = $maxpage;
217 function mysql_report($query, $attr = array())
219 foreach ($attr as $key => $val){
220 $this->$key = $val ;
224 * Pass 1 for column widths
226 $this->results = PMA_DBI_query($query, null, PMA_DBI_QUERY_UNBUFFERED);
227 $this->numFields = PMA_DBI_num_fields($this->results);
228 $this->fields = PMA_DBI_get_fields_meta($this->results);
230 // if column widths not set
231 if (!isset($this->tablewidths)){
233 // sColWidth = starting col width (an average size width)
234 $availableWidth = $this->w - $this->lMargin - $this->rMargin;
235 $this->sColWidth = $availableWidth / $this->numFields;
236 $totalTitleWidth = 0;
238 // loop through results header and set initial col widths/ titles/ alignment
239 // if a col title is less than the starting col width, reduce that column size
240 for ($i = 0; $i < $this->numFields; $i++){
241 $stringWidth = $this->getstringwidth($this->fields[$i]->name) + 6 ;
242 // save the real title's width
243 $titleWidth[$i] = $stringWidth;
244 $totalTitleWidth += $stringWidth;
246 // set any column titles less than the start width to the column title width
247 if ($stringWidth < $this->sColWidth){
248 $colFits[$i] = $stringWidth ;
250 $this->colTitles[$i] = $this->fields[$i]->name;
251 $this->display_column[$i] = true;
253 switch ($this->fields[$i]->type){
254 case 'int':
255 $this->colAlign[$i] = 'R';
256 break;
257 case 'blob':
258 case 'tinyblob':
259 case 'mediumblob':
260 case 'longblob':
262 * @todo do not deactivate completely the display
263 * but show the field's name and [BLOB]
265 if (stristr($this->fields[$i]->flags, 'BINARY')) {
266 $this->display_column[$i] = false;
267 unset($this->colTitles[$i]);
269 $this->colAlign[$i] = 'L';
270 break;
271 default:
272 $this->colAlign[$i] = 'L';
276 // title width verification
277 if ($totalTitleWidth > $availableWidth) {
278 $adjustingMode = true;
279 } else {
280 $adjustingMode = false;
281 // we have enough space for all the titles at their
282 // original width so use the true title's width
283 foreach ($titleWidth as $key => $val) {
284 $colFits[$key] = $val;
288 // loop through the data; any column whose contents
289 // is greater than the column size is resized
291 * @todo force here a LIMIT to avoid reading all rows
293 while ($row = PMA_DBI_fetch_row($this->results)) {
294 foreach ($colFits as $key => $val) {
295 $stringWidth = $this->getstringwidth($row[$key]) + 6 ;
296 if ($adjustingMode && ($stringWidth > $this->sColWidth)) {
297 // any column whose data's width is bigger than the start width is now discarded
298 unset($colFits[$key]);
299 } else {
300 // if data's width is bigger than the current column width,
301 // enlarge the column (but avoid enlarging it if the
302 // data's width is very big)
303 if ($stringWidth > $val && $stringWidth < ($this->sColWidth * 3)) {
304 $colFits[$key] = $stringWidth ;
310 $totAlreadyFitted = 0;
311 foreach ($colFits as $key => $val){
312 // set fitted columns to smallest size
313 $this->tablewidths[$key] = $val;
314 // to work out how much (if any) space has been freed up
315 $totAlreadyFitted += $val;
318 if ($adjustingMode) {
319 $surplus = (sizeof($colFits) * $this->sColWidth) - $totAlreadyFitted;
320 $surplusToAdd = $surplus / ($this->numFields - sizeof($colFits));
321 } else {
322 $surplusToAdd = 0;
325 for ($i=0; $i < $this->numFields; $i++) {
326 if (!in_array($i, array_keys($colFits))) {
327 $this->tablewidths[$i] = $this->sColWidth + $surplusToAdd;
329 if ($this->display_column[$i] == false) {
330 $this->tablewidths[$i] = 0;
334 ksort($this->tablewidths);
336 PMA_DBI_free_result($this->results);
338 // Pass 2
340 $this->results = PMA_DBI_query($query, null, PMA_DBI_QUERY_UNBUFFERED);
341 $this->Open();
342 $this->setY($this->tMargin);
343 $this->AddPage();
344 $this->morepagestable($this->FontSizePt);
345 PMA_DBI_free_result($this->results);
347 } // end of mysql_report function
349 } // end of PMA_PDF class
352 * Outputs comment
354 * @param string Text of comment
356 * @return bool Whether it suceeded
358 function PMA_exportComment($text)
360 return TRUE;
364 * Outputs export footer
366 * @return bool Whether it suceeded
368 * @access public
370 function PMA_exportFooter()
372 return TRUE;
376 * Outputs export header
378 * @return bool Whether it suceeded
380 * @access public
382 function PMA_exportHeader()
384 return TRUE;
388 * Outputs database header
390 * @param string Database name
392 * @return bool Whether it suceeded
394 * @access public
396 function PMA_exportDBHeader($db)
398 return TRUE;
402 * Outputs database footer
404 * @param string Database name
406 * @return bool Whether it suceeded
408 * @access public
410 function PMA_exportDBFooter($db)
412 return TRUE;
416 * Outputs create database database
418 * @param string Database name
420 * @return bool Whether it suceeded
422 * @access public
424 function PMA_exportDBCreate($db)
426 return TRUE;
430 * Outputs the content of a table in PDF format
432 * @todo user-defined page orientation, paper size
433 * @param string the database name
434 * @param string the table name
435 * @param string the end of line sequence
436 * @param string the url to go back in case of error
437 * @param string SQL query for obtaining data
439 * @return bool Whether it suceeded
441 * @access public
443 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
445 global $what;
446 global $pdf_report_title;
448 $pdf = new PMA_PDF('L', 'pt', 'A3');
450 $pdf->AddFont('DejaVuSans', '', 'dejavusans.php');
451 $pdf->AddFont('DejaVuSans', 'B', 'dejavusans-bold.php');
452 $pdf->AddFont('DejaVuSerif', '', 'dejavuserif.php');
453 $pdf->AddFont('DejaVuSerif', 'B', 'dejavuserif-bold.php');
454 $pdf->SetFont(PMA_PDF_FONT, '', 11.5);
455 $pdf->AliasNbPages();
456 $attr=array('titleFontSize' => 18, 'titleText' => $pdf_report_title);
457 $pdf->mysql_report($sql_query, $attr);
459 // instead of $pdf->Output():
460 if (!PMA_exportOutputHandler($pdf->getPDFData())) {
461 return FALSE;
464 return TRUE;
465 } // end of the 'PMA_exportData()' function