2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Produce a PDF report (export) from a query
6 * @package phpMyAdmin-Export-PDF
9 if (! defined('PHPMYADMIN')) {
16 if (isset($plugin_list)) {
17 $plugin_list['pdf'] = array(
20 'mime_type' => 'application/pdf',
23 array('type' => 'message_only', 'name' => 'explanation', 'text' => 'strPDFReportExplanation'),
24 array('type' => 'text', 'name' => 'report_title', 'text' => 'strPDFReportTitle'),
25 array('type' => 'hidden', 'name' => 'data'),
27 'options_text' => 'strOptions',
34 * @todo Make this configuratble (at least Sans/Serif).
36 define('PMA_PDF_FONT', 'DejaVuSans');
37 require_once './libraries/tcpdf/tcpdf.php';
40 * Adapted from a LGPL script by Philip Clarke
41 * @package phpMyAdmin-Export-PDF
43 class PMA_PDF
extends TCPDF
49 // added because tcpdf for PHP 5 has a protected $buffer
50 public function getBuffer()
55 public function getState()
60 // overloading of a tcpdf function:
61 function _beginpage($orientation)
64 // solved the problem of overwriting a page, if it already exists
65 if (!isset($this->pages
[$this->page
])) {
66 $this->pages
[$this->page
] = '';
69 $this->x
= $this->lMargin
;
70 $this->y
= $this->tMargin
;
72 $this->FontFamily
= '';
76 $orientation = $this->DefOrientation
;
78 $orientation = strtoupper($orientation{0});
79 if ($orientation != $this->DefOrientation
) {
80 $this->OrientationChanges
[$this->page
] = true;
83 if ($orientation != $this->CurOrientation
) {
85 if ($orientation == 'P') {
86 $this->wPt
= $this->fwPt
;
87 $this->hPt
= $this->fhPt
;
91 $this->wPt
= $this->fhPt
;
92 $this->hPt
= $this->fwPt
;
96 $this->PageBreakTrigger
= $this->h
- $this->bMargin
;
97 $this->CurOrientation
= $orientation;
105 // Check if header for this page already exists
106 if (!isset($this->headerset
[$this->page
])) {
108 foreach ($this->tablewidths
as $width) {
109 $fullwidth +
= $width;
111 $this->SetY(($this->tMargin
) - ($this->FontSizePt
/$this->k
)*2);
112 $this->cellFontSize
= $this->FontSizePt
;
113 $this->SetFont(PMA_PDF_FONT
, '', ($this->titleFontSize ?
$this->titleFontSize
: $this->FontSizePt
));
114 $this->Cell(0, $this->FontSizePt
, $this->titleText
, 0, 1, 'C');
115 $l = ($this->lMargin
);
116 $this->SetFont(PMA_PDF_FONT
, '', $this->cellFontSize
);
117 foreach ($this->colTitles
as $col => $txt) {
118 $this->SetXY($l, ($this->tMargin
));
119 $this->MultiCell($this->tablewidths
[$col], $this->FontSizePt
, $txt);
120 $l +
= $this->tablewidths
[$col] ;
121 $maxY = ($maxY < $this->getY()) ?
$this->getY() : $maxY ;
123 $this->SetXY($this->lMargin
, $this->tMargin
);
124 $this->setFillColor(200, 200, 200);
125 $l = ($this->lMargin
);
126 foreach ($this->colTitles
as $col => $txt) {
127 $this->SetXY($l, $this->tMargin
);
128 $this->cell($this->tablewidths
[$col], $maxY-($this->tMargin
), '', 1, 0, 'L', 1);
129 $this->SetXY($l, $this->tMargin
);
130 $this->MultiCell($this->tablewidths
[$col], $this->FontSizePt
, $txt, 0, 'C');
131 $l +
= $this->tablewidths
[$col];
133 $this->setFillColor(255, 255, 255);
135 $this->headerset
[$this->page
] = 1;
143 // Check if footer for this page already exists
144 if (!isset($this->footerset
[$this->page
])) {
147 $this->Cell(0, 10, $GLOBALS['strPageNumber'] .' '.$this->PageNo() .'/{nb}', 'T', 0, 'C');
150 $this->footerset
[$this->page
] = 1;
154 function morepagestable($lineheight=8)
156 // some things to set and 'remember'
158 $startheight = $h = $this->GetY();
159 $startpage = $currpage = $this->page
;
161 // calculate the whole width
163 foreach ($this->tablewidths
as $width) {
164 $fullwidth +
= $width;
167 // Now let's start to write the table
169 $tmpheight = array();
172 while ($data = PMA_DBI_fetch_row($this->results
)) {
173 $this->page
= $currpage;
174 // write the horizontal borders
175 $this->Line($l, $h, $fullwidth+
$l, $h);
176 // write the content and remember the height of the highest col
177 foreach ($data as $col => $txt) {
178 $this->page
= $currpage;
179 $this->SetXY($l, $h);
180 if ($this->tablewidths
[$col] > 0) {
181 $this->MultiCell($this->tablewidths
[$col], $lineheight, $txt, 0, $this->colAlign
[$col]);
182 $l +
= $this->tablewidths
[$col];
185 if (!isset($tmpheight[$row.'-'.$this->page
])) {
186 $tmpheight[$row.'-'.$this->page
] = 0;
188 if ($tmpheight[$row.'-'.$this->page
] < $this->GetY()) {
189 $tmpheight[$row.'-'.$this->page
] = $this->GetY();
191 if ($this->page
> $maxpage) {
192 $maxpage = $this->page
;
197 // get the height we were in the last used page
198 $h = $tmpheight[$row.'-'.$maxpage];
199 // set the "pointer" to the left margin
201 // set the $currpage to the last page
202 $currpage = $maxpage;
207 // we start adding a horizontal line on the last page
208 $this->page
= $maxpage;
209 $this->Line($l, $h, $fullwidth+
$l, $h);
210 // now we start at the top of the document and walk down
211 for ($i = $startpage; $i <= $maxpage; $i++
) {
214 $t = ($i == $startpage) ?
$startheight : $this->tMargin
;
215 $lh = ($i == $maxpage) ?
$h : $this->h
-$this->bMargin
;
216 $this->Line($l, $t, $l, $lh);
217 foreach ($this->tablewidths
as $width) {
219 $this->Line($l, $t, $l, $lh);
222 // set it to the last page, if not it'll cause some problems
223 $this->page
= $maxpage;
227 function mysql_report($query, $attr = array())
229 foreach ($attr as $key => $val){
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 // if column widths not set
241 if (!isset($this->tablewidths
)){
243 // sColWidth = starting col width (an average size width)
244 $availableWidth = $this->w
- $this->lMargin
- $this->rMargin
;
245 $this->sColWidth
= $availableWidth / $this->numFields
;
246 $totalTitleWidth = 0;
248 // loop through results header and set initial col widths/ titles/ alignment
249 // if a col title is less than the starting col width, reduce that column size
250 for ($i = 0; $i < $this->numFields
; $i++
){
251 $stringWidth = $this->getstringwidth($this->fields
[$i]->name
) +
6 ;
252 // save the real title's width
253 $titleWidth[$i] = $stringWidth;
254 $totalTitleWidth +
= $stringWidth;
256 // set any column titles less than the start width to the column title width
257 if ($stringWidth < $this->sColWidth
){
258 $colFits[$i] = $stringWidth ;
260 $this->colTitles
[$i] = $this->fields
[$i]->name
;
261 $this->display_column
[$i] = true;
263 switch ($this->fields
[$i]->type
){
265 $this->colAlign
[$i] = 'R';
272 * @todo do not deactivate completely the display
273 * but show the field's name and [BLOB]
275 if (stristr($this->fields
[$i]->flags
, 'BINARY')) {
276 $this->display_column
[$i] = false;
277 unset($this->colTitles
[$i]);
279 $this->colAlign
[$i] = 'L';
282 $this->colAlign
[$i] = 'L';
286 // title width verification
287 if ($totalTitleWidth > $availableWidth) {
288 $adjustingMode = true;
290 $adjustingMode = false;
291 // we have enough space for all the titles at their
292 // original width so use the true title's width
293 foreach ($titleWidth as $key => $val) {
294 $colFits[$key] = $val;
298 // loop through the data; any column whose contents
299 // is greater than the column size is resized
301 * @todo force here a LIMIT to avoid reading all rows
303 while ($row = PMA_DBI_fetch_row($this->results
)) {
304 foreach ($colFits as $key => $val) {
305 $stringWidth = $this->getstringwidth($row[$key]) +
6 ;
306 if ($adjustingMode && ($stringWidth > $this->sColWidth
)) {
307 // any column whose data's width is bigger than the start width is now discarded
308 unset($colFits[$key]);
310 // if data's width is bigger than the current column width,
311 // enlarge the column (but avoid enlarging it if the
312 // data's width is very big)
313 if ($stringWidth > $val && $stringWidth < ($this->sColWidth
* 3)) {
314 $colFits[$key] = $stringWidth ;
320 $totAlreadyFitted = 0;
321 foreach ($colFits as $key => $val){
322 // set fitted columns to smallest size
323 $this->tablewidths
[$key] = $val;
324 // to work out how much (if any) space has been freed up
325 $totAlreadyFitted +
= $val;
328 if ($adjustingMode) {
329 $surplus = (sizeof($colFits) * $this->sColWidth
) - $totAlreadyFitted;
330 $surplusToAdd = $surplus / ($this->numFields
- sizeof($colFits));
335 for ($i=0; $i < $this->numFields
; $i++
) {
336 if (!in_array($i, array_keys($colFits))) {
337 $this->tablewidths
[$i] = $this->sColWidth +
$surplusToAdd;
339 if ($this->display_column
[$i] == false) {
340 $this->tablewidths
[$i] = 0;
344 ksort($this->tablewidths
);
346 PMA_DBI_free_result($this->results
);
350 $this->results
= PMA_DBI_query($query, null, PMA_DBI_QUERY_UNBUFFERED
);
352 $this->setY($this->tMargin
);
354 $this->morepagestable($this->FontSizePt
);
355 PMA_DBI_free_result($this->results
);
357 } // end of mysql_report function
359 } // end of PMA_PDF class
364 * @param string Text of comment
366 * @return bool Whether it suceeded
368 function PMA_exportComment($text)
374 * Outputs export footer
376 * @return bool Whether it suceeded
380 function PMA_exportFooter()
386 * Outputs export header
388 * @return bool Whether it suceeded
392 function PMA_exportHeader()
398 * Outputs database header
400 * @param string Database name
402 * @return bool Whether it suceeded
406 function PMA_exportDBHeader($db)
412 * Outputs database footer
414 * @param string Database name
416 * @return bool Whether it suceeded
420 function PMA_exportDBFooter($db)
426 * Outputs create database database
428 * @param string Database name
430 * @return bool Whether it suceeded
434 function PMA_exportDBCreate($db)
440 * Outputs the content of a table in PDF format
442 * @todo user-defined page orientation, paper size
443 * @param string the database name
444 * @param string the table name
445 * @param string the end of line sequence
446 * @param string the url to go back in case of error
447 * @param string SQL query for obtaining data
449 * @return bool Whether it suceeded
453 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
456 global $pdf_report_title;
458 $pdf = new PMA_PDF('L', 'pt', 'A3');
460 $pdf->AddFont('DejaVuSans', '', 'dejavusans.php');
461 $pdf->AddFont('DejaVuSans', 'B', 'dejavusans-bold.php');
462 $pdf->AddFont('DejaVuSerif', '', 'dejavuserif.php');
463 $pdf->AddFont('DejaVuSerif', 'B', 'dejavuserif-bold.php');
464 $pdf->SetFont(PMA_PDF_FONT
, '', 11.5);
465 $pdf->AliasNbPages();
466 $attr=array('titleFontSize' => 18, 'titleText' => $pdf_report_title);
467 $pdf->mysql_report($sql_query, $attr);
469 // instead of $pdf->Output():
470 if ($pdf->getState() < 3) {
473 if (!PMA_exportOutputHandler($pdf->getBuffer())) {
478 } // end of the 'PMA_exportData()' function