2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Produce a PDF report (export) from a query
6 * @package phpMyAdmin-Export-PDF
8 if (! defined('PHPMYADMIN')) {
15 if (isset($plugin_list)) {
16 $plugin_list['pdf'] = array(
19 'mime_type' => 'application/pdf',
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'),
35 * @todo Make this configuratble (at least Sans/Serif).
37 define('PMA_PDF_FONT', 'DejaVuSans');
38 require_once './libraries/tcpdf/tcpdf.php';
41 * Adapted from a LGPL script by Philip Clarke
42 * @package phpMyAdmin-Export-PDF
44 class PMA_PDF
extends TCPDF
50 // overloading of a tcpdf function:
51 function _beginpage($orientation)
54 // solved the problem of overwriting a page, if it already exists
55 if (!isset($this->pages
[$this->page
])) {
56 $this->pages
[$this->page
] = '';
59 $this->x
= $this->lMargin
;
60 $this->y
= $this->tMargin
;
62 $this->FontFamily
= '';
66 $orientation = $this->DefOrientation
;
68 $orientation = strtoupper($orientation{0});
69 if ($orientation != $this->DefOrientation
) {
70 $this->OrientationChanges
[$this->page
] = true;
73 if ($orientation != $this->CurOrientation
) {
75 if ($orientation == 'P') {
76 $this->wPt
= $this->fwPt
;
77 $this->hPt
= $this->fhPt
;
81 $this->wPt
= $this->fhPt
;
82 $this->hPt
= $this->fwPt
;
86 $this->PageBreakTrigger
= $this->h
- $this->bMargin
;
87 $this->CurOrientation
= $orientation;
95 // Check if header for this page already exists
96 if (!isset($this->headerset
[$this->page
])) {
98 foreach ($this->tablewidths
as $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);
125 $this->headerset
[$this->page
] = 1;
133 // Check if footer for this page already exists
134 if (!isset($this->footerset
[$this->page
])) {
137 $this->Cell(0, 10, __('Page number:') .' '.$this->PageNo() .'/{nb}', 'T', 0, 'C');
140 $this->footerset
[$this->page
] = 1;
144 function morepagestable($lineheight=8)
146 // some things to set and 'remember'
148 $startheight = $h = $this->GetY();
149 $startpage = $currpage = $this->page
;
151 // calculate the whole width
153 foreach ($this->tablewidths
as $width) {
154 $fullwidth +
= $width;
157 // Now let's start to write the table
159 $tmpheight = array();
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
;
187 // get the height we were in the last used page
188 $h = $tmpheight[$row.'-'.$maxpage];
189 // set the "pointer" to the left margin
191 // set the $currpage to the last page
192 $currpage = $maxpage;
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++
) {
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) {
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){
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
){
255 $this->colAlign
[$i] = 'R';
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';
272 $this->colAlign
[$i] = 'L';
276 // title width verification
277 if ($totalTitleWidth > $availableWidth) {
278 $adjustingMode = true;
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]);
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));
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
);
340 $this->results
= PMA_DBI_query($query, null, PMA_DBI_QUERY_UNBUFFERED
);
342 $this->setY($this->tMargin
);
344 $this->morepagestable($this->FontSizePt
);
345 PMA_DBI_free_result($this->results
);
347 } // end of mysql_report function
349 } // end of PMA_PDF class
354 * @param string Text of comment
356 * @return bool Whether it suceeded
358 function PMA_exportComment($text)
364 * Outputs export footer
366 * @return bool Whether it suceeded
370 function PMA_exportFooter()
376 * Outputs export header
378 * @return bool Whether it suceeded
382 function PMA_exportHeader()
388 * Outputs database header
390 * @param string Database name
392 * @return bool Whether it suceeded
396 function PMA_exportDBHeader($db)
402 * Outputs database footer
404 * @param string Database name
406 * @return bool Whether it suceeded
410 function PMA_exportDBFooter($db)
416 * Outputs create database database
418 * @param string Database name
420 * @return bool Whether it suceeded
424 function PMA_exportDBCreate($db)
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
443 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
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())) {
465 } // end of the 'PMA_exportData()' function