Check before commit!
[phpmyadmin/crack.git] / libraries / export / pdf.php
blob7327f5436364ac879ae17fb585fc2f1ec64bc94e
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
5 /**
6 * Produce a PDF report (export) from a query
7 */
9 if (isset($plugin_list)) {
10 $plugin_list['pdf'] = array(
11 'text' => 'strPDF',
12 'extension' => 'pdf',
13 'mime_type' => 'application/pdf',
14 'force_file' => true,
15 'options' => array(
16 array('type' => 'message_only', 'name' => 'explanation', 'text' => 'strPDFReportExplanation'),
17 array('type' => 'text', 'name' => 'report_title', 'text' => 'strPDFReportTitle'),
18 array('type' => 'hidden', 'name' => 'data'),
20 'options_text' => 'strPDFOptions',
22 } else {
24 /**
25 * Font used in PDF.
27 * @todo Make this configuratble (at least Sans/Serif).
29 define('PMA_PDF_FONT', 'DejaVuSans');
30 require_once('./libraries/tcpdf/tcpdf.php');
32 // Adapted from a LGPL script by Philip Clarke
34 class PMA_PDF extends TCPDF
36 var $tablewidths;
37 var $headerset;
38 var $footerset;
40 // overloading of a tcpdf function:
41 function _beginpage($orientation)
43 $this->page++;
44 // solved the problem of overwriting a page, if it already exists
45 if (!isset($this->pages[$this->page])) {
46 $this->pages[$this->page] = '';
48 $this->state = 2;
49 $this->x = $this->lMargin;
50 $this->y = $this->tMargin;
51 $this->lasth = 0;
52 $this->FontFamily = '';
54 //Page orientation
55 if (!$orientation) {
56 $orientation = $this->DefOrientation;
57 } else {
58 $orientation = strtoupper($orientation{0});
59 if ($orientation != $this->DefOrientation) {
60 $this->OrientationChanges[$this->page] = true;
63 if ($orientation != $this->CurOrientation) {
64 //Change orientation
65 if ($orientation == 'P') {
66 $this->wPt = $this->fwPt;
67 $this->hPt = $this->fhPt;
68 $this->w = $this->fw;
69 $this->h = $this->fh;
70 } else {
71 $this->wPt = $this->fhPt;
72 $this->hPt = $this->fwPt;
73 $this->w = $this->fh;
74 $this->h = $this->fw;
76 $this->PageBreakTrigger = $this->h - $this->bMargin;
77 $this->CurOrientation = $orientation;
81 function Header()
83 global $maxY;
85 // Check if header for this page already exists
86 if (!isset($this->headerset[$this->page])) {
87 $fullwidth = 0;
88 foreach ($this->tablewidths as $width) {
89 $fullwidth += $width;
91 $this->SetY(($this->tMargin) - ($this->FontSizePt/$this->k)*2);
92 $this->cellFontSize = $this->FontSizePt ;
93 $this->SetFont(PMA_PDF_FONT, '', ($this->titleFontSize ? $this->titleFontSize : $this->FontSizePt));
94 $this->Cell(0, $this->FontSizePt, $this->titleText, 0, 1, 'C');
95 $l = ($this->lMargin);
96 $this->SetFont(PMA_PDF_FONT, '', $this->cellFontSize);
97 foreach ($this->colTitles as $col => $txt) {
98 $this->SetXY($l, ($this->tMargin));
99 $this->MultiCell($this->tablewidths[$col], $this->FontSizePt, $txt);
100 $l += $this->tablewidths[$col] ;
101 $maxY = ($maxY < $this->getY()) ? $this->getY() : $maxY ;
103 $this->SetXY($this->lMargin, $this->tMargin);
104 $this->setFillColor(200, 200, 200);
105 $l = ($this->lMargin);
106 foreach ($this->colTitles as $col => $txt) {
107 $this->SetXY($l, $this->tMargin);
108 $this->cell($this->tablewidths[$col], $maxY-($this->tMargin), '', 1, 0, 'L', 1);
109 $this->SetXY($l, $this->tMargin);
110 $this->MultiCell($this->tablewidths[$col], $this->FontSizePt, $txt, 0, 'C');
111 $l += $this->tablewidths[$col];
113 $this->setFillColor(255, 255, 255);
114 // set headerset
115 $this->headerset[$this->page] = 1;
118 $this->SetY($maxY);
121 function Footer()
123 // Check if footer for this page already exists
124 if (!isset($this->footerset[$this->page])) {
125 $this->SetY(-15);
126 //Page number
127 $this->Cell(0, 10, $GLOBALS['strPageNumber'] .' '.$this->PageNo() .'/{nb}', 'T', 0, 'C');
129 // set footerset
130 $this->footerset[$this->page] = 1;
134 function morepagestable($lineheight=8)
136 // some things to set and 'remember'
137 $l = $this->lMargin;
138 $startheight = $h = $this->GetY();
139 $startpage = $currpage = $this->page;
141 // calculate the whole width
142 $fullwidth = 0;
143 foreach ($this->tablewidths as $width) {
144 $fullwidth += $width;
147 // Now let's start to write the table
148 $row = 0;
149 $tmpheight = array();
150 $maxpage = 0;
152 while ($data = PMA_DBI_fetch_row($this->results)) {
153 $this->page = $currpage;
154 // write the horizontal borders
155 $this->Line($l, $h, $fullwidth+$l, $h);
156 // write the content and remember the height of the highest col
157 foreach ($data as $col => $txt) {
158 $this->page = $currpage;
159 $this->SetXY($l, $h);
160 if ($this->tablewidths[$col] > 0) {
161 $this->MultiCell($this->tablewidths[$col], $lineheight, $txt, 0, $this->colAlign[$col]);
162 $l += $this->tablewidths[$col];
165 if (!isset($tmpheight[$row.'-'.$this->page])) {
166 $tmpheight[$row.'-'.$this->page] = 0;
168 if ($tmpheight[$row.'-'.$this->page] < $this->GetY()) {
169 $tmpheight[$row.'-'.$this->page] = $this->GetY();
171 if ($this->page > $maxpage) {
172 $maxpage = $this->page;
174 unset($data[$col]);
177 // get the height we were in the last used page
178 $h = $tmpheight[$row.'-'.$maxpage];
179 // set the "pointer" to the left margin
180 $l = $this->lMargin;
181 // set the $currpage to the last page
182 $currpage = $maxpage;
183 unset($data[$row]);
184 $row++;
186 // draw the borders
187 // we start adding a horizontal line on the last page
188 $this->page = $maxpage;
189 $this->Line($l, $h, $fullwidth+$l, $h);
190 // now we start at the top of the document and walk down
191 for ($i = $startpage; $i <= $maxpage; $i++) {
192 $this->page = $i;
193 $l = $this->lMargin;
194 $t = ($i == $startpage) ? $startheight : $this->tMargin;
195 $lh = ($i == $maxpage) ? $h : $this->h-$this->bMargin;
196 $this->Line($l, $t, $l, $lh);
197 foreach ($this->tablewidths as $width) {
198 $l += $width;
199 $this->Line($l, $t, $l, $lh);
202 // set it to the last page, if not it'll cause some problems
203 $this->page = $maxpage;
207 function mysql_report($query, $attr = array())
209 foreach ($attr as $key => $val){
210 $this->$key = $val ;
214 * Pass 1 for column widths
215 * @todo force here a LIMIT to speed up pass 1 ?
217 $this->results = PMA_DBI_query($query, null, PMA_DBI_QUERY_UNBUFFERED);
218 $this->numFields = PMA_DBI_num_fields($this->results);
219 $this->fields = PMA_DBI_get_fields_meta($this->results);
221 // if column widths not set
222 if (!isset($this->tablewidths)){
224 // starting col width
225 $this->sColWidth = ($this->w - $this->lMargin - $this->rMargin) / $this->numFields;
227 // loop through results header and set initial col widths/ titles/ alignment
228 // if a col title is less than the starting col width / reduce that column size
229 for ($i=0; $i < $this->numFields; $i++){
230 $stringWidth = $this->getstringwidth($this->fields[$i]->name) + 6 ;
231 // set any column titles less than the start width to the column title width
232 if (($stringWidth) < $this->sColWidth){
233 $colFits[$i] = $stringWidth ;
235 $this->colTitles[$i] = $this->fields[$i]->name;
236 $this->display_column[$i] = true;
238 switch ($this->fields[$i]->type){
239 case 'int':
240 $this->colAlign[$i] = 'R';
241 break;
242 case 'blob':
243 case 'tinyblob':
244 case 'mediumblob':
245 case 'longblob':
247 * @todo do not deactivate completely the display
248 * but show the field's name and [BLOB]
250 if (stristr($this->fields[$i]->flags, 'BINARY')) {
251 $this->display_column[$i] = false;
252 unset($this->colTitles[$i]);
254 $this->colAlign[$i] = 'L';
255 break;
256 default:
257 $this->colAlign[$i] = 'L';
261 // loop through the data, any column whose contents is bigger
262 // than the col size is resized
263 // TODO: force here a LIMIT to avoid reading all rows
264 while ($row = PMA_DBI_fetch_row($this->results)) {
265 foreach ($colFits as $key => $val) {
266 $stringWidth = $this->getstringwidth($row[$key]) + 6 ;
267 if ($stringWidth > $this->sColWidth) {
268 // any col where row is bigger than the start width is now discarded
269 unset($colFits[$key]);
270 } else {
271 // if text is not bigger than the current column width setting enlarge the column
272 if ($stringWidth > $val) {
273 $colFits[$key] = $stringWidth ;
279 $totAlreadyFitted = 0;
280 foreach ($colFits as $key => $val){
281 // set fitted columns to smallest size
282 $this->tablewidths[$key] = $val;
283 // to work out how much (if any) space has been freed up
284 $totAlreadyFitted += $val;
287 $surplus = (sizeof($colFits) * $this->sColWidth) - $totAlreadyFitted;
288 for ($i=0; $i < $this->numFields; $i++) {
289 if (!in_array($i, array_keys($colFits))) {
290 $this->tablewidths[$i] = $this->sColWidth + ($surplus / ($this->numFields - sizeof($colFits)));
292 if ($this->display_column[$i] == false) {
293 $this->tablewidths[$i] = 0;
297 ksort($this->tablewidths);
300 PMA_DBI_free_result($this->results);
302 // Pass 2
304 $this->results = PMA_DBI_query($query, null, PMA_DBI_QUERY_UNBUFFERED);
305 $this->Open();
306 $this->setY($this->tMargin);
307 $this->AddPage();
308 $this->morepagestable($this->FontSizePt);
309 PMA_DBI_free_result($this->results);
311 } // end of mysql_report function
313 } // end of PMA_PDF class
316 * Outputs comment
318 * @param string Text of comment
320 * @return bool Whether it suceeded
322 function PMA_exportComment($text)
324 return TRUE;
328 * Outputs export footer
330 * @return bool Whether it suceeded
332 * @access public
334 function PMA_exportFooter()
336 return TRUE;
340 * Outputs export header
342 * @return bool Whether it suceeded
344 * @access public
346 function PMA_exportHeader()
348 return TRUE;
352 * Outputs database header
354 * @param string Database name
356 * @return bool Whether it suceeded
358 * @access public
360 function PMA_exportDBHeader($db)
362 return TRUE;
366 * Outputs database footer
368 * @param string Database name
370 * @return bool Whether it suceeded
372 * @access public
374 function PMA_exportDBFooter($db)
376 return TRUE;
380 * Outputs create database database
382 * @param string Database name
384 * @return bool Whether it suceeded
386 * @access public
388 function PMA_exportDBCreate($db)
390 return TRUE;
394 * Outputs the content of a table in PDF format
396 * @todo user-defined page orientation, paper size
397 * @param string the database name
398 * @param string the table name
399 * @param string the end of line sequence
400 * @param string the url to go back in case of error
401 * @param string SQL query for obtaining data
403 * @return bool Whether it suceeded
405 * @access public
407 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
409 global $what;
410 global $pdf_report_title;
412 $pdf = new PMA_PDF('L', 'pt', 'A3');
414 $pdf->AddFont('DejaVuSans', '', 'dejavusans.php');
415 $pdf->AddFont('DejaVuSans', 'B', 'dejavusans-bold.php');
416 $pdf->AddFont('DejaVuSerif', '', 'dejavuserif.php');
417 $pdf->AddFont('DejaVuSerif', 'B', 'dejavuserif-bold.php');
418 $pdf->SetFont(PMA_PDF_FONT, '', 11.5);
419 $pdf->AliasNbPages();
420 $attr=array('titleFontSize' => 18, 'titleText' => $pdf_report_title);
421 $pdf->mysql_report($sql_query, $attr);
423 // instead of $pdf->Output():
424 if ($pdf->state < 3) {
425 $pdf->Close();
427 if (!PMA_exportOutputHandler($pdf->buffer)) {
428 return FALSE;
431 return TRUE;
432 } // end of the 'PMA_exportData()' function