From 15a5a8104ddea53509e4898940d3007c8043f260 Mon Sep 17 00:00:00 2001 From: Marc Delisle Date: Fri, 13 Apr 2007 17:26:10 +0000 Subject: [PATCH] bug #1672636 [export] PDF export too wide --- ChangeLog | 1 + libraries/export/pdf.php | 55 ++++++++++++++++++++++++++++++---------- 2 files changed, 42 insertions(+), 14 deletions(-) diff --git a/ChangeLog b/ChangeLog index 497d44569..9079d70fd 100644 --- a/ChangeLog +++ b/ChangeLog @@ -77,6 +77,7 @@ $HeadURL: https://phpmyadmin.svn.sourceforge.net/svnroot/phpmyadmin/trunk/phpMyA - patch #1681620 [interface] support reordering of $cfg['ColumnTypes'], thanks to Leonard den Ottolander - bug #1690718 Can't edit if BLOB and no PK +- bug #1672636 [export] PDF export too wide + [lang] brazilian-portuguese update, thanks to Airon Luis Pereira 2.10.0.3 (not released yet) diff --git a/libraries/export/pdf.php b/libraries/export/pdf.php index cd181aa2c..12bfd7987 100644 --- a/libraries/export/pdf.php +++ b/libraries/export/pdf.php @@ -215,7 +215,6 @@ class PMA_PDF extends TCPDF /** * Pass 1 for column widths - * @todo force here a LIMIT to speed up pass 1 ? */ $this->results = PMA_DBI_query($query, null, PMA_DBI_QUERY_UNBUFFERED); $this->numFields = PMA_DBI_num_fields($this->results); @@ -224,15 +223,21 @@ class PMA_PDF extends TCPDF // if column widths not set if (!isset($this->tablewidths)){ - // starting col width - $this->sColWidth = ($this->w - $this->lMargin - $this->rMargin) / $this->numFields; + // sColWidth = starting col width (an average size width) + $availableWidth = $this->w - $this->lMargin - $this->rMargin; + $this->sColWidth = $availableWidth / $this->numFields; + $totalTitleWidth = 0; // loop through results header and set initial col widths/ titles/ alignment - // if a col title is less than the starting col width / reduce that column size - for ($i=0; $i < $this->numFields; $i++){ + // if a col title is less than the starting col width, reduce that column size + for ($i = 0; $i < $this->numFields; $i++){ $stringWidth = $this->getstringwidth($this->fields[$i]->name) + 6 ; + // save the real title's width + $titleWidth[$i] = $stringWidth; + $totalTitleWidth += $stringWidth; + // set any column titles less than the start width to the column title width - if (($stringWidth) < $this->sColWidth){ + if ($stringWidth < $this->sColWidth){ $colFits[$i] = $stringWidth ; } $this->colTitles[$i] = $this->fields[$i]->name; @@ -261,18 +266,34 @@ class PMA_PDF extends TCPDF } } + // title width verification + if ($totalTitleWidth > $availableWidth) { + $adjustingMode = true; + } else { + $adjustingMode = false; + // we have enough space for all the titles at their + // original width so use the true title's width + foreach ($titleWidth as $key => $val) { + $colFits[$key] = $val; + } + } + // loop through the data, any column whose contents is bigger // than the col size is resized - // TODO: force here a LIMIT to avoid reading all rows + /** + * @todo force here a LIMIT to avoid reading all rows + */ while ($row = PMA_DBI_fetch_row($this->results)) { foreach ($colFits as $key => $val) { $stringWidth = $this->getstringwidth($row[$key]) + 6 ; - if ($stringWidth > $this->sColWidth) { - // any col where row is bigger than the start width is now discarded - unset($colFits[$key]); + if ($adjustingMode && ($stringWidth > $this->sColWidth)) { + // any column whose data's width is bigger than the start width is now discarded + unset($colFits[$key]); } else { - // if text is not bigger than the current column width setting enlarge the column - if ($stringWidth > $val) { + // if data's width is bigger than the current column width, + // enlarge the column (but avoid enlarging it if the + // data's width is very big) + if ($stringWidth > $val && $stringWidth < ($this->sColWidth * 3)) { $colFits[$key] = $stringWidth ; } } @@ -287,10 +308,16 @@ class PMA_PDF extends TCPDF $totAlreadyFitted += $val; } - $surplus = (sizeof($colFits) * $this->sColWidth) - $totAlreadyFitted; + if ($adjustingMode) { + $surplus = (sizeof($colFits) * $this->sColWidth) - $totAlreadyFitted; + $surplusToAdd = $surplus / ($this->numFields - sizeof($colFits)); + } else { + $surplusToAdd = 0; + } + for ($i=0; $i < $this->numFields; $i++) { if (!in_array($i, array_keys($colFits))) { - $this->tablewidths[$i] = $this->sColWidth + ($surplus / ($this->numFields - sizeof($colFits))); + $this->tablewidths[$i] = $this->sColWidth + $surplusToAdd; } if ($this->display_column[$i] == false) { $this->tablewidths[$i] = 0;