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

Documentation generated on Mon, 05 Jan 2009 20:38:22 +0100 by phpDocumentor 1.4.1