Source for file PDF.php
Documentation is available at PDF.php
* Copyright (c) 2006 - 2009 PHPExcel
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
* @package PHPExcel_Writer
* @copyright Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
* @version 1.6.5, 2009-01-05
require_once 'PHPExcel/Writer/IWriter.php';
require_once 'PHPExcel/Cell.php';
require_once 'PHPExcel/RichText.php';
/** PHPExcel_Shared_Drawing */
require_once 'PHPExcel/Shared/Drawing.php';
/** PHPExcel_HashTable */
require_once 'PHPExcel/HashTable.php';
/** PHPExcel_Shared_PDF */
require_once 'PHPExcel/Shared/PDF.php';
* @package PHPExcel_Writer
* @copyright Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
* Temporary storage directory
* Create a new PHPExcel_Writer_PDF
* @param PHPExcel $phpExcel PHPExcel object
* @param string $pFileName
public function save($pFilename = null) {
$fileHandle = fopen($pFilename, 'w');
if ($fileHandle === false) {
throw new Exception("Could not open file $pFilename for writing.");
$pdf = new FPDF('P', 'pt', $paperSize);
foreach ($sheets as $sheet) {
$pdf->SetAutoPageBreak(true);
$pdf->SetFont('Arial', '', 10);
$pdf->AddPage($orientation);
// Get worksheet dimension
$dimension = explode(':', $sheet->calculateWorksheetDimension());
// Calculate column widths
$sheet->calculateColumnWidths();
for ($row = $dimension[0][1]; $row <= $dimension[1][1]; ++ $row) {
for ($column = $dimension[0][0]; $column <= $dimension[1][0]; ++ $column) {
$rowDimension = $sheet->getRowDimension($row);
$cellHeight = $sheet->getStyleByColumnAndRow($column, $row)->getFont()->getSize();
if ($cellHeight > $lineHeight) {
$lineHeight = $cellHeight;
for ($column = $dimension[0][0]; $column <= $dimension[1][0]; ++ $column) {
// Start with defaults...
$pdf->SetFont('Arial', '', 10);
$pdf->SetTextColor(0, 0, 0);
$pdf->SetDrawColor(100, 100, 100);
$pdf->SetFillColor(255, 255, 255);
if ($sheet->cellExistsByColumnAndRow($column, $row)) {
if ($sheet->getCellByColumnAndRow($column, $row)->getValue() instanceof PHPExcel_RichText) {
$cellData = $sheet->getCellByColumnAndRow($column, $row)->getValue()->getPlainText();
$sheet->getCellByColumnAndRow($column, $row)->getCalculatedValue(),
$sheet->getstyle( $sheet->getCellByColumnAndRow($column, $row)->getCoordinate() )->getNumberFormat()->getFormatCode()
$sheet->getCellByColumnAndRow($column, $row)->getValue(),
$sheet->getstyle( $sheet->getCellByColumnAndRow($column, $row)->getCoordinate() )->getNumberFormat()->getFormatCode()
$style = $sheet->getStyleByColumnAndRow($column, $row);
$columnDimension = $sheet->getColumnDimensionByColumn($column);
if ($columnDimension->getWidth() == - 1) {
$columnDimension->setAutoSize(true);
$sheet->calculateColumnWidths(false);
$rowDimension = $sheet->getRowDimension($row);
$cellHeight = $style->getFont()->getSize();
$singleCellWidth = $cellWidth;
$singleCellHeight = $cellHeight;
foreach ($sheet->getMergeCells() as $cells) {
if ($sheet->getCellByColumnAndRow($column, $row)->isInRange($cells)) {
if ($first == $sheet->getCellByColumnAndRow($column, $row)->getCoordinate()) {
$cellWidth = $cellWidth * $colSpan;
$cellHeight = $cellHeight * $rowSpan;
if ($cellHeight < $lineHeight) {
$cellHeight = $lineHeight;
$singleCellHeight = $cellHeight;
if ($style->getFont()->getBold()) {
if ($style->getFont()->getItalic()) {
$pdf->SetFont('Arial', $fontStyle, $style->getFont()->getSize());
switch ($style->getAlignment()->getHorizontal()) {
hexdec(substr($style->getFont()->getColor()->getRGB(), 0, 2)),
hexdec(substr($style->getFont()->getColor()->getRGB(), 2, 2)),
hexdec(substr($style->getFont()->getColor()->getRGB(), 4, 2))
hexdec(substr($style->getFill()->getStartColor()->getRGB(), 0, 2)),
hexdec(substr($style->getFill()->getStartColor()->getRGB(), 2, 2)),
hexdec(substr($style->getFill()->getStartColor()->getRGB(), 4, 2))
hexdec(substr($style->getBorders()->getLeft()->getColor()->getRGB(), 0, 2)),
hexdec(substr($style->getBorders()->getLeft()->getColor()->getRGB(), 2, 2)),
hexdec(substr($style->getBorders()->getLeft()->getColor()->getRGB(), 4, 2))
hexdec(substr($style->getBorders()->getRight()->getColor()->getRGB(), 0, 2)),
hexdec(substr($style->getBorders()->getRight()->getColor()->getRGB(), 2, 2)),
hexdec(substr($style->getBorders()->getRight()->getColor()->getRGB(), 4, 2))
hexdec(substr($style->getBorders()->getTop()->getColor()->getRGB(), 0, 2)),
hexdec(substr($style->getBorders()->getTop()->getColor()->getRGB(), 2, 2)),
hexdec(substr($style->getBorders()->getTop()->getColor()->getRGB(), 4, 2))
hexdec(substr($style->getBorders()->getBottom()->getColor()->getRGB(), 0, 2)),
hexdec(substr($style->getBorders()->getBottom()->getColor()->getRGB(), 2, 2)),
hexdec(substr($style->getBorders()->getBottom()->getColor()->getRGB(), 4, 2))
if ($sheet->getShowGridlines()) {
$iterator = $sheet->getDrawingCollection()->getIterator();
while ($iterator->valid()) {
$iterator->current()->getPath(),
$iterator->current()->getWidth(),
$iterator->current()->getHeight(),
} catch (Exception $ex) { }
// Revert to original Y location
if ($lineHeight < $lineHeight + ($endY - $startY)) {
$lineHeight = $lineHeight + ($endY - $startY);
$pdf->SetX($startX + $singleCellWidth);
if ($sheet->getCellByColumnAndRow($column, $row)->hasHyperlink()) {
if (!$sheet->getCellByColumnAndRow($column, $row)->getHyperlink()->isInternal()) {
$sheet->getCellByColumnAndRow($column, $row)->getHyperlink()->getUrl()
$sheet->garbageCollect();
$pdf->SetTitle($this->_phpExcel->getProperties()->getTitle());
$pdf->SetAuthor($this->_phpExcel->getProperties()->getCreator());
$pdf->SetSubject($this->_phpExcel->getProperties()->getSubject());
$pdf->SetKeywords($this->_phpExcel->getProperties()->getKeywords());
$pdf->SetCreator($this->_phpExcel->getProperties()->getCreator());
fwrite($fileHandle, $pdf->output($pFilename, 'S'));
* @param int $pValue Sheet index
* Write all sheets (resets sheetIndex to NULL)
* Get Pre-Calculate Formulas
* Set Pre-Calculate Formulas
* @param boolean $pValue Pre-Calculate Formulas?
* Get temporary storage directory
* Set temporary storage directory
* @param string $pValue Temporary storage directory
* @throws Exception Exception when directory does not exist
throw new Exception("Directory does not exist: $pValue");
|