PHPExcel_Writer
[ class tree: PHPExcel_Writer ] [ index: PHPExcel_Writer ] [ all elements ]

Source for file PDF.php

Documentation is available at PDF.php

  1. <?php
  2. /**
  3.  * PHPExcel
  4.  *
  5.  * Copyright (c) 2006 - 2009 PHPExcel
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  *
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  20.  *
  21.  * @category   PHPExcel
  22.  * @package    PHPExcel_Writer
  23.  * @copyright  Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.7.0, 2009-08-10
  26.  */
  27.  
  28.  
  29. /** PHPExcel root directory */
  30. if (!defined('PHPEXCEL_ROOT')) {
  31.     /**
  32.      * @ignore
  33.      */
  34.     define('PHPEXCEL_ROOT'dirname(__FILE__'/../../');
  35. }
  36.  
  37. /** PHPExcel_IWriter */
  38. require_once PHPEXCEL_ROOT 'PHPExcel/Writer/IWriter.php';
  39.  
  40. /** PHPExcel_Writer_HTML */
  41. require_once PHPEXCEL_ROOT 'PHPExcel/Writer/HTML.php';
  42.  
  43. /** PHPExcel_Cell */
  44. require_once PHPEXCEL_ROOT 'PHPExcel/Cell.php';
  45.  
  46. /** PHPExcel_RichText */
  47. require_once PHPEXCEL_ROOT 'PHPExcel/RichText.php';
  48.  
  49. /** PHPExcel_Shared_Drawing */
  50. require_once PHPEXCEL_ROOT 'PHPExcel/Shared/Drawing.php';
  51.  
  52. /** PHPExcel_HashTable */
  53. require_once PHPEXCEL_ROOT 'PHPExcel/HashTable.php';
  54.  
  55. /** PHPExcel_Shared_PDF */
  56. require_once PHPEXCEL_ROOT 'PHPExcel/Shared/PDF.php';
  57.  
  58.  
  59. /**
  60.  * PHPExcel_Writer_PDF
  61.  *
  62.  * @category   PHPExcel
  63.  * @package    PHPExcel_Writer
  64.  * @copyright  Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  65.  */
  66.     /**
  67.      * Temporary storage directory
  68.      *
  69.      * @var string 
  70.      */
  71.     private $_tempDir = '';
  72.  
  73.     /**
  74.      * Create a new PHPExcel_Writer_PDF
  75.      *
  76.      * @param     PHPExcel    $phpExcel    PHPExcel object
  77.      */
  78.     public function __construct(PHPExcel $phpExcel{
  79.         parent::__construct($phpExcel);
  80.         $this->setUseInlineCss(true);
  81.         $this->_tempDir = sys_get_temp_dir();
  82.     }
  83.  
  84.     /**
  85.      * Save PHPExcel to file
  86.      *
  87.      * @param     string         $pFileName 
  88.      * @throws     Exception
  89.      */
  90.     public function save($pFilename null{
  91.         // garbage collect
  92.         $this->_phpExcel->garbageCollect();
  93.  
  94.         $saveArrayReturnType PHPExcel_Calculation::getArrayReturnType();
  95.         PHPExcel_Calculation::setArrayReturnType(PHPExcel_Calculation::RETURN_ARRAY_AS_VALUE);
  96.  
  97.         // Open file
  98.         $fileHandle fopen($pFilename'w');
  99.         if ($fileHandle === false{
  100.             throw new Exception("Could not open file $pFilename for writing.");
  101.         }
  102.         
  103.         // Set PDF
  104.         $this->_isPdf = true;
  105.  
  106.         // Build CSS
  107.         $this->buildCSS(true);
  108.  
  109.         // Generate HTML
  110.         $html '';
  111.         //$html .= $this->generateHTMLHeader(false);
  112.         $html .= $this->generateSheetData();
  113.         //$html .= $this->generateHTMLFooter();
  114.  
  115.         // Default PDF paper size
  116.         $paperSize 'A4';
  117.         $orientation 'P';
  118.                 
  119.         // Check for overrides
  120.         if (is_null($this->getSheetIndex())) {
  121.             $orientation $this->_phpExcel->getSheet(0)->getPageSetup()->getOrientation(== 'landscape' 'L' 'P';
  122.         else {
  123.             $orientation $this->_phpExcel->getSheet($this->getSheetIndex())->getPageSetup()->getOrientation(== 'landscape' 'L' 'P';
  124.         }
  125.  
  126.         // Create PDF
  127.         $pdf new TCPDF($orientation'pt'$paperSize);
  128.         $pdf->setPrintHeader(false);
  129.         $pdf->setPrintFooter(false);
  130.         $pdf->AddPage();
  131.         $pdf->SetFont('freesans');
  132.         $pdf->writeHTML($html);
  133.  
  134.         // Document info
  135.         $pdf->SetTitle($this->_phpExcel->getProperties()->getTitle());
  136.         $pdf->SetAuthor($this->_phpExcel->getProperties()->getCreator());
  137.         $pdf->SetSubject($this->_phpExcel->getProperties()->getSubject());
  138.         $pdf->SetKeywords($this->_phpExcel->getProperties()->getKeywords());
  139.         $pdf->SetCreator($this->_phpExcel->getProperties()->getCreator());
  140.  
  141.         // Write to file
  142.         fwrite($fileHandle$pdf->output($pFilename'S'));
  143.  
  144.         // Close file
  145.         fclose($fileHandle);
  146.  
  147.         PHPExcel_Calculation::setArrayReturnType($saveArrayReturnType);
  148.     }
  149.  
  150.     /**
  151.      * Get temporary storage directory
  152.      *
  153.      * @return string 
  154.      */
  155.     public function getTempDir({
  156.         return $this->_tempDir;
  157.     }
  158.  
  159.     /**
  160.      * Set temporary storage directory
  161.      *
  162.      * @param     string    $pValue        Temporary storage directory
  163.      * @throws     Exception    Exception when directory does not exist
  164.      * @return PHPExcel_Writer_PDF 
  165.      */
  166.     public function setTempDir($pValue ''{
  167.         if (is_dir($pValue)) {
  168.             $this->_tempDir = $pValue;
  169.         else {
  170.             throw new Exception("Directory does not exist: $pValue");
  171.         }
  172.         return $this;
  173.     }
  174. }

Documentation generated on Mon, 10 Aug 2009 08:07:17 +0200 by phpDocumentor 1.4.1