[ Index ]
 

Code source de phpMyAdmin 2.10.3

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/libraries/export/ -> pdf.php (source)

   1  <?php
   2  /* vim: set expandtab sw=4 ts=4 sts=4: */
   3  /**
   4   * Produce a PDF report (export) from a query
   5   *
   6   * @version $Id: pdf.php 10375 2007-05-10 00:04:29Z lem9 $
   7   */
   8  
   9  /**
  10   *
  11   */
  12  if (isset($plugin_list)) {
  13      $plugin_list['pdf'] = array(
  14          'text' => 'strPDF',
  15          'extension' => 'pdf',
  16          'mime_type' => 'application/pdf',
  17          'force_file' => true,
  18          'options' => array(
  19              array('type' => 'message_only', 'name' => 'explanation', 'text' => 'strPDFReportExplanation'),
  20              array('type' => 'text', 'name' => 'report_title', 'text' => 'strPDFReportTitle'),
  21              array('type' => 'hidden', 'name' => 'data'),
  22              ),
  23          'options_text' => 'strPDFOptions',
  24          );
  25  } else {
  26  
  27  /**
  28   * Font used in PDF.
  29   *
  30   * @todo Make this configuratble (at least Sans/Serif).
  31   */
  32  define('PMA_PDF_FONT', 'DejaVuSans');
  33  require_once  './libraries/tcpdf/tcpdf.php';
  34  
  35  // Adapted from a LGPL script by Philip Clarke
  36  
  37  class PMA_PDF extends TCPDF
  38  {
  39      var $tablewidths;
  40      var $headerset;
  41      var $footerset;
  42  
  43      // overloading of a tcpdf function:
  44      function _beginpage($orientation)
  45      {
  46          $this->page++;
  47          // solved the problem of overwriting a page, if it already exists
  48          if (!isset($this->pages[$this->page])) {
  49              $this->pages[$this->page] = '';
  50          }
  51          $this->state = 2;
  52          $this->x = $this->lMargin;
  53          $this->y = $this->tMargin;
  54          $this->lasth = 0;
  55          $this->FontFamily = '';
  56  
  57          //Page orientation
  58          if (!$orientation) {
  59              $orientation = $this->DefOrientation;
  60          } else {
  61              $orientation = strtoupper($orientation{0});
  62              if ($orientation != $this->DefOrientation) {
  63                  $this->OrientationChanges[$this->page] = true;
  64              }
  65          }
  66          if ($orientation != $this->CurOrientation) {
  67              //Change orientation
  68              if ($orientation == 'P') {
  69                  $this->wPt = $this->fwPt;
  70                  $this->hPt = $this->fhPt;
  71                  $this->w = $this->fw;
  72                  $this->h = $this->fh;
  73              } else {
  74                  $this->wPt = $this->fhPt;
  75                  $this->hPt = $this->fwPt;
  76                  $this->w = $this->fh;
  77                  $this->h = $this->fw;
  78              }
  79              $this->PageBreakTrigger = $this->h - $this->bMargin;
  80              $this->CurOrientation = $orientation;
  81          }
  82      }
  83  
  84      function Header()
  85      {
  86          global $maxY;
  87  
  88          // Check if header for this page already exists
  89          if (!isset($this->headerset[$this->page])) {
  90              $fullwidth = 0;
  91              foreach ($this->tablewidths as $width) {
  92                  $fullwidth += $width;
  93              }
  94              $this->SetY(($this->tMargin) - ($this->FontSizePt/$this->k)*2);
  95              $this->cellFontSize = $this->FontSizePt ;
  96              $this->SetFont(PMA_PDF_FONT, '', ($this->titleFontSize ? $this->titleFontSize : $this->FontSizePt));
  97              $this->Cell(0, $this->FontSizePt, $this->titleText, 0, 1, 'C');
  98              $l = ($this->lMargin);
  99              $this->SetFont(PMA_PDF_FONT, '', $this->cellFontSize);
 100              foreach ($this->colTitles as $col => $txt) {
 101                  $this->SetXY($l, ($this->tMargin));
 102                  $this->MultiCell($this->tablewidths[$col], $this->FontSizePt, $txt);
 103                  $l += $this->tablewidths[$col] ;
 104                  $maxY = ($maxY < $this->getY()) ? $this->getY() : $maxY ;
 105              }
 106              $this->SetXY($this->lMargin, $this->tMargin);
 107              $this->setFillColor(200, 200, 200);
 108              $l = ($this->lMargin);
 109              foreach ($this->colTitles as $col => $txt) {
 110                  $this->SetXY($l, $this->tMargin);
 111                  $this->cell($this->tablewidths[$col], $maxY-($this->tMargin), '', 1, 0, 'L', 1);
 112                  $this->SetXY($l, $this->tMargin);
 113                  $this->MultiCell($this->tablewidths[$col], $this->FontSizePt, $txt, 0, 'C');
 114                  $l += $this->tablewidths[$col];
 115              }
 116              $this->setFillColor(255, 255, 255);
 117              // set headerset
 118              $this->headerset[$this->page] = 1;
 119          }
 120  
 121          $this->SetY($maxY);
 122      }
 123  
 124      function Footer()
 125      {
 126      // Check if footer for this page already exists
 127          if (!isset($this->footerset[$this->page])) {
 128              $this->SetY(-15);
 129              //Page number
 130              $this->Cell(0, 10, $GLOBALS['strPageNumber'] .' '.$this->PageNo() .'/{nb}', 'T', 0, 'C');
 131  
 132          // set footerset
 133              $this->footerset[$this->page] = 1;
 134          }
 135      }
 136  
 137      function morepagestable($lineheight=8)
 138      {
 139          // some things to set and 'remember'
 140          $l = $this->lMargin;
 141          $startheight = $h = $this->GetY();
 142          $startpage = $currpage = $this->page;
 143  
 144          // calculate the whole width
 145          $fullwidth = 0;
 146          foreach ($this->tablewidths as $width) {
 147              $fullwidth += $width;
 148          }
 149  
 150          // Now let's start to write the table
 151          $row = 0;
 152          $tmpheight = array();
 153          $maxpage = 0;
 154  
 155          while ($data = PMA_DBI_fetch_row($this->results)) {
 156              $this->page = $currpage;
 157              // write the horizontal borders
 158              $this->Line($l, $h, $fullwidth+$l, $h);
 159              // write the content and remember the height of the highest col
 160              foreach ($data as $col => $txt) {
 161                  $this->page = $currpage;
 162                  $this->SetXY($l, $h);
 163                  if ($this->tablewidths[$col] > 0) {
 164                      $this->MultiCell($this->tablewidths[$col], $lineheight, $txt, 0, $this->colAlign[$col]);
 165                      $l += $this->tablewidths[$col];
 166                  }
 167  
 168                  if (!isset($tmpheight[$row.'-'.$this->page])) {
 169                      $tmpheight[$row.'-'.$this->page] = 0;
 170                  }
 171                  if ($tmpheight[$row.'-'.$this->page] < $this->GetY()) {
 172                      $tmpheight[$row.'-'.$this->page] = $this->GetY();
 173                  }
 174                  if ($this->page > $maxpage) {
 175                      $maxpage = $this->page;
 176                  }
 177                  unset($data[$col]);
 178              }
 179  
 180              // get the height we were in the last used page
 181              $h = $tmpheight[$row.'-'.$maxpage];
 182              // set the "pointer" to the left margin
 183              $l = $this->lMargin;
 184              // set the $currpage to the last page
 185              $currpage = $maxpage;
 186              unset($data[$row]);
 187              $row++;
 188          }
 189          // draw the borders
 190          // we start adding a horizontal line on the last page
 191          $this->page = $maxpage;
 192          $this->Line($l, $h, $fullwidth+$l, $h);
 193          // now we start at the top of the document and walk down
 194          for ($i = $startpage; $i <= $maxpage; $i++) {
 195              $this->page = $i;
 196              $l = $this->lMargin;
 197              $t = ($i == $startpage) ? $startheight : $this->tMargin;
 198              $lh = ($i == $maxpage) ? $h : $this->h-$this->bMargin;
 199              $this->Line($l, $t, $l, $lh);
 200              foreach ($this->tablewidths as $width) {
 201                  $l += $width;
 202                  $this->Line($l, $t, $l, $lh);
 203              }
 204          }
 205          // set it to the last page, if not it'll cause some problems
 206          $this->page = $maxpage;
 207      }
 208  
 209  
 210      function mysql_report($query, $attr = array())
 211      {
 212          foreach ($attr as $key => $val){
 213              $this->$key = $val ;
 214          }
 215  
 216          /**
 217           * Pass 1 for column widths
 218           */
 219          $this->results = PMA_DBI_query($query, null, PMA_DBI_QUERY_UNBUFFERED);
 220          $this->numFields  = PMA_DBI_num_fields($this->results);
 221          $this->fields = PMA_DBI_get_fields_meta($this->results);
 222  
 223          // if column widths not set
 224          if (!isset($this->tablewidths)){
 225  
 226              // sColWidth = starting col width (an average size width)
 227              $availableWidth = $this->w - $this->lMargin - $this->rMargin;
 228              $this->sColWidth = $availableWidth / $this->numFields;
 229              $totalTitleWidth = 0;
 230  
 231              // loop through results header and set initial col widths/ titles/ alignment
 232              // if a col title is less than the starting col width, reduce that column size
 233              for ($i = 0; $i < $this->numFields; $i++){
 234                  $stringWidth = $this->getstringwidth($this->fields[$i]->name) + 6 ;
 235                  // save the real title's width
 236                  $titleWidth[$i] = $stringWidth; 
 237                  $totalTitleWidth += $stringWidth;
 238  
 239                  // set any column titles less than the start width to the column title width
 240                  if ($stringWidth < $this->sColWidth){
 241                      $colFits[$i] = $stringWidth ;
 242                  }
 243                  $this->colTitles[$i] = $this->fields[$i]->name;
 244                  $this->display_column[$i] = true;
 245  
 246                  switch ($this->fields[$i]->type){
 247                  case 'int':
 248                      $this->colAlign[$i] = 'R';
 249                      break;
 250                  case 'blob':
 251                  case 'tinyblob':
 252                  case 'mediumblob':
 253                  case 'longblob':
 254                      /**
 255                       * @todo do not deactivate completely the display
 256                       * but show the field's name and [BLOB]
 257                       */
 258                      if (stristr($this->fields[$i]->flags, 'BINARY')) {
 259                          $this->display_column[$i] = false;
 260                          unset($this->colTitles[$i]);
 261                      }
 262                      $this->colAlign[$i] = 'L';
 263                      break;
 264                  default:
 265                      $this->colAlign[$i] = 'L';
 266                  }
 267              }
 268  
 269              // title width verification
 270              if ($totalTitleWidth > $availableWidth) {
 271                  $adjustingMode = true;
 272              } else {
 273                  $adjustingMode = false;
 274                  // we have enough space for all the titles at their
 275                  // original width so use the true title's width
 276                  foreach ($titleWidth as $key => $val) {
 277                      $colFits[$key] = $val;
 278                  }
 279              }
 280  
 281              // loop through the data, any column whose contents is bigger
 282              // than the col size is resized
 283              /**
 284                * @todo force here a LIMIT to avoid reading all rows
 285                */
 286              while ($row = PMA_DBI_fetch_row($this->results)) {
 287                  foreach ($colFits as $key => $val) {
 288                      $stringWidth = $this->getstringwidth($row[$key]) + 6 ;
 289                      if ($adjustingMode && ($stringWidth > $this->sColWidth)) {
 290                      // any column whose data's width is bigger than the start width is now discarded
 291                          unset($colFits[$key]);
 292                      } else {
 293                      // if data's width is bigger than the current column width,
 294                      // enlarge the column (but avoid enlarging it if the
 295                      // data's width is very big)
 296                              if ($stringWidth > $val && $stringWidth < ($this->sColWidth * 3)) {
 297                              $colFits[$key] = $stringWidth ;
 298                          }
 299                      }
 300                  }
 301              }
 302  
 303              $totAlreadyFitted = 0;
 304              foreach ($colFits as $key => $val){
 305                  // set fitted columns to smallest size
 306                  $this->tablewidths[$key] = $val;
 307                  // to work out how much (if any) space has been freed up
 308                  $totAlreadyFitted += $val;
 309              }
 310  
 311              if ($adjustingMode) {
 312                  $surplus = (sizeof($colFits) * $this->sColWidth) - $totAlreadyFitted;
 313                  $surplusToAdd = $surplus / ($this->numFields - sizeof($colFits));
 314              } else {
 315                  $surplusToAdd = 0;
 316              }
 317  
 318              for ($i=0; $i < $this->numFields; $i++) {
 319                  if (!in_array($i, array_keys($colFits))) {
 320                      $this->tablewidths[$i] = $this->sColWidth + $surplusToAdd;
 321                  }
 322                  if ($this->display_column[$i] == false) {
 323                      $this->tablewidths[$i] = 0;
 324                  }
 325              }
 326  
 327              ksort($this->tablewidths);
 328          }
 329          PMA_DBI_free_result($this->results);
 330  
 331          // Pass 2
 332  
 333          $this->results = PMA_DBI_query($query, null, PMA_DBI_QUERY_UNBUFFERED);
 334          $this->Open();
 335          $this->setY($this->tMargin);
 336          $this->AddPage();
 337          $this->morepagestable($this->FontSizePt);
 338          PMA_DBI_free_result($this->results);
 339  
 340      } // end of mysql_report function
 341  
 342  } // end of PMA_PDF class
 343  
 344  /**
 345   * Outputs comment
 346   *
 347   * @param   string      Text of comment
 348   *
 349   * @return  bool        Whether it suceeded
 350   */
 351  function PMA_exportComment($text)
 352  {
 353      return TRUE;
 354  }
 355  
 356  /**
 357   * Outputs export footer
 358   *
 359   * @return  bool        Whether it suceeded
 360   *
 361   * @access  public
 362   */
 363  function PMA_exportFooter()
 364  {
 365      return TRUE;
 366  }
 367  
 368  /**
 369   * Outputs export header
 370   *
 371   * @return  bool        Whether it suceeded
 372   *
 373   * @access  public
 374   */
 375  function PMA_exportHeader()
 376  {
 377      return TRUE;
 378  }
 379  
 380  /**
 381   * Outputs database header
 382   *
 383   * @param   string      Database name
 384   *
 385   * @return  bool        Whether it suceeded
 386   *
 387   * @access  public
 388   */
 389  function PMA_exportDBHeader($db)
 390  {
 391      return TRUE;
 392  }
 393  
 394  /**
 395   * Outputs database footer
 396   *
 397   * @param   string      Database name
 398   *
 399   * @return  bool        Whether it suceeded
 400   *
 401   * @access  public
 402   */
 403  function PMA_exportDBFooter($db)
 404  {
 405      return TRUE;
 406  }
 407  
 408  /**
 409   * Outputs create database database
 410   *
 411   * @param   string      Database name
 412   *
 413   * @return  bool        Whether it suceeded
 414   *
 415   * @access  public
 416   */
 417  function PMA_exportDBCreate($db)
 418  {
 419      return TRUE;
 420  }
 421  
 422  /**
 423   * Outputs the content of a table in PDF format
 424   *
 425   * @todo    user-defined page orientation, paper size
 426   * @param   string      the database name
 427   * @param   string      the table name
 428   * @param   string      the end of line sequence
 429   * @param   string      the url to go back in case of error
 430   * @param   string      SQL query for obtaining data
 431   *
 432   * @return  bool        Whether it suceeded
 433   *
 434   * @access  public
 435   */
 436  function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
 437  {
 438      global $what;
 439      global $pdf_report_title;
 440  
 441      $pdf = new PMA_PDF('L', 'pt', 'A3');
 442  
 443      $pdf->AddFont('DejaVuSans', '', 'dejavusans.php');
 444      $pdf->AddFont('DejaVuSans', 'B', 'dejavusans-bold.php');
 445      $pdf->AddFont('DejaVuSerif', '', 'dejavuserif.php');
 446      $pdf->AddFont('DejaVuSerif', 'B', 'dejavuserif-bold.php');
 447      $pdf->SetFont(PMA_PDF_FONT, '', 11.5);
 448      $pdf->AliasNbPages();
 449      $attr=array('titleFontSize' => 18, 'titleText' => $pdf_report_title);
 450      $pdf->mysql_report($sql_query, $attr);
 451  
 452      // instead of $pdf->Output():
 453      if ($pdf->state < 3) {
 454          $pdf->Close();
 455      }
 456      if (!PMA_exportOutputHandler($pdf->buffer)) {
 457          return FALSE;
 458      }
 459  
 460      return TRUE;
 461  } // end of the 'PMA_exportData()' function
 462  }
 463  ?>


Généré le : Mon Nov 26 15:18:20 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics