home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / phpMyAdmin / libraries / export / pdf.php < prev    next >
Encoding:
PHP Script  |  2008-06-23  |  14.6 KB  |  467 lines

  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 11326 2008-06-17 21:32:48Z lem9 $
  7.  */
  8. if (! defined('PHPMYADMIN')) {
  9.     exit;
  10. }
  11.  
  12. /**
  13.  *
  14.  */
  15. if (isset($plugin_list)) {
  16.     $plugin_list['pdf'] = array(
  17.         'text' => 'strPDF',
  18.         'extension' => 'pdf',
  19.         'mime_type' => 'application/pdf',
  20.         'force_file' => true,
  21.         'options' => array(
  22.             array('type' => 'message_only', 'name' => 'explanation', 'text' => 'strPDFReportExplanation'),
  23.             array('type' => 'text', 'name' => 'report_title', 'text' => 'strPDFReportTitle'),
  24.             array('type' => 'hidden', 'name' => 'data'),
  25.             ),
  26.         'options_text' => 'strOptions',
  27.         );
  28. } else {
  29.  
  30. /**
  31.  * Font used in PDF.
  32.  *
  33.  * @todo Make this configuratble (at least Sans/Serif).
  34.  */
  35. define('PMA_PDF_FONT', 'DejaVuSans');
  36. require_once './libraries/tcpdf/tcpdf.php';
  37.  
  38. // Adapted from a LGPL script by Philip Clarke
  39.  
  40. class PMA_PDF extends TCPDF
  41. {
  42.     var $tablewidths;
  43.     var $headerset;
  44.     var $footerset;
  45.  
  46.     // overloading of a tcpdf function:
  47.     function _beginpage($orientation)
  48.     {
  49.         $this->page++;
  50.         // solved the problem of overwriting a page, if it already exists
  51.         if (!isset($this->pages[$this->page])) {
  52.             $this->pages[$this->page] = '';
  53.         }
  54.         $this->state = 2;
  55.         $this->x = $this->lMargin;
  56.         $this->y = $this->tMargin;
  57.         $this->lasth = 0;
  58.         $this->FontFamily = '';
  59.  
  60.         //Page orientation
  61.         if (!$orientation) {
  62.             $orientation = $this->DefOrientation;
  63.         } else {
  64.             $orientation = strtoupper($orientation{0});
  65.             if ($orientation != $this->DefOrientation) {
  66.                 $this->OrientationChanges[$this->page] = true;
  67.             }
  68.         }
  69.         if ($orientation != $this->CurOrientation) {
  70.             //Change orientation
  71.             if ($orientation == 'P') {
  72.                 $this->wPt = $this->fwPt;
  73.                 $this->hPt = $this->fhPt;
  74.                 $this->w = $this->fw;
  75.                 $this->h = $this->fh;
  76.             } else {
  77.                 $this->wPt = $this->fhPt;
  78.                 $this->hPt = $this->fwPt;
  79.                 $this->w = $this->fh;
  80.                 $this->h = $this->fw;
  81.             }
  82.             $this->PageBreakTrigger = $this->h - $this->bMargin;
  83.             $this->CurOrientation = $orientation;
  84.         }
  85.     }
  86.  
  87.     function Header()
  88.     {
  89.         global $maxY;
  90.  
  91.         // Check if header for this page already exists
  92.         if (!isset($this->headerset[$this->page])) {
  93.             $fullwidth = 0;
  94.             foreach ($this->tablewidths as $width) {
  95.                 $fullwidth += $width;
  96.             }
  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 ;
  108.             }
  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];
  118.             }
  119.             $this->setFillColor(255, 255, 255);
  120.             // set headerset
  121.             $this->headerset[$this->page] = 1;
  122.         }
  123.  
  124.         $this->SetY($maxY);
  125.     }
  126.  
  127.     function Footer()
  128.     {
  129.     // Check if footer for this page already exists
  130.         if (!isset($this->footerset[$this->page])) {
  131.             $this->SetY(-15);
  132.             //Page number
  133.             $this->Cell(0, 10, $GLOBALS['strPageNumber'] .' '.$this->PageNo() .'/{nb}', 'T', 0, 'C');
  134.  
  135.         // set footerset
  136.             $this->footerset[$this->page] = 1;
  137.         }
  138.     }
  139.  
  140.     function morepagestable($lineheight=8)
  141.     {
  142.         // some things to set and 'remember'
  143.         $l = $this->lMargin;
  144.         $startheight = $h = $this->GetY();
  145.         $startpage = $currpage = $this->page;
  146.  
  147.         // calculate the whole width
  148.         $fullwidth = 0;
  149.         foreach ($this->tablewidths as $width) {
  150.             $fullwidth += $width;
  151.         }
  152.  
  153.         // Now let's start to write the table
  154.         $row = 0;
  155.         $tmpheight = array();
  156.         $maxpage = 0;
  157.  
  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];
  169.                 }
  170.  
  171.                 if (!isset($tmpheight[$row.'-'.$this->page])) {
  172.                     $tmpheight[$row.'-'.$this->page] = 0;
  173.                 }
  174.                 if ($tmpheight[$row.'-'.$this->page] < $this->GetY()) {
  175.                     $tmpheight[$row.'-'.$this->page] = $this->GetY();
  176.                 }
  177.                 if ($this->page > $maxpage) {
  178.                     $maxpage = $this->page;
  179.                 }
  180.                 unset($data[$col]);
  181.             }
  182.  
  183.             // get the height we were in the last used page
  184.             $h = $tmpheight[$row.'-'.$maxpage];
  185.             // set the "pointer" to the left margin
  186.             $l = $this->lMargin;
  187.             // set the $currpage to the last page
  188.             $currpage = $maxpage;
  189.             unset($data[$row]);
  190.             $row++;
  191.         }
  192.         // draw the borders
  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++) {
  198.             $this->page = $i;
  199.             $l = $this->lMargin;
  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) {
  204.                 $l += $width;
  205.                 $this->Line($l, $t, $l, $lh);
  206.             }
  207.         }
  208.         // set it to the last page, if not it'll cause some problems
  209.         $this->page = $maxpage;
  210.     }
  211.  
  212.  
  213.     function mysql_report($query, $attr = array())
  214.     {
  215.         foreach ($attr as $key => $val){
  216.             $this->$key = $val ;
  217.         }
  218.  
  219.         /**
  220.          * Pass 1 for column widths
  221.          */
  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);
  225.  
  226.         // if column widths not set
  227.         if (!isset($this->tablewidths)){
  228.  
  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;
  233.  
  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;
  241.  
  242.                 // set any column titles less than the start width to the column title width
  243.                 if ($stringWidth < $this->sColWidth){
  244.                     $colFits[$i] = $stringWidth ;
  245.                 }
  246.                 $this->colTitles[$i] = $this->fields[$i]->name;
  247.                 $this->display_column[$i] = true;
  248.  
  249.                 switch ($this->fields[$i]->type){
  250.                 case 'int':
  251.                     $this->colAlign[$i] = 'R';
  252.                     break;
  253.                 case 'blob':
  254.                 case 'tinyblob':
  255.                 case 'mediumblob':
  256.                 case 'longblob':
  257.                     /**
  258.                      * @todo do not deactivate completely the display
  259.                      * but show the field's name and [BLOB]
  260.                      */
  261.                     if (stristr($this->fields[$i]->flags, 'BINARY')) {
  262.                         $this->display_column[$i] = false;
  263.                         unset($this->colTitles[$i]);
  264.                     }
  265.                     $this->colAlign[$i] = 'L';
  266.                     break;
  267.                 default:
  268.                     $this->colAlign[$i] = 'L';
  269.                 }
  270.             }
  271.  
  272.             // title width verification
  273.             if ($totalTitleWidth > $availableWidth) {
  274.                 $adjustingMode = true;
  275.             } else {
  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;
  281.                 }
  282.             }
  283.  
  284.             // loop through the data, any column whose contents is bigger
  285.             // than the col size is resized
  286.             /**
  287.               * @todo force here a LIMIT to avoid reading all rows
  288.               */
  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]);
  295.                     } else {
  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 ;
  301.                         }
  302.                     }
  303.                 }
  304.             }
  305.  
  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;
  312.             }
  313.  
  314.             if ($adjustingMode) {
  315.                 $surplus = (sizeof($colFits) * $this->sColWidth) - $totAlreadyFitted;
  316.                 $surplusToAdd = $surplus / ($this->numFields - sizeof($colFits));
  317.             } else {
  318.                 $surplusToAdd = 0;
  319.             }
  320.  
  321.             for ($i=0; $i < $this->numFields; $i++) {
  322.                 if (!in_array($i, array_keys($colFits))) {
  323.                     $this->tablewidths[$i] = $this->sColWidth + $surplusToAdd;
  324.                 }
  325.                 if ($this->display_column[$i] == false) {
  326.                     $this->tablewidths[$i] = 0;
  327.                 }
  328.             }
  329.  
  330.             ksort($this->tablewidths);
  331.         }
  332.         PMA_DBI_free_result($this->results);
  333.  
  334.         // Pass 2
  335.  
  336.         $this->results = PMA_DBI_query($query, null, PMA_DBI_QUERY_UNBUFFERED);
  337.         $this->Open();
  338.         $this->setY($this->tMargin);
  339.         $this->AddPage();
  340.         $this->morepagestable($this->FontSizePt);
  341.         PMA_DBI_free_result($this->results);
  342.  
  343.     } // end of mysql_report function
  344.  
  345. } // end of PMA_PDF class
  346.  
  347. /**
  348.  * Outputs comment
  349.  *
  350.  * @param   string      Text of comment
  351.  *
  352.  * @return  bool        Whether it suceeded
  353.  */
  354. function PMA_exportComment($text)
  355. {
  356.     return TRUE;
  357. }
  358.  
  359. /**
  360.  * Outputs export footer
  361.  *
  362.  * @return  bool        Whether it suceeded
  363.  *
  364.  * @access  public
  365.  */
  366. function PMA_exportFooter()
  367. {
  368.     return TRUE;
  369. }
  370.  
  371. /**
  372.  * Outputs export header
  373.  *
  374.  * @return  bool        Whether it suceeded
  375.  *
  376.  * @access  public
  377.  */
  378. function PMA_exportHeader()
  379. {
  380.     return TRUE;
  381. }
  382.  
  383. /**
  384.  * Outputs database header
  385.  *
  386.  * @param   string      Database name
  387.  *
  388.  * @return  bool        Whether it suceeded
  389.  *
  390.  * @access  public
  391.  */
  392. function PMA_exportDBHeader($db)
  393. {
  394.     return TRUE;
  395. }
  396.  
  397. /**
  398.  * Outputs database footer
  399.  *
  400.  * @param   string      Database name
  401.  *
  402.  * @return  bool        Whether it suceeded
  403.  *
  404.  * @access  public
  405.  */
  406. function PMA_exportDBFooter($db)
  407. {
  408.     return TRUE;
  409. }
  410.  
  411. /**
  412.  * Outputs create database database
  413.  *
  414.  * @param   string      Database name
  415.  *
  416.  * @return  bool        Whether it suceeded
  417.  *
  418.  * @access  public
  419.  */
  420. function PMA_exportDBCreate($db)
  421. {
  422.     return TRUE;
  423. }
  424.  
  425. /**
  426.  * Outputs the content of a table in PDF format
  427.  *
  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
  434.  *
  435.  * @return  bool        Whether it suceeded
  436.  *
  437.  * @access  public
  438.  */
  439. function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
  440. {
  441.     global $what;
  442.     global $pdf_report_title;
  443.  
  444.     $pdf = new PMA_PDF('L', 'pt', 'A3');
  445.  
  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);
  454.  
  455.     // instead of $pdf->Output():
  456.     if ($pdf->state < 3) {
  457.         $pdf->Close();
  458.     }
  459.     if (!PMA_exportOutputHandler($pdf->buffer)) {
  460.         return FALSE;
  461.     }
  462.  
  463.     return TRUE;
  464. } // end of the 'PMA_exportData()' function
  465. }
  466. ?>
  467.