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 - 2008 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 - 2008 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.6.4, 2008-10-27
  26.  */
  27.  
  28.  
  29. /** PHPExcel_IWriter */
  30. require_once 'PHPExcel/Writer/IWriter.php';
  31.  
  32. /** PHPExcel_Cell */
  33. require_once 'PHPExcel/Cell.php';
  34.  
  35. /** PHPExcel_RichText */
  36. require_once 'PHPExcel/RichText.php';
  37.  
  38. /** PHPExcel_Shared_Drawing */
  39. require_once 'PHPExcel/Shared/Drawing.php';
  40.  
  41. /** PHPExcel_HashTable */
  42. require_once 'PHPExcel/HashTable.php';
  43.  
  44. /** PHPExcel_Shared_PDF */
  45. require_once 'PHPExcel/Shared/PDF.php';
  46.  
  47.  
  48. /**
  49.  * PHPExcel_Writer_PDF
  50.  *
  51.  * @category   PHPExcel
  52.  * @package    PHPExcel_Writer
  53.  * @copyright  Copyright (c) 2006 - 2008 PHPExcel (http://www.codeplex.com/PHPExcel)
  54.  */
  55. class PHPExcel_Writer_PDF implements PHPExcel_Writer_IWriter {
  56.     /**
  57.      * PHPExcel object
  58.      *
  59.      * @var PHPExcel 
  60.      */
  61.     private $_phpExcel;
  62.  
  63.     /**
  64.      * Sheet index to write
  65.      *
  66.      * @var int 
  67.      */
  68.     private $_sheetIndex;
  69.  
  70.     /**
  71.      * Pre-calculate formulas
  72.      *
  73.      * @var boolean 
  74.      */
  75.     private $_preCalculateFormulas = true;
  76.  
  77.     /**
  78.      * Temporary storage directory
  79.      *
  80.      * @var string 
  81.      */
  82.     private $_tempDir = '';
  83.  
  84.     /**
  85.      * Create a new PHPExcel_Writer_PDF
  86.      *
  87.      * @param     PHPExcel    $phpExcel    PHPExcel object
  88.      */
  89.     public function __construct(PHPExcel $phpExcel{
  90.         $this->_phpExcel = $phpExcel;
  91.         $this->_sheetIndex     = 0;
  92.         $this->_tempDir = sys_get_temp_dir();
  93.     }
  94.  
  95.     /**
  96.      * Save PHPExcel to file
  97.      *
  98.      * @param     string         $pFileName 
  99.      * @throws     Exception
  100.      */
  101.     public function save($pFilename null{
  102.         // Open file
  103.         $fileHandle fopen($pFilename'w');
  104.         if ($fileHandle === false{
  105.             throw new Exception("Could not open file $pFilename for writing.");
  106.         }
  107.  
  108.         // Fetch sheets
  109.         $sheets array();
  110.         if (is_null($this->_sheetIndex)) {
  111.             $sheets $this->_phpExcel->getAllSheets();
  112.         else {
  113.             $sheets[$this->_phpExcel->getSheet($this->_sheetIndex);
  114.         }
  115.  
  116.         // PDF paper size
  117.         $paperSize 'A4';
  118.  
  119.         // Create PDF
  120.         $pdf new FPDF('P''pt'$paperSize);
  121.  
  122.         // Loop all sheets
  123.         foreach ($sheets as $sheet{
  124.             // PDF orientation
  125.             $orientation 'P';
  126.             if ($sheet->getPageSetup()->getOrientation(== PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE{
  127.                 $orientation 'L';
  128.             }
  129.  
  130.             // Start sheet
  131.             $pdf->SetAutoPageBreak(true);
  132.             $pdf->SetFont('Arial'''10);
  133.             $pdf->AddPage($orientation);
  134.  
  135.             // Get worksheet dimension
  136.             $dimension explode(':'$sheet->calculateWorksheetDimension());
  137.             $dimension[0PHPExcel_Cell::coordinateFromString($dimension[0]);
  138.             $dimension[0][0PHPExcel_Cell::columnIndexFromString($dimension[0][0]1;
  139.             $dimension[1PHPExcel_Cell::coordinateFromString($dimension[1]);
  140.             $dimension[1][0PHPExcel_Cell::columnIndexFromString($dimension[1][0]1;
  141.  
  142.             // Calculate column widths
  143.             $sheet->calculateColumnWidths();
  144.  
  145.             // Loop trough cells
  146.             for ($row $dimension[0][1]$row <= $dimension[1][1]++$row{
  147.                 // Line height
  148.                 $lineHeight 0;
  149.  
  150.                 // Calulate line height
  151.                 for ($column $dimension[0][0]$column <= $dimension[1][0]++$column{
  152.                     $rowDimension $sheet->getRowDimension($row);
  153.                     $cellHeight PHPExcel_Shared_Drawing::pixelsToPoints(
  154.                         PHPExcel_Shared_Drawing::cellDimensionToPixels($rowDimension->getRowHeight())
  155.                     );
  156.                     if ($cellHeight <= 0{
  157.                         $cellHeight PHPExcel_Shared_Drawing::pixelsToPoints(
  158.                             PHPExcel_Shared_Drawing::cellDimensionToPixels($sheet->getDefaultRowDimension()->getRowHeight())
  159.                         );
  160.                     }
  161.                     if ($cellHeight <= 0{
  162.                         $cellHeight $sheet->getStyleByColumnAndRow($column$row)->getFont()->getSize();
  163.                     }
  164.                     if ($cellHeight $lineHeight{
  165.                         $lineHeight $cellHeight;
  166.                     }
  167.                 }
  168.  
  169.                 // Output values
  170.                 for ($column $dimension[0][0]$column <= $dimension[1][0]++$column{
  171.                     // Start with defaults...
  172.                     $pdf->SetFont('Arial'''10);
  173.                     $pdf->SetTextColor(000);
  174.                     $pdf->SetDrawColor(100100100);
  175.                     $pdf->SetFillColor(255255255);
  176.  
  177.                     // Coordinates
  178.                     $startX $pdf->GetX();
  179.                     $startY $pdf->GetY();
  180.  
  181.                     // Cell exists?
  182.                     $cellData '';
  183.                     if ($sheet->cellExistsByColumnAndRow($column$row)) {
  184.                         if ($sheet->getCellByColumnAndRow($column$row)->getValue(instanceof PHPExcel_RichText{
  185.                             $cellData $sheet->getCellByColumnAndRow($column$row)->getValue()->getPlainText();
  186.                         else {
  187.                             if ($this->_preCalculateFormulas{
  188.                                 $cellData PHPExcel_Style_NumberFormat::ToFormattedString(
  189.                                     $sheet->getCellByColumnAndRow($column$row)->getCalculatedValue(),
  190.                                     $sheet->getstyle$sheet->getCellByColumnAndRow($column$row)->getCoordinate() )->getNumberFormat()->getFormatCode()
  191.                                 );
  192.                             else {
  193.                                 $cellData PHPExcel_Style_NumberFormat::ToFormattedString(
  194.                                     $sheet->getCellByColumnAndRow($column$row)->getValue(),
  195.                                     $sheet->getstyle$sheet->getCellByColumnAndRow($column$row)->getCoordinate() )->getNumberFormat()->getFormatCode()
  196.                                 );
  197.                             }
  198.                         }
  199.                     }
  200.  
  201.                     // Style information
  202.                     $style $sheet->getStyleByColumnAndRow($column$row);
  203.  
  204.                     // Cell width
  205.                     $columnDimension $sheet->getColumnDimensionByColumn($column);
  206.                     if ($columnDimension->getWidth(== -1{
  207.                         $columnDimension->setAutoSize(true);
  208.                         $sheet->calculateColumnWidths(false);
  209.                     }
  210.                     $cellWidth PHPExcel_Shared_Drawing::pixelsToPoints(
  211.                         PHPExcel_Shared_Drawing::cellDimensionToPixels($columnDimension->getWidth())
  212.                     );
  213.  
  214.                     // Cell height
  215.                     $rowDimension $sheet->getRowDimension($row);
  216.                     $cellHeight PHPExcel_Shared_Drawing::pixelsToPoints(
  217.                         PHPExcel_Shared_Drawing::cellDimensionToPixels($rowDimension->getRowHeight())
  218.                     );
  219.                     if ($cellHeight <= 0{
  220.                         $cellHeight $style->getFont()->getSize();
  221.                     }
  222.  
  223.                     // Column span? Rowspan?
  224.                     $singleCellWidth $cellWidth;
  225.                     $singleCellHeight $cellHeight;
  226.                     foreach ($sheet->getMergeCells(as $cells{
  227.                         if ($sheet->getCellByColumnAndRow($column$row)->isInRange($cells)) {
  228.                             list($firstPHPExcel_Cell::splitRange($cells);
  229.  
  230.                             if ($first == $sheet->getCellByColumnAndRow($column$row)->getCoordinate()) {
  231.                                 list($colSpan$rowSpanPHPExcel_Cell::rangeDimension($cells);
  232.  
  233.                                 $cellWidth $cellWidth $colSpan;
  234.                                 $cellHeight $cellHeight $rowSpan;
  235.                             }
  236.  
  237.                             break;
  238.                         }
  239.                     }
  240.  
  241.                     // Cell height OK?
  242.                     if ($cellHeight $lineHeight{
  243.                         $cellHeight $lineHeight;
  244.                         $singleCellHeight $cellHeight;
  245.                     }
  246.  
  247.                     // Font formatting
  248.                     $fontStyle '';
  249.                     if ($style->getFont()->getBold()) {
  250.                         $fontStyle .= 'B';
  251.                     }
  252.                     if ($style->getFont()->getItalic()) {
  253.                         $fontStyle .= 'I';
  254.                     }
  255.                     if ($style->getFont()->getUnderline(!= PHPExcel_Style_Font::UNDERLINE_NONE{
  256.                         $fontStyle .= 'U';
  257.                     }
  258.                     $pdf->SetFont('Arial'$fontStyle$style->getFont()->getSize());
  259.  
  260.                     // Text alignment
  261.                     $alignment 'L';
  262.                     switch ($style->getAlignment()->getHorizontal()) {
  263.                         case PHPExcel_Style_Alignment::HORIZONTAL_CENTER:
  264.                             $alignment 'C'break;
  265.                         case PHPExcel_Style_Alignment::HORIZONTAL_RIGHT:
  266.                             $alignment 'R'break;
  267.                         case PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY:
  268.                             $alignment 'J'break;
  269.                         case PHPExcel_Style_Alignment::HORIZONTAL_LEFT:
  270.                         case PHPExcel_Style_Alignment::HORIZONTAL_GENERAL:
  271.                         default:
  272.                             $alignment 'L'break;
  273.                     }
  274.  
  275.                     // Text color
  276.                     $pdf->SetTextColor(
  277.                         hexdec(substr($style->getFont()->getColor()->getRGB()02)),
  278.                         hexdec(substr($style->getFont()->getColor()->getRGB()22)),
  279.                         hexdec(substr($style->getFont()->getColor()->getRGB()42))
  280.                     );
  281.  
  282.                     // Fill color
  283.                     if ($style->getFill()->getFillType(!= PHPExcel_Style_Fill::FILL_NONE{
  284.                         $pdf->SetFillColor(
  285.                             hexdec(substr($style->getFill()->getStartColor()->getRGB()02)),
  286.                             hexdec(substr($style->getFill()->getStartColor()->getRGB()22)),
  287.                             hexdec(substr($style->getFill()->getStartColor()->getRGB()42))
  288.                         );
  289.                     }
  290.  
  291.                     // Border color
  292.                     $borders '';
  293.                     if ($style->getBorders()->getLeft()->getBorderStyle(!= PHPExcel_Style_Border::BORDER_NONE{
  294.                         $borders .= 'L';
  295.                         $pdf->SetDrawColor(
  296.                             hexdec(substr($style->getBorders()->getLeft()->getColor()->getRGB()02)),
  297.                             hexdec(substr($style->getBorders()->getLeft()->getColor()->getRGB()22)),
  298.                             hexdec(substr($style->getBorders()->getLeft()->getColor()->getRGB()42))
  299.                         );
  300.                     }
  301.                     if ($style->getBorders()->getRight()->getBorderStyle(!= PHPExcel_Style_Border::BORDER_NONE{
  302.                         $borders .= 'R';
  303.                         $pdf->SetDrawColor(
  304.                             hexdec(substr($style->getBorders()->getRight()->getColor()->getRGB()02)),
  305.                             hexdec(substr($style->getBorders()->getRight()->getColor()->getRGB()22)),
  306.                             hexdec(substr($style->getBorders()->getRight()->getColor()->getRGB()42))
  307.                         );
  308.                     }
  309.                     if ($style->getBorders()->getTop()->getBorderStyle(!= PHPExcel_Style_Border::BORDER_NONE{
  310.                         $borders .= 'T';
  311.                         $pdf->SetDrawColor(
  312.                             hexdec(substr($style->getBorders()->getTop()->getColor()->getRGB()02)),
  313.                             hexdec(substr($style->getBorders()->getTop()->getColor()->getRGB()22)),
  314.                             hexdec(substr($style->getBorders()->getTop()->getColor()->getRGB()42))
  315.                         );
  316.                     }
  317.                     if ($style->getBorders()->getBottom()->getBorderStyle(!= PHPExcel_Style_Border::BORDER_NONE{
  318.                         $borders .= 'B';
  319.                         $pdf->SetDrawColor(
  320.                             hexdec(substr($style->getBorders()->getBottom()->getColor()->getRGB()02)),
  321.                             hexdec(substr($style->getBorders()->getBottom()->getColor()->getRGB()22)),
  322.                             hexdec(substr($style->getBorders()->getBottom()->getColor()->getRGB()42))
  323.                         );
  324.                     }
  325.                     if ($borders == ''{
  326.                         $borders 0;
  327.                     }
  328.                     if ($sheet->getShowGridlines()) {
  329.                         $borders 'LTRB';
  330.                     }
  331.  
  332.                     // Image?
  333.                     $iterator $sheet->getDrawingCollection()->getIterator();
  334.                     while ($iterator->valid()) {
  335.                         if ($iterator->current()->getCoordinates(== PHPExcel_Cell::stringFromColumnIndex($column($row 1)) {
  336.                             try {
  337.                                 $pdf->Image(
  338.                                     $iterator->current()->getPath(),
  339.                                     $pdf->GetX(),
  340.                                     $pdf->GetY(),
  341.                                     $iterator->current()->getWidth(),
  342.                                     $iterator->current()->getHeight(),
  343.                                     '',
  344.                                     $this->_tempDir
  345.                                 );
  346.                             catch (Exception $ex}
  347.                         }
  348.  
  349.                         $iterator->next();
  350.                     }
  351.  
  352.                     // Print cell
  353.                     $pdf->MultiCell(
  354.                         $cellWidth,
  355.                         $cellHeight,
  356.                         $cellData,
  357.                         $borders,
  358.                         $alignment,
  359.                         ($style->getFill()->getFillType(== PHPExcel_Style_Fill::FILL_NONE 1)
  360.                     );
  361.  
  362.                     // Coordinates
  363.                     $endX $pdf->GetX();
  364.                     $endY $pdf->GetY();
  365.  
  366.                     // Revert to original Y location
  367.                     if ($endY $startY{
  368.                         $pdf->SetY($startY);
  369.                         if ($lineHeight $lineHeight ($endY $startY)) {
  370.                             $lineHeight $lineHeight ($endY $startY);
  371.                         }
  372.                     }
  373.                     $pdf->SetX($startX $singleCellWidth);
  374.  
  375.                     // Hyperlink?
  376.                     if ($sheet->getCellByColumnAndRow($column$row)->hasHyperlink()) {
  377.                         if (!$sheet->getCellByColumnAndRow($column$row)->getHyperlink()->isInternal()) {
  378.                             $pdf->Link(
  379.                                 $startX,
  380.                                 $startY,
  381.                                 $endX $startX,
  382.                                 $endY $startY,
  383.                                 $sheet->getCellByColumnAndRow($column$row)->getHyperlink()->getUrl()
  384.                             );
  385.                         }
  386.                     }
  387.                 }
  388.  
  389.                 // Garbage collect!
  390.                 $sheet->garbageCollect();
  391.  
  392.                 // Next line...
  393.                 $pdf->Ln($lineHeight);
  394.             }
  395.         }
  396.  
  397.         // Document info
  398.         $pdf->SetTitle($this->_phpExcel->getProperties()->getTitle());
  399.         $pdf->SetAuthor($this->_phpExcel->getProperties()->getCreator());
  400.         $pdf->SetSubject($this->_phpExcel->getProperties()->getSubject());
  401.         $pdf->SetKeywords($this->_phpExcel->getProperties()->getKeywords());
  402.         $pdf->SetCreator($this->_phpExcel->getProperties()->getCreator());
  403.  
  404.         // Write to file
  405.         fwrite($fileHandle$pdf->output($pFilename'S'));
  406.  
  407.         // Close file
  408.         fclose($fileHandle);
  409.     }
  410.  
  411.     /**
  412.      * Get sheet index
  413.      *
  414.      * @return int 
  415.      */
  416.     public function getSheetIndex({
  417.         return $this->_sheetIndex;
  418.     }
  419.  
  420.     /**
  421.      * Set sheet index
  422.      *
  423.      * @param    int        $pValue        Sheet index
  424.      */
  425.     public function setSheetIndex($pValue 0{
  426.         $this->_sheetIndex = $pValue;
  427.     }
  428.  
  429.     /**
  430.      * Write all sheets (resets sheetIndex to NULL)
  431.      */
  432.     public function writeAllSheets({
  433.         $this->_sheetIndex = null;
  434.     }
  435.  
  436.     /**
  437.      * Get Pre-Calculate Formulas
  438.      *
  439.      * @return boolean 
  440.      */
  441.     public function getPreCalculateFormulas({
  442.         return $this->_preCalculateFormulas;
  443.     }
  444.  
  445.     /**
  446.      * Set Pre-Calculate Formulas
  447.      *
  448.      * @param boolean $pValue    Pre-Calculate Formulas?
  449.      */
  450.     public function setPreCalculateFormulas($pValue true{
  451.         $this->_preCalculateFormulas = $pValue;
  452.     }
  453.  
  454.     /**
  455.      * Get temporary storage directory
  456.      *
  457.      * @return string 
  458.      */
  459.     public function getTempDir({
  460.         return $this->_tempDir;
  461.     }
  462.  
  463.     /**
  464.      * Set temporary storage directory
  465.      *
  466.      * @param     string    $pValue        Temporary storage directory
  467.      * @throws     Exception    Exception when directory does not exist
  468.      */
  469.     public function setTempDir($pValue ''{
  470.         if (is_dir($pValue)) {
  471.             $this->_tempDir = $pValue;
  472.         else {
  473.             throw new Exception("Directory does not exist: $pValue");
  474.         }
  475.     }
  476. }

Documentation generated on Mon, 27 Oct 2008 08:40:58 +0100 by phpDocumentor 1.4.1