2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Produce a PDF report (export) from a query
8 if (! defined('PHPMYADMIN')) {
15 if (isset($plugin_list)) {
16 $plugin_list['pdf'] = array(
19 'mime_type' => 'application/pdf',
22 array('type' => 'message_only', 'name' => 'explanation', 'text' => 'strPDFReportExplanation'),
23 array('type' => 'text', 'name' => 'report_title', 'text' => 'strPDFReportTitle'),
24 array('type' => 'hidden', 'name' => 'data'),
26 'options_text' => 'strOptions',
33 * @todo Make this configuratble (at least Sans/Serif).
35 define('PMA_PDF_FONT', 'DejaVuSans');
36 require_once './libraries/tcpdf/tcpdf.php';
38 // Adapted from a LGPL script by Philip Clarke
40 class PMA_PDF
extends TCPDF
46 // overloading of a tcpdf function:
47 function _beginpage($orientation)
50 // solved the problem of overwriting a page, if it already exists
51 if (!isset($this->pages
[$this->page
])) {
52 $this->pages
[$this->page
] = '';
55 $this->x
= $this->lMargin
;
56 $this->y
= $this->tMargin
;
58 $this->FontFamily
= '';
62 $orientation = $this->DefOrientation
;
64 $orientation = strtoupper($orientation{0});
65 if ($orientation != $this->DefOrientation
) {
66 $this->OrientationChanges
[$this->page
] = true;
69 if ($orientation != $this->CurOrientation
) {
71 if ($orientation == 'P') {
72 $this->wPt
= $this->fwPt
;
73 $this->hPt
= $this->fhPt
;
77 $this->wPt
= $this->fhPt
;
78 $this->hPt
= $this->fwPt
;
82 $this->PageBreakTrigger
= $this->h
- $this->bMargin
;
83 $this->CurOrientation
= $orientation;
91 // Check if header for this page already exists
92 if (!isset($this->headerset
[$this->page
])) {
94 foreach ($this->tablewidths
as $width) {
97 $this->SetY(($this->tMargin
) - ($this->FontSizePt
/$this->k
)*2);
98 $this->cellFontSize
= $this->FontSizePt
;
99 $this->SetFont(PMA_PDF_FONT
, '', ($this->titleFontSize ?
$this->titleFontSize
: $this->FontSizePt
));
100 $this->Cell(0, $this->FontSizePt
, $this->titleText
, 0, 1, 'C');
101 $l = ($this->lMargin
);
102 $this->SetFont(PMA_PDF_FONT
, '', $this->cellFontSize
);
103 foreach ($this->colTitles
as $col => $txt) {
104 $this->SetXY($l, ($this->tMargin
));
105 $this->MultiCell($this->tablewidths
[$col], $this->FontSizePt
, $txt);
106 $l +
= $this->tablewidths
[$col] ;
107 $maxY = ($maxY < $this->getY()) ?
$this->getY() : $maxY ;
109 $this->SetXY($this->lMargin
, $this->tMargin
);
110 $this->setFillColor(200, 200, 200);
111 $l = ($this->lMargin
);
112 foreach ($this->colTitles
as $col => $txt) {
113 $this->SetXY($l, $this->tMargin
);
114 $this->cell($this->tablewidths
[$col], $maxY-($this->tMargin
), '', 1, 0, 'L', 1);
115 $this->SetXY($l, $this->tMargin
);
116 $this->MultiCell($this->tablewidths
[$col], $this->FontSizePt
, $txt, 0, 'C');
117 $l +
= $this->tablewidths
[$col];
119 $this->setFillColor(255, 255, 255);
121 $this->headerset
[$this->page
] = 1;
129 // Check if footer for this page already exists
130 if (!isset($this->footerset
[$this->page
])) {
133 $this->Cell(0, 10, $GLOBALS['strPageNumber'] .' '.$this->PageNo() .'/{nb}', 'T', 0, 'C');
136 $this->footerset
[$this->page
] = 1;
140 function morepagestable($lineheight=8)
142 // some things to set and 'remember'
144 $startheight = $h = $this->GetY();
145 $startpage = $currpage = $this->page
;
147 // calculate the whole width
149 foreach ($this->tablewidths
as $width) {
150 $fullwidth +
= $width;
153 // Now let's start to write the table
155 $tmpheight = array();
158 while ($data = PMA_DBI_fetch_row($this->results
)) {
159 $this->page
= $currpage;
160 // write the horizontal borders
161 $this->Line($l, $h, $fullwidth+
$l, $h);
162 // write the content and remember the height of the highest col
163 foreach ($data as $col => $txt) {
164 $this->page
= $currpage;
165 $this->SetXY($l, $h);
166 if ($this->tablewidths
[$col] > 0) {
167 $this->MultiCell($this->tablewidths
[$col], $lineheight, $txt, 0, $this->colAlign
[$col]);
168 $l +
= $this->tablewidths
[$col];
171 if (!isset($tmpheight[$row.'-'.$this->page
])) {
172 $tmpheight[$row.'-'.$this->page
] = 0;
174 if ($tmpheight[$row.'-'.$this->page
] < $this->GetY()) {
175 $tmpheight[$row.'-'.$this->page
] = $this->GetY();
177 if ($this->page
> $maxpage) {
178 $maxpage = $this->page
;
183 // get the height we were in the last used page
184 $h = $tmpheight[$row.'-'.$maxpage];
185 // set the "pointer" to the left margin
187 // set the $currpage to the last page
188 $currpage = $maxpage;
193 // we start adding a horizontal line on the last page
194 $this->page
= $maxpage;
195 $this->Line($l, $h, $fullwidth+
$l, $h);
196 // now we start at the top of the document and walk down
197 for ($i = $startpage; $i <= $maxpage; $i++
) {
200 $t = ($i == $startpage) ?
$startheight : $this->tMargin
;
201 $lh = ($i == $maxpage) ?
$h : $this->h
-$this->bMargin
;
202 $this->Line($l, $t, $l, $lh);
203 foreach ($this->tablewidths
as $width) {
205 $this->Line($l, $t, $l, $lh);
208 // set it to the last page, if not it'll cause some problems
209 $this->page
= $maxpage;
213 function mysql_report($query, $attr = array())
215 foreach ($attr as $key => $val){
220 * Pass 1 for column widths
222 $this->results
= PMA_DBI_query($query, null, PMA_DBI_QUERY_UNBUFFERED
);
223 $this->numFields
= PMA_DBI_num_fields($this->results
);
224 $this->fields
= PMA_DBI_get_fields_meta($this->results
);
226 // if column widths not set
227 if (!isset($this->tablewidths
)){
229 // sColWidth = starting col width (an average size width)
230 $availableWidth = $this->w
- $this->lMargin
- $this->rMargin
;
231 $this->sColWidth
= $availableWidth / $this->numFields
;
232 $totalTitleWidth = 0;
234 // loop through results header and set initial col widths/ titles/ alignment
235 // if a col title is less than the starting col width, reduce that column size
236 for ($i = 0; $i < $this->numFields
; $i++
){
237 $stringWidth = $this->getstringwidth($this->fields
[$i]->name
) +
6 ;
238 // save the real title's width
239 $titleWidth[$i] = $stringWidth;
240 $totalTitleWidth +
= $stringWidth;
242 // set any column titles less than the start width to the column title width
243 if ($stringWidth < $this->sColWidth
){
244 $colFits[$i] = $stringWidth ;
246 $this->colTitles
[$i] = $this->fields
[$i]->name
;
247 $this->display_column
[$i] = true;
249 switch ($this->fields
[$i]->type
){
251 $this->colAlign
[$i] = 'R';
258 * @todo do not deactivate completely the display
259 * but show the field's name and [BLOB]
261 if (stristr($this->fields
[$i]->flags
, 'BINARY')) {
262 $this->display_column
[$i] = false;
263 unset($this->colTitles
[$i]);
265 $this->colAlign
[$i] = 'L';
268 $this->colAlign
[$i] = 'L';
272 // title width verification
273 if ($totalTitleWidth > $availableWidth) {
274 $adjustingMode = true;
276 $adjustingMode = false;
277 // we have enough space for all the titles at their
278 // original width so use the true title's width
279 foreach ($titleWidth as $key => $val) {
280 $colFits[$key] = $val;
284 // loop through the data, any column whose contents is bigger
285 // than the col size is resized
287 * @todo force here a LIMIT to avoid reading all rows
289 while ($row = PMA_DBI_fetch_row($this->results
)) {
290 foreach ($colFits as $key => $val) {
291 $stringWidth = $this->getstringwidth($row[$key]) +
6 ;
292 if ($adjustingMode && ($stringWidth > $this->sColWidth
)) {
293 // any column whose data's width is bigger than the start width is now discarded
294 unset($colFits[$key]);
296 // if data's width is bigger than the current column width,
297 // enlarge the column (but avoid enlarging it if the
298 // data's width is very big)
299 if ($stringWidth > $val && $stringWidth < ($this->sColWidth
* 3)) {
300 $colFits[$key] = $stringWidth ;
306 $totAlreadyFitted = 0;
307 foreach ($colFits as $key => $val){
308 // set fitted columns to smallest size
309 $this->tablewidths
[$key] = $val;
310 // to work out how much (if any) space has been freed up
311 $totAlreadyFitted +
= $val;
314 if ($adjustingMode) {
315 $surplus = (sizeof($colFits) * $this->sColWidth
) - $totAlreadyFitted;
316 $surplusToAdd = $surplus / ($this->numFields
- sizeof($colFits));
321 for ($i=0; $i < $this->numFields
; $i++
) {
322 if (!in_array($i, array_keys($colFits))) {
323 $this->tablewidths
[$i] = $this->sColWidth +
$surplusToAdd;
325 if ($this->display_column
[$i] == false) {
326 $this->tablewidths
[$i] = 0;
330 ksort($this->tablewidths
);
332 PMA_DBI_free_result($this->results
);
336 $this->results
= PMA_DBI_query($query, null, PMA_DBI_QUERY_UNBUFFERED
);
338 $this->setY($this->tMargin
);
340 $this->morepagestable($this->FontSizePt
);
341 PMA_DBI_free_result($this->results
);
343 } // end of mysql_report function
345 } // end of PMA_PDF class
350 * @param string Text of comment
352 * @return bool Whether it suceeded
354 function PMA_exportComment($text)
360 * Outputs export footer
362 * @return bool Whether it suceeded
366 function PMA_exportFooter()
372 * Outputs export header
374 * @return bool Whether it suceeded
378 function PMA_exportHeader()
384 * Outputs database header
386 * @param string Database name
388 * @return bool Whether it suceeded
392 function PMA_exportDBHeader($db)
398 * Outputs database footer
400 * @param string Database name
402 * @return bool Whether it suceeded
406 function PMA_exportDBFooter($db)
412 * Outputs create database database
414 * @param string Database name
416 * @return bool Whether it suceeded
420 function PMA_exportDBCreate($db)
426 * Outputs the content of a table in PDF format
428 * @todo user-defined page orientation, paper size
429 * @param string the database name
430 * @param string the table name
431 * @param string the end of line sequence
432 * @param string the url to go back in case of error
433 * @param string SQL query for obtaining data
435 * @return bool Whether it suceeded
439 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
442 global $pdf_report_title;
444 $pdf = new PMA_PDF('L', 'pt', 'A3');
446 $pdf->AddFont('DejaVuSans', '', 'dejavusans.php');
447 $pdf->AddFont('DejaVuSans', 'B', 'dejavusans-bold.php');
448 $pdf->AddFont('DejaVuSerif', '', 'dejavuserif.php');
449 $pdf->AddFont('DejaVuSerif', 'B', 'dejavuserif-bold.php');
450 $pdf->SetFont(PMA_PDF_FONT
, '', 11.5);
451 $pdf->AliasNbPages();
452 $attr=array('titleFontSize' => 18, 'titleText' => $pdf_report_title);
453 $pdf->mysql_report($sql_query, $attr);
455 // instead of $pdf->Output():
456 if ($pdf->state
< 3) {
459 if (!PMA_exportOutputHandler($pdf->buffer
)) {
464 } // end of the 'PMA_exportData()' function