2.11.1.2 release
[phpmyadmin/arisferyanto.git] / libraries / export / pdf.php
blob12bfd79873beb5369bbf8cb2d70ebcb1f062d584
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Produce a PDF report (export) from a query
6 * @version $Id$
7 */
9 /**
12 if (isset($plugin_list)) {
13 $plugin_list['pdf'] = array(
14 'text' => 'strPDF',
15 'extension' => 'pdf',
16 'mime_type' => 'application/pdf',
17 'force_file' => true,
18 'options' => array(
19 array('type' => 'message_only', 'name' => 'explanation', 'text' => 'strPDFReportExplanation'),
20 array('type' => 'text', 'name' => 'report_title', 'text' => 'strPDFReportTitle'),
21 array('type' => 'hidden', 'name' => 'data'),
23 'options_text' => 'strOptions',
25 } else {
27 /**
28 * Font used in PDF.
30 * @todo Make this configuratble (at least Sans/Serif).
32 define('PMA_PDF_FONT', 'DejaVuSans');
33 require_once './libraries/tcpdf/tcpdf.php';
35 // Adapted from a LGPL script by Philip Clarke
37 class PMA_PDF extends TCPDF
39 var $tablewidths;
40 var $headerset;
41 var $footerset;
43 // overloading of a tcpdf function:
44 function _beginpage($orientation)
46 $this->page++;
47 // solved the problem of overwriting a page, if it already exists
48 if (!isset($this->pages[$this->page])) {
49 $this->pages[$this->page] = '';
51 $this->state = 2;
52 $this->x = $this->lMargin;
53 $this->y = $this->tMargin;
54 $this->lasth = 0;
55 $this->FontFamily = '';
57 //Page orientation
58 if (!$orientation) {
59 $orientation = $this->DefOrientation;
60 } else {
61 $orientation = strtoupper($orientation{0});
62 if ($orientation != $this->DefOrientation) {
63 $this->OrientationChanges[$this->page] = true;
66 if ($orientation != $this->CurOrientation) {
67 //Change orientation
68 if ($orientation == 'P') {
69 $this->wPt = $this->fwPt;
70 $this->hPt = $this->fhPt;
71 $this->w = $this->fw;
72 $this->h = $this->fh;
73 } else {
74 $this->wPt = $this->fhPt;
75 $this->hPt = $this->fwPt;
76 $this->w = $this->fh;
77 $this->h = $this->fw;
79 $this->PageBreakTrigger = $this->h - $this->bMargin;
80 $this->CurOrientation = $orientation;
84 function Header()
86 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)*2);
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 $l = ($this->lMargin);
99 $this->SetFont(PMA_PDF_FONT, '', $this->cellFontSize);
100 foreach ($this->colTitles as $col => $txt) {
101 $this->SetXY($l, ($this->tMargin));
102 $this->MultiCell($this->tablewidths[$col], $this->FontSizePt, $txt);
103 $l += $this->tablewidths[$col] ;
104 $maxY = ($maxY < $this->getY()) ? $this->getY() : $maxY ;
106 $this->SetXY($this->lMargin, $this->tMargin);
107 $this->setFillColor(200, 200, 200);
108 $l = ($this->lMargin);
109 foreach ($this->colTitles as $col => $txt) {
110 $this->SetXY($l, $this->tMargin);
111 $this->cell($this->tablewidths[$col], $maxY-($this->tMargin), '', 1, 0, 'L', 1);
112 $this->SetXY($l, $this->tMargin);
113 $this->MultiCell($this->tablewidths[$col], $this->FontSizePt, $txt, 0, 'C');
114 $l += $this->tablewidths[$col];
116 $this->setFillColor(255, 255, 255);
117 // set headerset
118 $this->headerset[$this->page] = 1;
121 $this->SetY($maxY);
124 function Footer()
126 // Check if footer for this page already exists
127 if (!isset($this->footerset[$this->page])) {
128 $this->SetY(-15);
129 //Page number
130 $this->Cell(0, 10, $GLOBALS['strPageNumber'] .' '.$this->PageNo() .'/{nb}', 'T', 0, 'C');
132 // set footerset
133 $this->footerset[$this->page] = 1;
137 function morepagestable($lineheight=8)
139 // some things to set and 'remember'
140 $l = $this->lMargin;
141 $startheight = $h = $this->GetY();
142 $startpage = $currpage = $this->page;
144 // calculate the whole width
145 $fullwidth = 0;
146 foreach ($this->tablewidths as $width) {
147 $fullwidth += $width;
150 // Now let's start to write the table
151 $row = 0;
152 $tmpheight = array();
153 $maxpage = 0;
155 while ($data = PMA_DBI_fetch_row($this->results)) {
156 $this->page = $currpage;
157 // write the horizontal borders
158 $this->Line($l, $h, $fullwidth+$l, $h);
159 // write the content and remember the height of the highest col
160 foreach ($data as $col => $txt) {
161 $this->page = $currpage;
162 $this->SetXY($l, $h);
163 if ($this->tablewidths[$col] > 0) {
164 $this->MultiCell($this->tablewidths[$col], $lineheight, $txt, 0, $this->colAlign[$col]);
165 $l += $this->tablewidths[$col];
168 if (!isset($tmpheight[$row.'-'.$this->page])) {
169 $tmpheight[$row.'-'.$this->page] = 0;
171 if ($tmpheight[$row.'-'.$this->page] < $this->GetY()) {
172 $tmpheight[$row.'-'.$this->page] = $this->GetY();
174 if ($this->page > $maxpage) {
175 $maxpage = $this->page;
177 unset($data[$col]);
180 // get the height we were in the last used page
181 $h = $tmpheight[$row.'-'.$maxpage];
182 // set the "pointer" to the left margin
183 $l = $this->lMargin;
184 // set the $currpage to the last page
185 $currpage = $maxpage;
186 unset($data[$row]);
187 $row++;
189 // draw the borders
190 // we start adding a horizontal line on the last page
191 $this->page = $maxpage;
192 $this->Line($l, $h, $fullwidth+$l, $h);
193 // now we start at the top of the document and walk down
194 for ($i = $startpage; $i <= $maxpage; $i++) {
195 $this->page = $i;
196 $l = $this->lMargin;
197 $t = ($i == $startpage) ? $startheight : $this->tMargin;
198 $lh = ($i == $maxpage) ? $h : $this->h-$this->bMargin;
199 $this->Line($l, $t, $l, $lh);
200 foreach ($this->tablewidths as $width) {
201 $l += $width;
202 $this->Line($l, $t, $l, $lh);
205 // set it to the last page, if not it'll cause some problems
206 $this->page = $maxpage;
210 function mysql_report($query, $attr = array())
212 foreach ($attr as $key => $val){
213 $this->$key = $val ;
217 * Pass 1 for column widths
219 $this->results = PMA_DBI_query($query, null, PMA_DBI_QUERY_UNBUFFERED);
220 $this->numFields = PMA_DBI_num_fields($this->results);
221 $this->fields = PMA_DBI_get_fields_meta($this->results);
223 // if column widths not set
224 if (!isset($this->tablewidths)){
226 // sColWidth = starting col width (an average size width)
227 $availableWidth = $this->w - $this->lMargin - $this->rMargin;
228 $this->sColWidth = $availableWidth / $this->numFields;
229 $totalTitleWidth = 0;
231 // loop through results header and set initial col widths/ titles/ alignment
232 // if a col title is less than the starting col width, reduce that column size
233 for ($i = 0; $i < $this->numFields; $i++){
234 $stringWidth = $this->getstringwidth($this->fields[$i]->name) + 6 ;
235 // save the real title's width
236 $titleWidth[$i] = $stringWidth;
237 $totalTitleWidth += $stringWidth;
239 // set any column titles less than the start width to the column title width
240 if ($stringWidth < $this->sColWidth){
241 $colFits[$i] = $stringWidth ;
243 $this->colTitles[$i] = $this->fields[$i]->name;
244 $this->display_column[$i] = true;
246 switch ($this->fields[$i]->type){
247 case 'int':
248 $this->colAlign[$i] = 'R';
249 break;
250 case 'blob':
251 case 'tinyblob':
252 case 'mediumblob':
253 case 'longblob':
255 * @todo do not deactivate completely the display
256 * but show the field's name and [BLOB]
258 if (stristr($this->fields[$i]->flags, 'BINARY')) {
259 $this->display_column[$i] = false;
260 unset($this->colTitles[$i]);
262 $this->colAlign[$i] = 'L';
263 break;
264 default:
265 $this->colAlign[$i] = 'L';
269 // title width verification
270 if ($totalTitleWidth > $availableWidth) {
271 $adjustingMode = true;
272 } else {
273 $adjustingMode = false;
274 // we have enough space for all the titles at their
275 // original width so use the true title's width
276 foreach ($titleWidth as $key => $val) {
277 $colFits[$key] = $val;
281 // loop through the data, any column whose contents is bigger
282 // than the col size is resized
284 * @todo force here a LIMIT to avoid reading all rows
286 while ($row = PMA_DBI_fetch_row($this->results)) {
287 foreach ($colFits as $key => $val) {
288 $stringWidth = $this->getstringwidth($row[$key]) + 6 ;
289 if ($adjustingMode && ($stringWidth > $this->sColWidth)) {
290 // any column whose data's width is bigger than the start width is now discarded
291 unset($colFits[$key]);
292 } else {
293 // if data's width is bigger than the current column width,
294 // enlarge the column (but avoid enlarging it if the
295 // data's width is very big)
296 if ($stringWidth > $val && $stringWidth < ($this->sColWidth * 3)) {
297 $colFits[$key] = $stringWidth ;
303 $totAlreadyFitted = 0;
304 foreach ($colFits as $key => $val){
305 // set fitted columns to smallest size
306 $this->tablewidths[$key] = $val;
307 // to work out how much (if any) space has been freed up
308 $totAlreadyFitted += $val;
311 if ($adjustingMode) {
312 $surplus = (sizeof($colFits) * $this->sColWidth) - $totAlreadyFitted;
313 $surplusToAdd = $surplus / ($this->numFields - sizeof($colFits));
314 } else {
315 $surplusToAdd = 0;
318 for ($i=0; $i < $this->numFields; $i++) {
319 if (!in_array($i, array_keys($colFits))) {
320 $this->tablewidths[$i] = $this->sColWidth + $surplusToAdd;
322 if ($this->display_column[$i] == false) {
323 $this->tablewidths[$i] = 0;
327 ksort($this->tablewidths);
329 PMA_DBI_free_result($this->results);
331 // Pass 2
333 $this->results = PMA_DBI_query($query, null, PMA_DBI_QUERY_UNBUFFERED);
334 $this->Open();
335 $this->setY($this->tMargin);
336 $this->AddPage();
337 $this->morepagestable($this->FontSizePt);
338 PMA_DBI_free_result($this->results);
340 } // end of mysql_report function
342 } // end of PMA_PDF class
345 * Outputs comment
347 * @param string Text of comment
349 * @return bool Whether it suceeded
351 function PMA_exportComment($text)
353 return TRUE;
357 * Outputs export footer
359 * @return bool Whether it suceeded
361 * @access public
363 function PMA_exportFooter()
365 return TRUE;
369 * Outputs export header
371 * @return bool Whether it suceeded
373 * @access public
375 function PMA_exportHeader()
377 return TRUE;
381 * Outputs database header
383 * @param string Database name
385 * @return bool Whether it suceeded
387 * @access public
389 function PMA_exportDBHeader($db)
391 return TRUE;
395 * Outputs database footer
397 * @param string Database name
399 * @return bool Whether it suceeded
401 * @access public
403 function PMA_exportDBFooter($db)
405 return TRUE;
409 * Outputs create database database
411 * @param string Database name
413 * @return bool Whether it suceeded
415 * @access public
417 function PMA_exportDBCreate($db)
419 return TRUE;
423 * Outputs the content of a table in PDF format
425 * @todo user-defined page orientation, paper size
426 * @param string the database name
427 * @param string the table name
428 * @param string the end of line sequence
429 * @param string the url to go back in case of error
430 * @param string SQL query for obtaining data
432 * @return bool Whether it suceeded
434 * @access public
436 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
438 global $what;
439 global $pdf_report_title;
441 $pdf = new PMA_PDF('L', 'pt', 'A3');
443 $pdf->AddFont('DejaVuSans', '', 'dejavusans.php');
444 $pdf->AddFont('DejaVuSans', 'B', 'dejavusans-bold.php');
445 $pdf->AddFont('DejaVuSerif', '', 'dejavuserif.php');
446 $pdf->AddFont('DejaVuSerif', 'B', 'dejavuserif-bold.php');
447 $pdf->SetFont(PMA_PDF_FONT, '', 11.5);
448 $pdf->AliasNbPages();
449 $attr=array('titleFontSize' => 18, 'titleText' => $pdf_report_title);
450 $pdf->mysql_report($sql_query, $attr);
452 // instead of $pdf->Output():
453 if ($pdf->state < 3) {
454 $pdf->Close();
456 if (!PMA_exportOutputHandler($pdf->buffer)) {
457 return FALSE;
460 return TRUE;
461 } // end of the 'PMA_exportData()' function