2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Produce a PDF report (export) from a query
6 * @package phpMyAdmin-Export
9 if (! defined('PHPMYADMIN')) {
16 if (isset($plugin_list)) {
17 $plugin_list['pdf'] = array(
20 'mime_type' => 'application/pdf',
23 array('type' => 'begin_group', 'name' => 'general_opts'),
24 array('type' => 'message_only', 'name' => 'explanation', 'text' => __('(Generates a report containing the data of a single table)')),
25 array('type' => 'text', 'name' => 'report_title', 'text' => __('Report title:')),
26 array('type' => 'hidden', 'name' => 'structure_or_data'),
27 array('type' => 'end_group')
29 'options_text' => __('Options'),
36 * @todo Make this configuratble (at least Sans/Serif).
38 define('PMA_PDF_FONT', 'DejaVuSans');
39 require_once './libraries/tcpdf/tcpdf.php';
42 * Adapted from a LGPL script by Philip Clarke
43 * @package phpMyAdmin-Export
46 class PMA_PDF
extends TCPDF
52 function checkPageBreak($h=0, $y='', $addpage=true) {
53 if ($this->empty_string($y)) {
56 $current_page = $this->page
;
57 if ((($y +
$h) > $this->PageBreakTrigger
) AND (!$this->InFooter
) AND ($this->AcceptPageBreak())) {
59 //Automatic page break
61 $this->AddPage($this->CurOrientation
);
62 $this->y
= $this->dataY
;
63 $oldpage = $this->page
- 1;
65 if ($this->pagedim
[$this->page
]['orm'] != $this->pagedim
[$oldpage]['orm']) {
66 $this->x
= $x - ($this->pagedim
[$this->page
]['orm'] - $this->pagedim
[$oldpage]['orm']);
71 if ($this->pagedim
[$this->page
]['olm'] != $this->pagedim
[$oldpage]['olm']) {
72 $this->x
= $x +
($this->pagedim
[$this->page
]['olm'] - $this->pagedim
[$oldpage]['olm']);
80 if ($current_page != $this->page
) {
81 // account for columns mode
90 // Check if header for this page already exists
91 if (!isset($this->headerset
[$this->page
])) {
93 foreach ($this->tablewidths
as $width) {
96 $this->SetY(($this->tMargin
) - ($this->FontSizePt
/$this->k
)*3);
97 $this->cellFontSize
= $this->FontSizePt
;
98 $this->SetFont(PMA_PDF_FONT
, '', ($this->titleFontSize ?
$this->titleFontSize
: $this->FontSizePt
));
99 $this->Cell(0, $this->FontSizePt
, $this->titleText
, 0, 1, 'C');
100 $this->SetFont(PMA_PDF_FONT
, '', $this->cellFontSize
);
101 $this->SetY(($this->tMargin
) - ($this->FontSizePt
/$this->k
)*1.5);
102 $this->Cell(0, $this->FontSizePt
, __('Database') .': ' .$this->currentDb
.', ' .__('Table') .': ' .$this->currentTable
, 0, 1, 'L');
103 $l = ($this->lMargin
);
104 foreach ($this->colTitles
as $col => $txt) {
105 $this->SetXY($l, ($this->tMargin
));
106 $this->MultiCell($this->tablewidths
[$col], $this->FontSizePt
, $txt);
107 $l +
= $this->tablewidths
[$col] ;
108 $maxY = ($maxY < $this->getY()) ?
$this->getY() : $maxY ;
110 $this->SetXY($this->lMargin
, $this->tMargin
);
111 $this->setFillColor(200, 200, 200);
112 $l = ($this->lMargin
);
113 foreach ($this->colTitles
as $col => $txt) {
114 $this->SetXY($l, $this->tMargin
);
115 $this->cell($this->tablewidths
[$col], $maxY-($this->tMargin
), '', 1, 0, 'L', 1);
116 $this->SetXY($l, $this->tMargin
);
117 $this->MultiCell($this->tablewidths
[$col], $this->FontSizePt
, $txt, 0, 'C');
118 $l +
= $this->tablewidths
[$col];
120 $this->setFillColor(255, 255, 255);
122 $this->headerset
[$this->page
] = 1;
125 $this->dataY
= $maxY;
130 // Check if footer for this page already exists
131 if (!isset($this->footerset
[$this->page
])) {
134 $this->setFooterFont(PMA_PDF_FONT
, '', 14);
135 $this->Cell(0, 6, __('Page number:') . ' ' . $this->getAliasNumPage() . '/' . $this->getAliasNbPages(), 'T', 0, 'C');
138 $this->footerset
[$this->page
] = 1;
142 function morepagestable($lineheight=8)
144 // some things to set and 'remember'
146 $startheight = $h = $this->dataY
;
147 $startpage = $currpage = $this->page
;
149 // calculate the whole width
151 foreach ($this->tablewidths
as $width) {
152 $fullwidth +
= $width;
155 // Now let's start to write the table
157 $tmpheight = array();
158 $maxpage = $this->page
;
160 while ($data = PMA_DBI_fetch_row($this->results
)) {
161 $this->page
= $currpage;
162 // write the horizontal borders
163 $this->Line($l, $h, $fullwidth+
$l, $h);
164 // write the content and remember the height of the highest col
165 foreach ($data as $col => $txt) {
166 $this->page
= $currpage;
167 $this->SetXY($l, $h);
168 if ($this->tablewidths
[$col] > 0) {
169 $this->MultiCell($this->tablewidths
[$col], $lineheight, $txt, 0, $this->colAlign
[$col]);
170 $l +
= $this->tablewidths
[$col];
173 if (!isset($tmpheight[$row.'-'.$this->page
])) {
174 $tmpheight[$row.'-'.$this->page
] = 0;
176 if ($tmpheight[$row.'-'.$this->page
] < $this->GetY()) {
177 $tmpheight[$row.'-'.$this->page
] = $this->GetY();
179 if ($this->page
> $maxpage) {
180 $maxpage = $this->page
;
185 // get the height we were in the last used page
186 $h = $tmpheight[$row.'-'.$maxpage];
187 // set the "pointer" to the left margin
189 // set the $currpage to the last page
190 $currpage = $maxpage;
195 // we start adding a horizontal line on the last page
196 $this->page
= $maxpage;
197 $this->Line($l, $h, $fullwidth+
$l, $h);
198 // now we start at the top of the document and walk down
199 for ($i = $startpage; $i <= $maxpage; $i++
) {
202 $t = ($i == $startpage) ?
$startheight : $this->tMargin
;
203 $lh = ($i == $maxpage) ?
$h : $this->h
-$this->bMargin
;
204 $this->Line($l, $t, $l, $lh);
205 foreach ($this->tablewidths
as $width) {
207 $this->Line($l, $t, $l, $lh);
210 // set it to the last page, if not it'll cause some problems
211 $this->page
= $maxpage;
214 function setAttributes($attr = array())
216 foreach ($attr as $key => $val) {
221 function setTopMargin($topMargin)
223 $this->tMargin
= $topMargin;
226 function mysql_report($query)
228 unset($this->tablewidths
);
229 unset($this->colTitles
);
230 unset($this->titleWidth
);
231 unset($this->colFits
);
232 unset($this->display_column
);
233 unset($this->colAlign
);
236 * Pass 1 for column widths
238 $this->results
= PMA_DBI_query($query, null, PMA_DBI_QUERY_UNBUFFERED
);
239 $this->numFields
= PMA_DBI_num_fields($this->results
);
240 $this->fields
= PMA_DBI_get_fields_meta($this->results
);
242 // sColWidth = starting col width (an average size width)
243 $availableWidth = $this->w
- $this->lMargin
- $this->rMargin
;
244 $this->sColWidth
= $availableWidth / $this->numFields
;
245 $totalTitleWidth = 0;
247 // loop through results header and set initial col widths/ titles/ alignment
248 // if a col title is less than the starting col width, reduce that column size
249 for ($i = 0; $i < $this->numFields
; $i++
) {
250 $stringWidth = $this->getstringwidth($this->fields
[$i]->name
) +
6 ;
251 // save the real title's width
252 $titleWidth[$i] = $stringWidth;
253 $totalTitleWidth +
= $stringWidth;
255 // set any column titles less than the start width to the column title width
256 if ($stringWidth < $this->sColWidth
) {
257 $colFits[$i] = $stringWidth ;
259 $this->colTitles
[$i] = $this->fields
[$i]->name
;
260 $this->display_column
[$i] = true;
262 switch ($this->fields
[$i]->type
) {
264 $this->colAlign
[$i] = 'R';
271 * @todo do not deactivate completely the display
272 * but show the field's name and [BLOB]
274 if (stristr($this->fields
[$i]->flags
, 'BINARY')) {
275 $this->display_column
[$i] = false;
276 unset($this->colTitles
[$i]);
278 $this->colAlign
[$i] = 'L';
281 $this->colAlign
[$i] = 'L';
285 // title width verification
286 if ($totalTitleWidth > $availableWidth) {
287 $adjustingMode = true;
289 $adjustingMode = false;
290 // we have enough space for all the titles at their
291 // original width so use the true title's width
292 foreach ($titleWidth as $key => $val) {
293 $colFits[$key] = $val;
297 // loop through the data; any column whose contents
298 // is greater than the column size is resized
300 * @todo force here a LIMIT to avoid reading all rows
302 while ($row = PMA_DBI_fetch_row($this->results
)) {
303 foreach ($colFits as $key => $val) {
304 $stringWidth = $this->getstringwidth($row[$key]) +
6 ;
305 if ($adjustingMode && ($stringWidth > $this->sColWidth
)) {
306 // any column whose data's width is bigger than the start width is now discarded
307 unset($colFits[$key]);
309 // if data's width is bigger than the current column width,
310 // enlarge the column (but avoid enlarging it if the
311 // data's width is very big)
312 if ($stringWidth > $val && $stringWidth < ($this->sColWidth
* 3)) {
313 $colFits[$key] = $stringWidth ;
319 $totAlreadyFitted = 0;
320 foreach ($colFits as $key => $val) {
321 // set fitted columns to smallest size
322 $this->tablewidths
[$key] = $val;
323 // to work out how much (if any) space has been freed up
324 $totAlreadyFitted +
= $val;
327 if ($adjustingMode) {
328 $surplus = (sizeof($colFits) * $this->sColWidth
) - $totAlreadyFitted;
329 $surplusToAdd = $surplus / ($this->numFields
- sizeof($colFits));
334 for ($i=0; $i < $this->numFields
; $i++
) {
335 if (!in_array($i, array_keys($colFits))) {
336 $this->tablewidths
[$i] = $this->sColWidth +
$surplusToAdd;
338 if ($this->display_column
[$i] == false) {
339 $this->tablewidths
[$i] = 0;
343 ksort($this->tablewidths
);
345 PMA_DBI_free_result($this->results
);
349 $this->results
= PMA_DBI_query($query, null, PMA_DBI_QUERY_UNBUFFERED
);
350 $this->setY($this->tMargin
);
352 $this->morepagestable($this->FontSizePt
);
353 PMA_DBI_free_result($this->results
);
355 } // end of mysql_report function
357 } // end of PMA_PDF class
359 $pdf = new PMA_PDF('L', 'pt', 'A3');
364 * @return bool Whether it suceeded
368 function PMA_exportFooter()
372 // instead of $pdf->Output():
373 if (!PMA_exportOutputHandler($pdf->getPDFData())) {
381 * Initialize the pdf to export data.
383 * @return bool Whether it suceeded
387 function PMA_exportHeader()
389 global $pdf_report_title;
392 $pdf->AddFont('DejaVuSans', '', 'dejavusans.php');
393 $pdf->AddFont('DejaVuSans', 'B', 'dejavusansb.php');
394 $pdf->AddFont('DejaVuSerif', '', 'dejavuserif.php');
395 $pdf->AddFont('DejaVuSerif', 'B', 'dejavuserifb.php');
396 $pdf->SetFont(PMA_PDF_FONT
, '', 11.5);
397 $pdf->setFooterFont(array(PMA_PDF_FONT
, '', 11.5));
398 $pdf->AliasNbPages();
401 $attr=array('titleFontSize' => 18, 'titleText' => $pdf_report_title);
402 $pdf->setAttributes($attr);
403 $pdf->setTopMargin(45);
409 * Outputs database header
411 * @param string $db Database name
412 * @return bool Whether it suceeded
416 function PMA_exportDBHeader($db)
422 * Outputs database footer
424 * @param string $db Database name
425 * @return bool Whether it suceeded
429 function PMA_exportDBFooter($db)
435 * Outputs CREATE DATABASE statement
437 * @param string $db Database name
438 * @return bool Whether it suceeded
442 function PMA_exportDBCreate($db)
448 * Outputs the content of a table in PDF format
450 * @param string $db database name
451 * @param string $table table name
452 * @param string $crlf the end of line sequence
453 * @param string $error_url the url to go back in case of error
454 * @param string $sql_query SQL query for obtaining data
455 * @return bool Whether it suceeded
459 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
463 $attr=array('currentDb' => $db, 'currentTable' => $table);
464 $pdf->setAttributes($attr);
465 $pdf->mysql_report($sql_query);
468 } // end of the 'PMA_exportData()' function