PageRenderTime 38ms CodeModel.GetById 18ms app.highlight 12ms RepoModel.GetById 2ms app.codeStats 0ms

/PHPExcel/Writer/PDF/DomPDF.php

https://bitbucket.org/nfredricks/wp-employee-time
PHP | 128 lines | 55 code | 18 blank | 55 comment | 13 complexity | 02241f0c76f1a77c52224c9be60674ba MD5 | raw file
  1<?php
  2/**
  3 * PHPExcel
  4 *
  5 * Copyright (c) 2006 - 2012 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 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
 24 * @license		http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt	LGPL
 25 * @version		1.7.8, 2012-10-12
 26 */
 27
 28
 29/** Require DomPDF library */
 30$pdfRendererClassFile = PHPExcel_Settings::getPdfRendererPath() . '/dompdf_config.inc.php';
 31if (file_exists($pdfRendererClassFile)) {
 32	require_once $pdfRendererClassFile;
 33} else {
 34	throw new Exception('Unable to load PDF Rendering library');
 35}
 36
 37/**
 38 * PHPExcel_Writer_PDF_DomPDF
 39 *
 40 * @category	PHPExcel
 41 * @package		PHPExcel_Writer
 42 * @copyright	Copyright (c) 2006 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
 43 */
 44class PHPExcel_Writer_PDF_DomPDF extends PHPExcel_Writer_PDF_Core implements PHPExcel_Writer_IWriter {
 45	/**
 46	 * Create a new PHPExcel_Writer_PDF
 47	 *
 48	 * @param 	PHPExcel	$phpExcel	PHPExcel object
 49	 */
 50	public function __construct(PHPExcel $phpExcel) {
 51		parent::__construct($phpExcel);
 52	}
 53
 54	/**
 55	 * Save PHPExcel to file
 56	 *
 57	 * @param 	string 		$pFileName
 58	 * @throws 	Exception
 59	 */
 60	public function save($pFilename = null) {
 61		// garbage collect
 62		$this->_phpExcel->garbageCollect();
 63
 64		$saveArrayReturnType = PHPExcel_Calculation::getArrayReturnType();
 65		PHPExcel_Calculation::setArrayReturnType(PHPExcel_Calculation::RETURN_ARRAY_AS_VALUE);
 66
 67		// Open file
 68		$fileHandle = fopen($pFilename, 'w');
 69		if ($fileHandle === false) {
 70			throw new Exception("Could not open file $pFilename for writing.");
 71		}
 72
 73		// Set PDF
 74		$this->_isPdf = true;
 75		// Build CSS
 76		$this->buildCSS(true);
 77
 78		// Default PDF paper size
 79		$paperSize = 'LETTER';	//	Letter	(8.5 in. by 11 in.)
 80
 81		// Check for paper size and page orientation
 82		if (is_null($this->getSheetIndex())) {
 83			$orientation = ($this->_phpExcel->getSheet(0)->getPageSetup()->getOrientation() == PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P';
 84			$printPaperSize = $this->_phpExcel->getSheet(0)->getPageSetup()->getPaperSize();
 85			$printMargins = $this->_phpExcel->getSheet(0)->getPageMargins();
 86		} else {
 87			$orientation = ($this->_phpExcel->getSheet($this->getSheetIndex())->getPageSetup()->getOrientation() == PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P';
 88			$printPaperSize = $this->_phpExcel->getSheet($this->getSheetIndex())->getPageSetup()->getPaperSize();
 89			$printMargins = $this->_phpExcel->getSheet($this->getSheetIndex())->getPageMargins();
 90		}
 91
 92		//	Override Page Orientation
 93		if (!is_null($this->getOrientation())) {
 94			$orientation = ($this->getOrientation() == PHPExcel_Worksheet_PageSetup::ORIENTATION_DEFAULT) ?
 95				PHPExcel_Worksheet_PageSetup::ORIENTATION_PORTRAIT : $this->getOrientation();
 96		}
 97		//	Override Paper Size
 98		if (!is_null($this->getPaperSize())) {
 99			$printPaperSize = $this->getPaperSize();
100		}
101
102		if (isset(self::$_paperSizes[$printPaperSize])) {
103			$paperSize = self::$_paperSizes[$printPaperSize];
104		}
105
106		$orientation = ($orientation == 'L') ? 'landscape' : 'portrait';
107
108		// Create PDF
109		$pdf = new DOMPDF();
110		$pdf->set_paper(strtolower($paperSize), $orientation);
111
112		$pdf->load_html(
113			$this->generateHTMLHeader(false) .
114			$this->generateSheetData() .
115			$this->generateHTMLFooter()
116		);
117		$pdf->render();
118
119		// Write to file
120		fwrite($fileHandle, $pdf->output());
121
122		// Close file
123		fclose($fileHandle);
124
125		PHPExcel_Calculation::setArrayReturnType($saveArrayReturnType);
126	}
127
128}