2 /* vim: set expandtab sw=4 ts=4 sts=4: */
6 * @package PhpMyAdmin-Export
9 if (! defined('PHPMYADMIN')) {
13 /* Get the PDF class */
14 require_once 'libraries/PDF.class.php';
17 * Adapted from a LGPL script by Philip Clarke
19 * @package PhpMyAdmin-Export
22 class PMA_ExportPdf
extends PMA_PDF
30 * @param float $h cell height. Default value: 0
31 * @param mixed $y starting y position, leave empty for current position
32 * @param boolean $addpage if true add a page, otherwise only return
33 * the true/false state
35 * @return boolean true in case of page break, false otherwise.
37 function checkPageBreak($h = 0, $y = '', $addpage = true)
39 if ($this->empty_string($y)) {
42 $current_page = $this->page
;
43 if ((($y +
$h) > $this->PageBreakTrigger
)
44 AND (! $this->InFooter
)
45 AND ($this->AcceptPageBreak())
48 //Automatic page break
50 $this->AddPage($this->CurOrientation
);
51 $this->y
= $this->dataY
;
52 $oldpage = $this->page
- 1;
54 $this_page_orm = $this->pagedim
[$this->page
]['orm'];
55 $old_page_orm = $this->pagedim
[$oldpage]['orm'];
56 $this_page_olm = $this->pagedim
[$this->page
]['olm'];
57 $old_page_olm = $this->pagedim
[$oldpage]['olm'];
59 if ($this_page_orm!= $old_page_orm) {
60 $this->x
= $x - ($this_page_orm - $old_page_orm);
65 if ($this_page_olm != $old_page_olm) {
66 $this->x
= $x +
($this_page_olm - $old_page_olm);
74 if ($current_page != $this->page
) {
75 // account for columns mode
82 * This method is used to render the page header.
89 // We don't want automatic page breaks while generating header
90 // as this can lead to infinite recursion as auto generated page
91 // will want header as well causing another page break
92 // FIXME: Better approach might be to try to compact the content
93 $this->SetAutoPageBreak(false);
94 // Check if header for this page already exists
95 if (! isset($this->headerset
[$this->page
])) {
97 foreach ($this->tablewidths
as $width) {
100 $this->SetY(($this->tMargin
) - ($this->FontSizePt
/ $this->k
) * 5);
101 $this->cellFontSize
= $this->FontSizePt
;
105 ($this->titleFontSize
106 ?
$this->titleFontSize
109 $this->Cell(0, $this->FontSizePt
, $this->titleText
, 0, 1, 'C');
110 $this->SetFont(PMA_PDF_FONT
, '', $this->cellFontSize
);
111 $this->SetY(($this->tMargin
) - ($this->FontSizePt
/ $this->k
) * 2.5);
115 __('Database') . ': ' . $this->currentDb
. ', '
116 . __('Table') . ': ' . $this->currentTable
,
119 $l = ($this->lMargin
);
120 foreach ($this->colTitles
as $col => $txt) {
121 $this->SetXY($l, ($this->tMargin
));
123 $this->tablewidths
[$col],
127 $l +
= $this->tablewidths
[$col] ;
128 $maxY = ($maxY < $this->getY()) ?
$this->getY() : $maxY ;
130 $this->SetXY($this->lMargin
, $this->tMargin
);
131 $this->setFillColor(200, 200, 200);
132 $l = ($this->lMargin
);
133 foreach ($this->colTitles
as $col => $txt) {
134 $this->SetXY($l, $this->tMargin
);
136 $this->tablewidths
[$col],
137 $maxY-($this->tMargin
),
144 $this->SetXY($l, $this->tMargin
);
146 $this->tablewidths
[$col],
152 $l +
= $this->tablewidths
[$col];
154 $this->setFillColor(255, 255, 255);
156 $this->headerset
[$this->page
] = 1;
159 $this->dataY
= $maxY;
160 $this->SetAutoPageBreak(true);
163 function morepagestable($lineheight = 8)
165 // some things to set and 'remember'
167 $startheight = $h = $this->dataY
;
168 $startpage = $currpage = $this->page
;
170 // calculate the whole width
172 foreach ($this->tablewidths
as $width) {
173 $fullwidth +
= $width;
176 // Now let's start to write the table
178 $tmpheight = array();
179 $maxpage = $this->page
;
181 while ($data = PMA_DBI_fetch_row($this->results
)) {
182 $this->page
= $currpage;
183 // write the horizontal borders
184 $this->Line($l, $h, $fullwidth+
$l, $h);
185 // write the content and remember the height of the highest col
186 foreach ($data as $col => $txt) {
187 $this->page
= $currpage;
188 $this->SetXY($l, $h);
189 if ($this->tablewidths
[$col] > 0) {
191 $this->tablewidths
[$col],
195 $this->colAlign
[$col]
197 $l +
= $this->tablewidths
[$col];
200 if (! isset($tmpheight[$row.'-'.$this->page
])) {
201 $tmpheight[$row.'-'.$this->page
] = 0;
203 if ($tmpheight[$row.'-'.$this->page
] < $this->GetY()) {
204 $tmpheight[$row.'-'.$this->page
] = $this->GetY();
206 if ($this->page
> $maxpage) {
207 $maxpage = $this->page
;
212 // get the height we were in the last used page
213 $h = $tmpheight[$row.'-'.$maxpage];
214 // set the "pointer" to the left margin
216 // set the $currpage to the last page
217 $currpage = $maxpage;
222 // we start adding a horizontal line on the last page
223 $this->page
= $maxpage;
224 $this->Line($l, $h, $fullwidth+
$l, $h);
225 // now we start at the top of the document and walk down
226 for ($i = $startpage; $i <= $maxpage; $i++
) {
229 $t = ($i == $startpage) ?
$startheight : $this->tMargin
;
230 $lh = ($i == $maxpage) ?
$h : $this->h
-$this->bMargin
;
231 $this->Line($l, $t, $l, $lh);
232 foreach ($this->tablewidths
as $width) {
234 $this->Line($l, $t, $l, $lh);
237 // set it to the last page, if not it'll cause some problems
238 $this->page
= $maxpage;
242 * Sets a set of attributes.
244 * @param array $attr array containing the attributes
248 function setAttributes($attr = array())
250 foreach ($attr as $key => $val) {
256 * Defines the top margin.
257 * The method can be called before creating the first page.
259 * @param float $topMargin the margin
263 function setTopMargin($topMargin)
265 $this->tMargin
= $topMargin;
268 function mysqlReport($query)
270 unset($this->tablewidths
);
271 unset($this->colTitles
);
272 unset($this->titleWidth
);
273 unset($this->colFits
);
274 unset($this->display_column
);
275 unset($this->colAlign
);
278 * Pass 1 for column widths
280 $this->results
= PMA_DBI_query($query, null, PMA_DBI_QUERY_UNBUFFERED
);
281 $this->numFields
= PMA_DBI_num_fields($this->results
);
282 $this->fields
= PMA_DBI_get_fields_meta($this->results
);
284 // sColWidth = starting col width (an average size width)
285 $availableWidth = $this->w
- $this->lMargin
- $this->rMargin
;
286 $this->sColWidth
= $availableWidth / $this->numFields
;
287 $totalTitleWidth = 0;
289 // loop through results header and set initial
290 // col widths/ titles/ alignment
291 // if a col title is less than the starting col width,
292 // reduce that column size
294 for ($i = 0; $i < $this->numFields
; $i++
) {
295 $stringWidth = $this->getstringwidth($this->fields
[$i]->name
) +
6 ;
296 // save the real title's width
297 $titleWidth[$i] = $stringWidth;
298 $totalTitleWidth +
= $stringWidth;
300 // set any column titles less than the start width to
301 // the column title width
302 if ($stringWidth < $this->sColWidth
) {
303 $colFits[$i] = $stringWidth ;
305 $this->colTitles
[$i] = $this->fields
[$i]->name
;
306 $this->display_column
[$i] = true;
308 switch ($this->fields
[$i]->type
) {
310 $this->colAlign
[$i] = 'R';
317 * @todo do not deactivate completely the display
318 * but show the field's name and [BLOB]
320 if (stristr($this->fields
[$i]->flags
, 'BINARY')) {
321 $this->display_column
[$i] = false;
322 unset($this->colTitles
[$i]);
324 $this->colAlign
[$i] = 'L';
327 $this->colAlign
[$i] = 'L';
331 // title width verification
332 if ($totalTitleWidth > $availableWidth) {
333 $adjustingMode = true;
335 $adjustingMode = false;
336 // we have enough space for all the titles at their
337 // original width so use the true title's width
338 foreach ($titleWidth as $key => $val) {
339 $colFits[$key] = $val;
343 // loop through the data; any column whose contents
344 // is greater than the column size is resized
346 * @todo force here a LIMIT to avoid reading all rows
348 while ($row = PMA_DBI_fetch_row($this->results
)) {
349 foreach ($colFits as $key => $val) {
350 $stringWidth = $this->getstringwidth($row[$key]) +
6 ;
351 if ($adjustingMode && ($stringWidth > $this->sColWidth
)) {
352 // any column whose data's width is bigger than
353 // the start width is now discarded
354 unset($colFits[$key]);
356 // if data's width is bigger than the current column width,
357 // enlarge the column (but avoid enlarging it if the
358 // data's width is very big)
359 if ($stringWidth > $val
360 && $stringWidth < ($this->sColWidth
* 3)
362 $colFits[$key] = $stringWidth ;
368 $totAlreadyFitted = 0;
369 foreach ($colFits as $key => $val) {
370 // set fitted columns to smallest size
371 $this->tablewidths
[$key] = $val;
372 // to work out how much (if any) space has been freed up
373 $totAlreadyFitted +
= $val;
376 if ($adjustingMode) {
377 $surplus = (sizeof($colFits) * $this->sColWidth
) - $totAlreadyFitted;
378 $surplusToAdd = $surplus / ($this->numFields
- sizeof($colFits));
383 for ($i = 0; $i < $this->numFields
; $i++
) {
384 if (! in_array($i, array_keys($colFits))) {
385 $this->tablewidths
[$i] = $this->sColWidth +
$surplusToAdd;
387 if ($this->display_column
[$i] == false) {
388 $this->tablewidths
[$i] = 0;
392 ksort($this->tablewidths
);
394 PMA_DBI_free_result($this->results
);
398 $this->results
= PMA_DBI_query($query, null, PMA_DBI_QUERY_UNBUFFERED
);
399 $this->setY($this->tMargin
);
401 $this->SetFont(PMA_PDF_FONT
, '', 9);
402 $this->morepagestable($this->FontSizePt
);
403 PMA_DBI_free_result($this->results
);
405 } // end of mysqlReport function
407 } // end of PMA_Export_PDF class