118 lines
3.0 KiB
PHP
Raw Normal View History

<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
2014-03-27 23:55:06 +07:00
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
2014-05-04 21:03:28 +04:00
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Writer;
use PhpOffice\PhpWord\Exception\Exception;
2014-05-04 21:03:28 +04:00
use PhpOffice\PhpWord\PhpWord;
/**
* RTF writer
*
* @since 0.7.0
*/
class RTF extends AbstractWriter implements WriterInterface
{
/**
* Last paragraph style
*
* @var mixed
*/
private $lastParagraphStyle;
/**
* Create new instance
*
* @param \PhpOffice\PhpWord\PhpWord $phpWord
*/
public function __construct(PhpWord $phpWord = null)
{
$this->setPhpWord($phpWord);
$this->parts = array('Header', 'Document');
foreach ($this->parts as $partName) {
$partClass = get_class($this) . '\\Part\\' . $partName;
if (class_exists($partClass)) {
/** @var \PhpOffice\PhpWord\Writer\RTF\Part\AbstractPart $part Type hint */
$part = new $partClass();
$part->setParentWriter($this);
$this->writerParts[strtolower($partName)] = $part;
}
}
}
/**
* Save PhpWord to file
*
2014-05-07 09:47:02 +07:00
* @param string $filename
* @throws \PhpOffice\PhpWord\Exception\Exception
*/
2014-05-07 09:47:02 +07:00
public function save($filename = null)
{
$content = '';
2014-05-07 09:47:02 +07:00
$filename = $this->getTempFile($filename);
$hFile = fopen($filename, 'w');
if ($hFile !== false) {
$content .= '{';
$content .= '\rtf1' . PHP_EOL;
$content .= $this->getWriterPart('Header')->write();
$content .= $this->getWriterPart('Document')->write();
$content .= '}';
fwrite($hFile, $content);
2014-05-07 09:47:02 +07:00
fclose($hFile);
} else {
2014-05-07 09:47:02 +07:00
throw new Exception("Can't open file");
}
2014-05-07 09:47:02 +07:00
$this->cleanupTempFile();
}
/**
* Get font table
*/
public function getFontTable()
{
return $this->getWriterPart('Header')->getFontTable();
}
/**
* Get color table
*/
public function getColorTable()
{
return $this->getWriterPart('Header')->getColorTable();
}
/**
2014-04-25 23:57:43 +07:00
* Get last paragraph style
*/
2014-04-25 23:57:43 +07:00
public function getLastParagraphStyle()
{
2014-04-25 23:57:43 +07:00
return $this->lastParagraphStyle;
}
/**
2014-04-25 23:57:43 +07:00
* Set last paragraph style
*
* @param mixed $value
*/
2014-04-25 23:57:43 +07:00
public function setLastParagraphStyle($value = '')
{
2014-04-25 23:57:43 +07:00
$this->lastParagraphStyle = $value;
}
}