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 - 2011 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 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license        http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version        1.7.6, 2011-02-27
  26.  */
  27.  
  28.  
  29. /** Require FPDF library */
  30. $k_path_url dirname(__FILE__'/PDF';
  31. require_once PHPEXCEL_ROOT 'PHPExcel/Shared/PDF/tcpdf.php';
  32.  
  33. /**
  34.  * PHPExcel_Writer_PDF
  35.  *
  36.  * @category    PHPExcel
  37.  * @package        PHPExcel_Writer
  38.  * @copyright    Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  39.  */
  40.     /**
  41.      * Temporary storage directory
  42.      *
  43.      * @var string 
  44.      */
  45.     private $_tempDir '';
  46.  
  47.     /**
  48.      * Font
  49.      *
  50.      * @var string 
  51.      */
  52.     private $_font 'freesans';
  53.  
  54.     /**
  55.      * Orientation (Over-ride)
  56.      *
  57.      * @var string 
  58.      */
  59.     private $_orientation    null;
  60.  
  61.     /**
  62.      * Paper size (Over-ride)
  63.      *
  64.      * @var int 
  65.      */
  66.     private $_paperSize    null;
  67.  
  68.  
  69.     /**
  70.      * Paper Sizes xRef List
  71.      *
  72.      * @var array 
  73.      */
  74.     private static $_paperSizes array(
  75.         //    Excel Paper Size                                                    TCPDF Paper Size
  76.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_LETTER                            => 'LETTER',                //    (8.5 in. by 11 in.)
  77.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_LETTER_SMALL                    => 'LETTER',                //    (8.5 in. by 11 in.)
  78.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_TABLOID                            => array(792.00,1224.00),    //    (11 in. by 17 in.)
  79.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_LEDGER                            => array(1224.00,792.00),    //    (17 in. by 11 in.)
  80.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_LEGAL                            => 'LEGAL',                    //    (8.5 in. by 14 in.)
  81.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_STATEMENT                        => array(396.00,612.00),    //    (5.5 in. by 8.5 in.)
  82.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_EXECUTIVE                        => 'EXECUTIVE',                //    (7.25 in. by 10.5 in.)
  83.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_A3                                => 'A3',                    //    (297 mm by 420 mm)
  84.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4                                => 'A4',                    //    (210 mm by 297 mm)
  85.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4_SMALL                        => 'A4',                    //    (210 mm by 297 mm)
  86.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_A5                                => 'A5',                    //    (148 mm by 210 mm)
  87.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_B4                                => 'B4',                    //    (250 mm by 353 mm)
  88.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_B5                                => 'B5',                    //    (176 mm by 250 mm)
  89.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_FOLIO                            => 'FOLIO',                    //    (8.5 in. by 13 in.)
  90.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_QUARTO                            => array(609.45,779.53),    //    (215 mm by 275 mm)
  91.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_STANDARD_1                        => array(720.00,1008.00),    //    (10 in. by 14 in.)
  92.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_STANDARD_2                        => array(792.00,1224.00),    //    (11 in. by 17 in.)
  93.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_NOTE                            => 'LETTER',                //    (8.5 in. by 11 in.)
  94.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_NO9_ENVELOPE                    => array(279.00,639.00),    //    (3.875 in. by 8.875 in.)
  95.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_NO10_ENVELOPE                    => array(297.00,684.00),    //    (4.125 in. by 9.5 in.)
  96.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_NO11_ENVELOPE                    => array(324.00,747.00),    //    (4.5 in. by 10.375 in.)
  97.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_NO12_ENVELOPE                    => array(342.00,792.00),    //    (4.75 in. by 11 in.)
  98.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_NO14_ENVELOPE                    => array(360.00,828.00),    //    (5 in. by 11.5 in.)
  99.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_C                                => array(1224.00,1584.00),    //    (17 in. by 22 in.)
  100.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_D                                => array(1584.00,2448.00),    //    (22 in. by 34 in.)
  101.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_E                                => array(2448.00,3168.00),    //    (34 in. by 44 in.)
  102.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_DL_ENVELOPE                        => array(311.81,623.62),    //    (110 mm by 220 mm)
  103.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_C5_ENVELOPE                        => 'C5',                    //    (162 mm by 229 mm)
  104.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_C3_ENVELOPE                        => 'C3',                    //    (324 mm by 458 mm)
  105.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_C4_ENVELOPE                        => 'C4',                    //    (229 mm by 324 mm)
  106.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_C6_ENVELOPE                        => 'C6',                    //    (114 mm by 162 mm)
  107.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_C65_ENVELOPE                    => array(323.15,649.13),    //    (114 mm by 229 mm)
  108.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_B4_ENVELOPE                        => 'B4',                    //    (250 mm by 353 mm)
  109.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_B5_ENVELOPE                        => 'B5',                    //    (176 mm by 250 mm)
  110.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_B6_ENVELOPE                        => array(498.90,354.33),    //    (176 mm by 125 mm)
  111.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_ITALY_ENVELOPE                    => array(311.81,651.97),    //    (110 mm by 230 mm)
  112.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_MONARCH_ENVELOPE                => array(279.00,540.00),    //    (3.875 in. by 7.5 in.)
  113.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_6_3_4_ENVELOPE                    => array(261.00,468.00),    //    (3.625 in. by 6.5 in.)
  114.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_US_STANDARD_FANFOLD                => array(1071.00,792.00),    //    (14.875 in. by 11 in.)
  115.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_GERMAN_STANDARD_FANFOLD            => array(612.00,864.00),    //    (8.5 in. by 12 in.)
  116.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_GERMAN_LEGAL_FANFOLD            => 'FOLIO',                    //    (8.5 in. by 13 in.)
  117.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_ISO_B4                            => 'B4',                    //    (250 mm by 353 mm)
  118.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_JAPANESE_DOUBLE_POSTCARD        => array(566.93,419.53),    //    (200 mm by 148 mm)
  119.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_STANDARD_PAPER_1                => array(648.00,792.00),    //    (9 in. by 11 in.)
  120.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_STANDARD_PAPER_2                => array(720.00,792.00),    //    (10 in. by 11 in.)
  121.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_STANDARD_PAPER_3                => array(1080.00,792.00),    //    (15 in. by 11 in.)
  122.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_INVITE_ENVELOPE                    => array(623.62,623.62),    //    (220 mm by 220 mm)
  123.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_LETTER_EXTRA_PAPER                => array(667.80,864.00),    //    (9.275 in. by 12 in.)
  124.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_LEGAL_EXTRA_PAPER                => array(667.80,1080.00),    //    (9.275 in. by 15 in.)
  125.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_TABLOID_EXTRA_PAPER                => array(841.68,1296.00),    //    (11.69 in. by 18 in.)
  126.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4_EXTRA_PAPER                    => array(668.98,912.76),    //    (236 mm by 322 mm)
  127.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_LETTER_TRANSVERSE_PAPER            => array(595.80,792.00),    //    (8.275 in. by 11 in.)
  128.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4_TRANSVERSE_PAPER                => 'A4',                    //    (210 mm by 297 mm)
  129.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_LETTER_EXTRA_TRANSVERSE_PAPER    => array(667.80,864.00),    //    (9.275 in. by 12 in.)
  130.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_SUPERA_SUPERA_A4_PAPER            => array(643.46,1009.13),    //    (227 mm by 356 mm)
  131.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_SUPERB_SUPERB_A3_PAPER            => array(864.57,1380.47),    //    (305 mm by 487 mm)
  132.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_LETTER_PLUS_PAPER                => array(612.00,913.68),    //    (8.5 in. by 12.69 in.)
  133.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4_PLUS_PAPER                    => array(595.28,935.43),    //    (210 mm by 330 mm)
  134.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_A5_TRANSVERSE_PAPER                => 'A5',                    //    (148 mm by 210 mm)
  135.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_JIS_B5_TRANSVERSE_PAPER            => array(515.91,728.50),    //    (182 mm by 257 mm)
  136.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_A3_EXTRA_PAPER                    => array(912.76,1261.42),    //    (322 mm by 445 mm)
  137.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_A5_EXTRA_PAPER                    => array(493.23,666.14),    //    (174 mm by 235 mm)
  138.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_ISO_B5_EXTRA_PAPER                => array(569.76,782.36),    //    (201 mm by 276 mm)
  139.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_A2_PAPER                        => 'A2',                    //    (420 mm by 594 mm)
  140.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_A3_TRANSVERSE_PAPER                => 'A3',                    //    (297 mm by 420 mm)
  141.         PHPExcel_Worksheet_PageSetup::PAPERSIZE_A3_EXTRA_TRANSVERSE_PAPER        => array(912.76,1261.42)    //    (322 mm by 445 mm)
  142.     );
  143.  
  144.     /**
  145.      * Create a new PHPExcel_Writer_PDF
  146.      *
  147.      * @param     PHPExcel    $phpExcel    PHPExcel object
  148.      */
  149.     public function __construct(PHPExcel $phpExcel{
  150.         parent::__construct($phpExcel);
  151.         $this->setUseInlineCss(true);
  152.         $this->_tempDir PHPExcel_Shared_File::sys_get_temp_dir();
  153.     }
  154.  
  155.     /**
  156.      * Set font. Examples:
  157.      *   'arialunicid0-chinese-simplified'
  158.      *   'arialunicid0-chinese-traditional'
  159.      *   'arialunicid0-korean'
  160.      *   'arialunicid0-japanese'
  161.      *
  162.      * @param    string    $fontName 
  163.      */
  164.     public function setFont($fontName{
  165.         $this->_font $fontName;
  166.         return $this;
  167.     }
  168.  
  169.     /**
  170.      * Get Paper Size
  171.      *
  172.      * @return int 
  173.      */
  174.     public function getPaperSize({
  175.         return $this->_paperSize;
  176.     }
  177.  
  178.     /**
  179.      * Set Paper Size
  180.      *
  181.      * @param int $pValue 
  182.      * @return PHPExcel_Writer_PDF 
  183.      */
  184.     public function setPaperSize($pValue PHPExcel_Worksheet_PageSetup::PAPERSIZE_LETTER{
  185.         $this->_paperSize $pValue;
  186.         return $this;
  187.     }
  188.  
  189.     /**
  190.      * Get Orientation
  191.      *
  192.      * @return string 
  193.      */
  194.     public function getOrientation({
  195.         return $this->_orientation;
  196.     }
  197.  
  198.     /**
  199.      * Set Orientation
  200.      *
  201.      * @param string $pValue 
  202.      * @return PHPExcel_Writer_PDF 
  203.      */
  204.     public function setOrientation($pValue PHPExcel_Worksheet_PageSetup::ORIENTATION_DEFAULT{
  205.         $this->_orientation $pValue;
  206.         return $this;
  207.     }
  208.  
  209.     /**
  210.      * Save PHPExcel to file
  211.      *
  212.      * @param     string         $pFileName 
  213.      * @throws     Exception
  214.      */
  215.     public function save($pFilename null{
  216.         // garbage collect
  217.         $this->_phpExcel->garbageCollect();
  218.  
  219.         $saveArrayReturnType PHPExcel_Calculation::getArrayReturnType();
  220.         PHPExcel_Calculation::setArrayReturnType(PHPExcel_Calculation::RETURN_ARRAY_AS_VALUE);
  221.  
  222.         // Open file
  223.         $fileHandle fopen($pFilename'w');
  224.         if ($fileHandle === false{
  225.             throw new Exception("Could not open file $pFilename for writing.");
  226.         }
  227.  
  228.         // Set PDF
  229.         $this->_isPdf = true;
  230.  
  231.         // Build CSS
  232.         $this->buildCSS(true);
  233.  
  234.         // Generate HTML
  235.         $html '';
  236.         //$html .= $this->generateHTMLHeader(false);
  237.         $html .= $this->generateSheetData();
  238.         //$html .= $this->generateHTMLFooter();
  239.  
  240.         // Default PDF paper size
  241.         $paperSize 'LETTER';    //    Letter    (8.5 in. by 11 in.)
  242.  
  243.         // Check for paper size and page orientation
  244.         if (is_null($this->getSheetIndex())) {
  245.             $orientation ($this->_phpExcel->getSheet(0)->getPageSetup()->getOrientation(== PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE'L' 'P';
  246.             $printPaperSize $this->_phpExcel->getSheet(0)->getPageSetup()->getPaperSize();
  247.             $printMargins $this->_phpExcel->getSheet(0)->getPageMargins();
  248.         else {
  249.             $orientation ($this->_phpExcel->getSheet($this->getSheetIndex())->getPageSetup()->getOrientation(== PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE'L' 'P';
  250.             $printPaperSize $this->_phpExcel->getSheet($this->getSheetIndex())->getPageSetup()->getPaperSize();
  251.             $printMargins $this->_phpExcel->getSheet($this->getSheetIndex())->getPageMargins();
  252.         }
  253.  
  254.         //    Override Page Orientation
  255.         if (!is_null($this->_orientation)) {
  256.             $orientation ($this->_orientation == PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE'L' 'P';
  257.         }
  258.         //    Override Paper Size
  259.         if (!is_null($this->_paperSize)) {
  260.             $printPaperSize $this->_paperSize;
  261.         }
  262.  
  263.  
  264.         if (isset(self::$_paperSizes[$printPaperSize])) {
  265.             $paperSize self::$_paperSizes[$printPaperSize];
  266.         }
  267.  
  268.  
  269.         // Create PDF
  270.         $pdf new TCPDF($orientation'pt'$paperSize);
  271.         $pdf->setFontSubsetting(false);
  272.         //    Set margins, converting inches to points (using 72 dpi)
  273.         $pdf->SetMargins($printMargins->getLeft(72,$printMargins->getTop(72,$printMargins->getRight(72);
  274.         $pdf->SetAutoPageBreak(true,$printMargins->getBottom(72);
  275. //        $pdf->setHeaderMargin($printMargins->getHeader() * 72);
  276. //        $pdf->setFooterMargin($printMargins->getFooter() * 72);
  277.  
  278.         $pdf->setPrintHeader(false);
  279.         $pdf->setPrintFooter(false);
  280.  
  281.         $pdf->AddPage();
  282.  
  283.         // Set the appropriate font
  284.         $pdf->SetFont($this->_font);
  285.         $pdf->writeHTML($html);
  286.  
  287.         // Document info
  288.         $pdf->SetTitle($this->_phpExcel->getProperties()->getTitle());
  289.         $pdf->SetAuthor($this->_phpExcel->getProperties()->getCreator());
  290.         $pdf->SetSubject($this->_phpExcel->getProperties()->getSubject());
  291.         $pdf->SetKeywords($this->_phpExcel->getProperties()->getKeywords());
  292.         $pdf->SetCreator($this->_phpExcel->getProperties()->getCreator());
  293.  
  294.         // Write to file
  295.         fwrite($fileHandle$pdf->output($pFilename'S'));
  296.  
  297.         // Close file
  298.         fclose($fileHandle);
  299.  
  300.         PHPExcel_Calculation::setArrayReturnType($saveArrayReturnType);
  301.     }
  302.  
  303.     /**
  304.      * Get temporary storage directory
  305.      *
  306.      * @return string 
  307.      */
  308.     public function getTempDir({
  309.         return $this->_tempDir;
  310.     }
  311.  
  312.     /**
  313.      * Set temporary storage directory
  314.      *
  315.      * @param     string    $pValue        Temporary storage directory
  316.      * @throws     Exception    Exception when directory does not exist
  317.      * @return PHPExcel_Writer_PDF 
  318.      */
  319.     public function setTempDir($pValue ''{
  320.         if (is_dir($pValue)) {
  321.             $this->_tempDir $pValue;
  322.         else {
  323.             throw new Exception("Directory does not exist: $pValue");
  324.         }
  325.         return $this;
  326.     }
  327. }

Documentation generated on Sun, 27 Feb 2011 16:33:21 -0800 by phpDocumentor 1.4.3